Exemplo n.º 1
0
        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);
                }
            }
        }
Exemplo n.º 2
0
        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));
            }
        }