/* * File structure * Byte 1 to 4 = ConfigOffsetLength int32 * Section2: Salt1 * Section3: Salt2 * Section4: PasswordHash 64 bytes * section5: EncodedBlockSize 4 byte * section6: Sha256 EncodedDataValidation 256 bytes * section7: encodedBlock to EOF * * * Byte */ public bool LoadSettings(string filename, string password) { FileStream fs = null; try { fs = File.OpenRead(filename); int bytesRead = 0; var buffer = new byte[4]; bytesRead = fs.Read(buffer, 0, 4); int configOffsetBytes = BitConverter.ToInt32(buffer, 0); if (configOffsetBytes <= 0 || configOffsetBytes > SaltByteLength * 2 + 64) { throw new DataMisalignedException("configOffsetBytes is invalid"); } buffer = new byte[configOffsetBytes]; bytesRead += fs.Read(buffer, 0, buffer.Length); byte[] salt1Bytes = new byte[SaltByteLength]; byte[] salt2Bytes = new byte[SaltByteLength]; byte[] passwordHashBytes = new byte[64]; Buffer.BlockCopy(buffer, 0, salt1Bytes, 0, SaltByteLength); Buffer.BlockCopy(buffer, SaltByteLength, salt2Bytes, 0, SaltByteLength); Buffer.BlockCopy(buffer, SaltByteLength * 2, passwordHashBytes, 0, passwordHashBytes.Length); buffer = new byte[4]; bytesRead += fs.Read(buffer, 0, 4); int encodedBlockSize = BitConverter.ToInt32(buffer, 0); if (encodedBlockSize <= 0 || encodedBlockSize > fs.Length - bytesRead) { throw new DataMisalignedException("configOffsetBytes is invalid"); } buffer = new byte[encodedBlockSize]; fs.Read(buffer, 0, buffer.Length); _otpSettings = new OTPSettings(null, salt1Bytes, salt2Bytes); if (_otpSettings.Decode(buffer, password)) { Initialized = true; } } catch (Exception ex) { Log.Error(ex, "Exception in LoadSettings()"); return(false); } finally { fs?.Close(); } return(true); }
/// <summary> /// Create a new instance of otpSettings /// </summary> /// <param name="password"></param> /// <returns>Password hash</returns> public string Create(string password, Authenticator.SecretKeyLength keyLength) { using (var randomNumberGenerator = RandomNumberGenerator.Create()) { byte[] salt1Bytes = new byte[SaltByteLength]; byte[] salt2Bytes = new byte[SaltByteLength]; byte[] passwordBytes = GeneralConverters.GetByteArrayFromString(password); randomNumberGenerator.GetBytes(salt1Bytes); randomNumberGenerator.GetBytes(salt2Bytes); PasswordHash = CreatePasswordHash(salt1Bytes, salt2Bytes, passwordBytes); _otpSettings = new OTPSettings(Authenticator.GenerateKey(keyLength), salt1Bytes, salt2Bytes); Initialized = true; return(PasswordHash); } }
public OTPGenerator(OTPSettings settings) { _random = new Random(Guid.NewGuid().GetHashCode()); _activeTime = settings.OTPLifeTime; _numberOfDigits = settings.NumberOfDigits; }
private OTPConfigService() { _otpSettings = null; }
public static void AddOTPGenerator(this IServiceCollection services, OTPSettings settings) { var generator = new OTPGenerator(settings); services.AddTransient <IOTPGenerator>(provider => { return(generator); }); }