public async Task WriteAllHeaders()
        {
            var database = (await Scenarios.LoadDatabase(PasswordDatabasePath, PasswordDatabasePassword, null));
            var kdb4File = new Kdb4File(database);
            kdb4File.pbMasterSeed = CryptographicBuffer.GenerateRandom(32).AsBytes();
            kdb4File.pbTransformSeed = CryptographicBuffer.GenerateRandom(32).AsBytes();
            kdb4File.pbEncryptionIV = CryptographicBuffer.GenerateRandom(16).AsBytes();
            kdb4File.pbProtectedStreamKey = CryptographicBuffer.GenerateRandom(32).AsBytes();
            kdb4File.pbStreamStartBytes = CryptographicBuffer.GenerateRandom(32).AsBytes();
     

            hw.WriteHeaders(dataWriter, kdb4File);
            var stream = dataWriter.BaseStream;
            stream.Position = 0;
            var factory = new KdbReaderFactory(
                          new WinRTCrypto(),
                          new MultiThreadedBouncyCastleCrypto(),
                          new SHA256HasherRT(),
                          new GZipFactoryRT());

      
            var headerinfo = factory.ReadVersionInfo(stream);
        }
Пример #2
0
        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);
        }