Esempio n. 1
0
        public PrivateKey Unprotect(string passphrase)
        {
            ImmutableArray <byte> derivedKey = Kdf.Derive(passphrase);
            var mac = CalculateMac(derivedKey, Ciphertext);

            if (!Mac.SequenceEqual(mac))
            {
                throw new IncorrectPassphraseException(
                          "The input passphrase is incorrect.",
                          nameof(passphrase),
                          Mac,
                          mac
                          );
            }

            ImmutableArray <byte> encKey    = MakeEncryptionKey(derivedKey);
            ImmutableArray <byte> plaintext = Cipher.Decrypt(encKey, Ciphertext);

            var     key           = new PrivateKey(plaintext.ToArray());
            Address actualAddress = key.PublicKey.ToAddress();

            if (!Address.Equals(actualAddress))
            {
                throw new MismatchedAddressException(
                          "The actual address of the unprotected private key does not match to " +
                          "the expected address of the protected private key.",
                          Address,
                          actualAddress
                          );
            }

            return(key);
        }