/* * This file is part of Socialvoid.NET Project (https://github.com/Intellivoid/Socialvoid.NET). * Copyright (c) 2021 Socialvoid.NET Authors. * * This library is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, version 3. * * This library is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this source code of library. * If not, see . */ /* * Credits to Devin Martin and the original OtpSharp library: * https://github.com/kspearrin/Otp.NET */ using System; namespace Socialvoid.Security.Otp { /// /// Some helper methods to perform common key functions. /// since: v0.0.0 /// internal static class KeyUtilities { //------------------------------------------------- #region static Method's Region /// /// Overwrite potentially sensitive data with random junk. /// since: v0.0.0 /// /// /// Warning! /// /// This isn't foolproof by any means. /// The garbage collector could have moved the actual location in memory /// to another location during a collection cycle and left the old data /// in place simply marking it as available. /// We can't control this or even detect it. /// This method is simply a good faith effort to limit the exposure of /// sensitive data in memory as much as possible. /// internal static void Destroy(byte[] sensitiveData) { if(sensitiveData == null || sensitiveData.Length == 0) { // if there is no data, there is nothing to destroy; // don't throw an exception, just return. return; } new Random().NextBytes(sensitiveData); } /// /// converts a long into a big endian byte array. /// since: v0.0.0 /// /// /// RFC 4226 specifies big endian as the method for converting the counter /// to data and then to hash. /// static internal byte[] GetBigEndianBytes(long input) { // Since .net uses little endian numbers, we need to reverse the byte order to get big endian. var data = BitConverter.GetBytes(input); Array.Reverse(data); return data; } /// /// converts an int into a big endian byte array. /// since: v0.0.0 /// /// /// RFC 4226 specifies big endian as the method for converting /// the counter to data and then to hash. /// static internal byte[] GetBigEndianBytes(int input) { // Since .net uses little endian numbers, we need to reverse the byte order to get big endian. var data = BitConverter.GetBytes(input); Array.Reverse(data); return data; } #endregion //------------------------------------------------- } }