Exemplo n.º 1
0
 public string ToKeyString(string password, KeyOptions opts)
 {
     return(JsonConvert.SerializeObject(ToKey(password, opts)));
 }
Exemplo n.º 2
0
        /**
         * Generate key by passphrase and options.
         *
         * @param {Password} password - Provided password.
         * @param {KeyOptions} opts - Key options.
         *
         * @return {Key} Key Object.
         *
         * @example var key = account.toKey("passphrase");
         */
        public Key ToKey(string password, KeyOptions opts = null)
        {
            byte[] derivedKey;
            if (opts.kdf == KDFEnum.Pbkdf2)
            {
                RNGCryptoServiceProvider crypto = new RNGCryptoServiceProvider();
                crypto.GetBytes(opts.salt);
                Rfc2898DeriveBytes pbkdf2 = new Rfc2898DeriveBytes(password, opts.salt, opts.c);
                derivedKey = pbkdf2.GetBytes(opts.dklen);
            }
            else if (opts.kdf == KDFEnum.Scrypt)
            {
                //derivedKey = Replicon.Cryptography.SCrypt.SCrypt.DeriveKey(Encoding.UTF8.GetBytes(password), opts.salt, (ulong)opts.n, (uint)opts.r, (uint)opts.p, (uint)opts.dklen);

                derivedKey = ScryptUtil.Scrypt(Encoding.UTF8.GetBytes(password), opts.salt, opts.n, opts.r, opts.p, opts.dklen);
            }
            else
            {
                throw new Exception("Unsupported kdf");
            }


            var       iv  = opts.iv.Slice(0, opts.iv.Length);
            Aes128CTR aes = new Aes128CTR(iv);

            byte[] key = derivedKey.Slice(0, 16);
            byte[] src = GetPrivateKey();
            byte[] dest;

            using (ICryptoTransform encrypt = aes.CreateEncryptor(key, null))
            {
                dest = encrypt.TransformFinalBlock(src, 0, src.Length);
                //encrypt.TransformBlock(src, 0, src.Length, dest, 0);
            }


            byte[] ciphertext = ByteUtil.Merge(dest, new byte[] { });


            var algoStr = opts.cipher;
            var algoBuf = Encoding.UTF8.GetBytes(algoStr);

            var data = ByteUtil.Merge(derivedKey.Slice(16, 32), ciphertext, opts.iv, algoBuf);

            bool bl = data.Compare(new byte[] { 29, 52, 224, 140, 175, 174, 254, 246, 150, 111, 54, 131, 20, 66, 32, 30, 74, 150, 12, 191, 5, 61, 192, 196, 41, 236, 65, 21, 61, 184, 251, 34, 181, 156, 116, 62, 192, 63, 123, 193, 144, 210, 110, 229, 144, 235, 148, 57, 174, 176, 29, 188, 185, 223, 149, 83, 130, 30, 94, 120, 100, 252, 2, 217, 97, 101, 115, 45, 49, 50, 56, 45, 99, 116, 114 });

            byte[] mac = Sha3Util.Get256Hash(data);

            return(new Key
            {
                version = KEYCURRENTVERSION,
                id = Guid.NewGuid().ToString(),
                address = GetAddressString(),
                crypto = new KeyCrypto
                {
                    ciphertext = ciphertext.ToHex(),
                    cipherparams = new CipherParams()
                    {
                        iv = opts.iv.ToHex()
                    },
                    cipher = opts.cipher,
                    kdf = Enum.GetName(typeof(KDFEnum), opts.kdf).ToLower(),
                    kdfparams = new KDFParams {
                        c = opts.c,
                        salt = opts.salt.ToHex(),
                        dklen = opts.dklen,
                        n = opts.n,
                        p = opts.p,
                        prf = (opts.kdf == KDFEnum.Pbkdf2) ? "hmac-sha256" : "",
                        r = opts.r
                    },
                    mac = mac.ToHex(),
                    machash = "sha3256"
                }
            });
        }