public GenerateHashedKeyAsync ( byte masterSeed, byte transformSeed, int rounds ) : Task |
||
masterSeed | byte | |
transformSeed | byte | |
rounds | int | |
Результат | Task |
public async Task<byte[]> GenerateAESKey() { var pwDatabase = file.pwDatabase; var keyGenerator = new KeyGenerator( _hasher, _keyTransformer, pwDatabase.MasterKey, pwDatabase.MasterKey.PercentComplete); var aesKey = await keyGenerator.GenerateHashedKeyAsync(file.pbMasterSeed, file.pbTransformSeed, (int)pwDatabase.KeyEncryptionRounds); return aesKey; }
public async Task Write(PwDatabase databaseData, IFile databaseFile) { kdb4File = new Kdb4File(databaseData); ResetHeaderBytes(); MemoryStream memStream = new MemoryStream(); BinaryWriter writer = new BinaryWriter(memStream); _headerWriter.WriteHeaders(writer, kdb4File); var header = memStream.ToArray(); var hashOfHeader = _hasher.Hash(header); var outStream = new MemoryStream(); await outStream.WriteAsync(kdb4File.pbStreamStartBytes, 0, (int)kdb4File.pbStreamStartBytes.Length); var configuredStream = ConfigureStream(outStream); var persister = new Kdb4Persister(new CryptoRandomStream(CrsAlgorithm.Salsa20, kdb4File.pbProtectedStreamKey, _hasher)); var data = persister.Persist(databaseData.Tree, hashOfHeader); try { await Task.Run(() => configuredStream.Write(data, 0, data.Length)); } catch(Exception e) { } configuredStream.Dispose(); var compressed = outStream.ToArray(); var keyGenerator = new KeyGenerator( _hasher, _keyTransformer, databaseData.MasterKey, databaseData.MasterKey.PercentComplete); var aesKey = await keyGenerator.GenerateHashedKeyAsync(kdb4File.pbMasterSeed, kdb4File.pbTransformSeed, (int)databaseData.KeyEncryptionRounds); var encrypted = await _databaseEncryptor.Encrypt(compressed, aesKey, kdb4File.pbEncryptionIV); writer.Write(encrypted); var streamToWriteToFile = writer.BaseStream; streamToWriteToFile.Position = 0; var bytesToWrite = streamToWriteToFile.ToArray(); var databaseStream = await databaseFile.OpenAsync(FileAccess.ReadAndWrite); databaseStream.SetLength(0); await databaseStream.FlushAsync(); await databaseStream.WriteAsync(bytesToWrite, 0, bytesToWrite.Length); await databaseStream.FlushAsync(); databaseStream.Dispose(); var cryptoStream = new CryptoRandomStream(CrsAlgorithm.Salsa20, kdb4File.pbProtectedStreamKey,_hasher); var parser = new Kdb4Parser(cryptoStream); databaseData.Tree = parser.ParseAndDecode(databaseData.Tree.Document); }