internal static KeyStoreV3 <Pbkdf2Params> EncryptKey(PrivateKey key, string password, Pbkdf2Params kdfParams)
        {
            if (ReferenceEquals(key, null))
            {
                throw new KdfException(ErrorCode.BAD_ARGUMENT, "empty key");
            }
            if (string.IsNullOrEmpty(password))
            {
                throw new KdfException(ErrorCode.BAD_ARGUMENT, "empty password");
            }

            // unsupported prf
            if (kdfParams.prf != Pbkdf2Params.HMACSHA256)
            {
                throw new KdfException(ErrorCode.UNSUPPORTED, $"unsupported kdfparams.prf:{kdfParams.prf}");
            }

            // random values ( salt, iv )
            var salt         = kdfParams.salt;
            var cipherParams = new CipherParams();

            // derivedKey -> cipherKey -> cipherText -> mac
            var derivedKey = PbkdfCrypt.GeneratePbkdf2Sha256DerivedKey(password, salt.HexToBytes(), kdfParams.c, kdfParams.dklen);
            var cipherKey  = PbkdfCrypt.GenerateCipherKey(derivedKey);
            var cipherText = PbkdfCrypt.GenerateAesCtrCipher(cipherParams.iv.HexToBytes(), cipherKey, key.Bytes);
            var mac        = PbkdfCrypt.GenerateMac(derivedKey, cipherText);

            return(new KeyStoreV3 <Pbkdf2Params>()
            {
                version = Version,
                id = Guid.NewGuid().ToString(),
                address = key.Address.HexAddress.ToLower(),
                crypto =
                {
                    ciphertext   = cipherText.ToHex(),
                    cipherparams = cipherParams,
                    cipher       = CIPHER,
                    kdf          = KdfType.pbkdf2.ToString(),
                    kdfparams    = kdfParams,
                    mac          = mac.ToHex()
                }
            });
        }
Esempio n. 2
0
        private static KeyStoreV3 <Pbkdf2Params> EncryptKey(byte[] key, string address, string password, Pbkdf2Params kdfParams)
        {
            if (key.IsNullOrEmpty())
            {
                throw new ArgumentNullException(nameof(key));
            }
            if (password.IsNullOrEmpty())
            {
                throw new ArgumentNullException(nameof(password));
            }
            // unsupported prf
            if (kdfParams.prf != Pbkdf2Params.HMACSHA256)
            {
                throw new ArgumentException("unsupported kdfparams.prf");
            }

            // random values ( salt, iv )
            var salt         = kdfParams.salt;
            var cipherParams = new CipherParams();

            // derivedKey -> cipherKey -> cipherText -> mac
            var derivedKey = PbkdfCrypt.GeneratePbkdf2Sha256DerivedKey(password, salt.ToByteArray(), kdfParams.c, kdfParams.dklen);
            var cipherKey  = PbkdfCrypt.GenerateCipherKey(derivedKey);
            var cipherText = PbkdfCrypt.GenerateAesCtrCipher(cipherParams.iv.ToByteArray(), cipherKey, key);
            var mac        = PbkdfCrypt.GenerateMac(derivedKey, cipherText);

            return(new KeyStoreV3 <Pbkdf2Params>()
            {
                version = Version,
                id = Guid.NewGuid().ToString(),
                address = address,
                crypto =
                {
                    ciphertext   = cipherText.ToHexString(),
                    cipherparams = cipherParams,
                    cipher       = CIPHER,
                    kdf          = KdfType.pbkdf2.ToString(),
                    kdfparams    = kdfParams,
                    mac          = mac.ToHexString()
                }
            });
        }