public async Task LoadPwDatabase(IStorageFile pwDatabaseFile, IList<IUserKey> userKeys, IProgress<double> percentComplete)
        {
            StorageFile = pwDatabaseFile;
            var factory = new KdbReaderFactory(_encryptionEngine,
                      _keyTransformer,
                      _hasher,
                      _gzipStreamFactory);

            var file = await pwDatabaseFile.OpenAsync(FileAccessMode.Read);

            Stream kdbDataReader = file.AsStream();

            this.PwDatabase = await factory.LoadAsync(kdbDataReader, userKeys, percentComplete);
        }
Exemplo n.º 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);
        }
        public async void Save()
        {
            CanSave = false;
            var databaseXDocument = GenerateDatabase.NewDatabase();
            var parser = new Kdb4Parser(null);
            var passwordKey = await KcpPassword.Create(Password, new SHA256HasherRT());

            var compositeKey = new CompositeKey(new List<IUserKey> { passwordKey }, new NullableProgress<double>());
            var pwDatabase = new PwDatabase(compositeKey);



            pwDatabase.Tree = parser.ParseXmlDocument(databaseXDocument);
            pwDatabase.DataCipherUuid = new PwUuid(new byte[]{
						0x31, 0xC1, 0xF2, 0xE6, 0xBF, 0x71, 0x43, 0x50,
						0xBE, 0x58, 0x05, 0x21, 0x6A, 0xFC, 0x5A, 0xFF });


            if (await _pageServices.EnsureUnsnapped())
            {
                FileSavePicker saver = new FileSavePicker();
                saver.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;
                saver.SuggestedFileName = this.Name;
                saver.FileTypeChoices.Add(".kdbx", new List<string> { ".kdbx" });
                var storageFile = await saver.PickSaveFileAsync();

                if (storageFile != null)
                {

                    _dataSource.PwDatabase = pwDatabase;
                    _dataSource.StorageFile = storageFile;
                    await _dataSource.SavePwDatabase();
                    var encodedUUID = _dataSource.PwDatabase.Tree.Group.UUID;
                    _navigationService.UriFor<EntryGroupListViewModel>().WithParam(vm => vm.GroupId, encodedUUID).Navigate();
                }
            }


        }
        private async void NavigateToCreateDemo(object obj)
        {
            var databaseXDocument = GenerateDatabase.NewDatabase();
            var parser = new Kdb4Parser(null);
            var passwordKey = await KcpPassword.Create("demo", _hasher);

            var compositeKey = new CompositeKey(new List<IUserKey> { passwordKey }, new NullableProgress<double>());
            var pwDatabase = new PwDatabase(compositeKey);



            pwDatabase.Tree = parser.ParseXmlDocument(databaseXDocument);
            pwDatabase.DataCipherUuid = new PwUuid(new byte[]{
						0x31, 0xC1, 0xF2, 0xE6, 0xBF, 0x71, 0x43, 0x50,
						0xBE, 0x58, 0x05, 0x21, 0x6A, 0xFC, 0x5A, 0xFF });


            _databaseSource.PwDatabase = pwDatabase;

            await _databaseInfoRepository.SaveDatabaseFromDatasouce("demo.kdbx", "", "", new MemoryStream());
            var databaseInfo = await _databaseInfoRepository.GetDatabaseInfo("demo.kdbx");

            _databaseSource.StorageFile = await databaseInfo.GetDatabase();
            await _databaseSource.SavePwDatabase();

            _dialogService.ShowDialogBox("Demo information", "The password to the database is 'demo'");

            _navigationService.GoBack();
        }