public async Task <PasswordEntry> Add(PasswordEntry passwordEntry)
        {
            using (var masterKey = await _masterKeyManager.Get())
            {
                var iv = AesKey.Generate().Iv;

                var newPassword = new PasswordModel(
                    iv,
                    await GetEncryptedSecureString(passwordEntry.Password, masterKey.Value, iv),
                    await _aesCrypter.Encrypt(Encoding.ASCII.GetBytes(passwordEntry.Description), masterKey.Value, iv),
                    await _aesCrypter.Encrypt(Encoding.ASCII.GetBytes(passwordEntry.UserName), masterKey.Value, iv));

                var currentData = await _fileStorage.Read();

                var updatedPasswords = currentData.Passwords.Append(newPassword);

                await _fileStorage.Store(
                    new StorageModel(currentData.EncryptedMasterKey, currentData.Iv, updatedPasswords.ToList()));

                return(new PasswordEntry(newPassword.Id, passwordEntry.Password, passwordEntry.UserName, passwordEntry.Description));
            }
        }
        public async Task Create(SecureString masterPassword)
        {
            using (var masterKey = AesKey.Generate())
            {
                using (var derivedKey = await Argon2Key.Calculate(masterPassword))
                {
                    var encryptedMasterKey = await _aesCrypter.Encrypt(masterKey.Key, derivedKey.Value, masterKey.Iv);

                    var storableModel = new StorageModel(encryptedMasterKey, (byte[])masterKey.Iv.Clone(), new List <PasswordModel>());
                    await _fileStorage.Store(storableModel);

                    _masterKey = new byte[masterKey.Key.Length];
                    masterKey.Key.CopyTo(_masterKey, 0);
                    ProtectedMemory.Protect(_masterKey, MemoryProtectionScope.SameProcess);
                }
            }
        }
Beispiel #3
0
        async void doSomthingAsych()
        {
            try
            {
                FileStream data = (FileStream)openFileDialog1.OpenFile();

                setProgress(2);
                Stream encrypted = aes.Encrypt(data);
                setProgress(4);
                FileStream s = (FileStream)saveFileDialog1.OpenFile();
                setProgress(6);
                encrypted.CopyTo(s);

                encrypted.Close();
                data.Close();
                s.Close();
                setProgress(10);
            }
            catch (Exception ex)
            {
                setColor(Color.Red);
                appedConsole($"Error: {ex.ToString()}\n");
            }
        }