예제 #1
0
        private Result PersistKey(Address address, KeyStoreItem keyData)
        {
            var serializedKey = _jsonSerializer.Serialize(keyData);

            try
            {
                // "UTC--2018-12-30T14-04-11.699600594Z--1a959a04db22b9f4360db07125f690449fa97a83"
                DateTime utcNow      = DateTime.UtcNow;
                string   keyFileName = $"UTC--{utcNow:yyyy-MM-dd}T{utcNow:HH-mm-ss.ffffff}000Z--{address.ToString(false, false)}";
                var      path        = Path.Combine(GetStoreDirectory(), keyFileName);
                File.WriteAllText(path, serializedKey, _keyStoreEncoding);
                return(new Result {
                    ResultType = ResultType.Success
                });
            }
            catch (Exception e)
            {
                var msg = $"Error during persisting key for address: {address}";
                if (_logger.IsError)
                {
                    _logger.Error(msg, e);
                }
                return(Result.Fail(msg));
            }
        }
예제 #2
0
        public Result StoreKey(PrivateKey key, SecureString password)
        {
            var salt      = _cryptoRandom.GenerateRandomBytes(32);
            var passBytes = password.ToByteArray(_keyStoreEncoding);

            var derivedKey = SCrypt.ComputeDerivedKey(passBytes, salt, _config.KdfparamsN, _config.KdfparamsR, _config.KdfparamsP, null, _config.KdfparamsDklen);

            var encryptKey     = Keccak.Compute(derivedKey.Take(16).ToArray()).Bytes.Take(16).ToArray();
            var encryptContent = key.KeyBytes;
            var iv             = _cryptoRandom.GenerateRandomBytes(_config.IVSize);

            var cipher = _symmetricEncrypter.Encrypt(encryptContent, encryptKey, iv, _config.Cipher);

            if (cipher == null)
            {
                return(Result.Fail("Error during encryption"));
            }

            var mac = Keccak.Compute(derivedKey.Skip(_config.KdfparamsDklen - 16).Take(16).Concat(cipher).ToArray()).Bytes;

            var address      = key.Address.ToString();
            var keyStoreItem = new KeyStoreItem
            {
                Address = address,
                Crypto  = new Crypto
                {
                    Cipher       = _config.Cipher,
                    CipherText   = cipher.ToHexString(true),
                    CipherParams = new CipherParams
                    {
                        IV = iv.ToHexString(true)
                    },
                    KDF       = _config.Kdf,
                    KDFParams = new KdfParams
                    {
                        DkLen = _config.KdfparamsDklen,
                        N     = _config.KdfparamsN,
                        P     = _config.KdfparamsP,
                        R     = _config.KdfparamsR,
                        Salt  = salt.ToHexString(true)
                    },
                    MAC     = mac.ToHexString(true),
                    Version = CryptoVersion
                },
                Id      = address,
                Version = Version
            };

            var serializedKey = _jsonSerializer.Serialize(keyStoreItem);

            if (serializedKey == null)
            {
                return(Result.Fail("Error during key serialization"));
            }

            return(PersistKey(address, serializedKey));
        }
예제 #3
0
        public Result StoreKey(PrivateKey key, SecureString password)
        {
            if (!password.IsReadOnly())
            {
                throw new InvalidOperationException("Cannot work with password that is not readonly");
            }

            var salt      = _cryptoRandom.GenerateRandomBytes(32);
            var passBytes = password.ToByteArray(_keyStoreEncoding);

            var derivedKey = SCrypt.ComputeDerivedKey(passBytes, salt, _config.KdfparamsN, _config.KdfparamsR, _config.KdfparamsP, null, _config.KdfparamsDklen);

            var encryptKey     = Keccak.Compute(derivedKey.Take(16).ToArray()).Bytes.Take(16).ToArray();
            var encryptContent = key.KeyBytes;
            var iv             = _cryptoRandom.GenerateRandomBytes(_config.IVSize);

            var cipher = _symmetricEncrypter.Encrypt(encryptContent, encryptKey, iv, _config.Cipher);

            if (cipher == null)
            {
                return(Result.Fail("Error during encryption"));
            }

            var mac = Keccak.Compute(derivedKey.Skip(_config.KdfparamsDklen - 16).Take(16).Concat(cipher).ToArray()).Bytes;

            string addressString = key.Address.ToString(false, false);
            var    keyStoreItem  = new KeyStoreItem
            {
                Address = addressString,
                Crypto  = new Crypto
                {
                    Cipher       = _config.Cipher,
                    CipherText   = cipher.ToHexString(false),
                    CipherParams = new CipherParams
                    {
                        IV = iv.ToHexString(false)
                    },
                    KDF       = _config.Kdf,
                    KDFParams = new KdfParams
                    {
                        DkLen = _config.KdfparamsDklen,
                        N     = _config.KdfparamsN,
                        P     = _config.KdfparamsP,
                        R     = _config.KdfparamsR,
                        Salt  = salt.ToHexString(false)
                    },
                    MAC = mac.ToHexString(false),
                },

                Id      = addressString,
                Version = Version
            };

            return(StoreKey(key.Address, keyStoreItem));
        }
예제 #4
0
 public (KeyStoreItem KeyData, Result Result) Verify(string keyJson)
 {
     try
     {
         KeyStoreItem keyData = _jsonSerializer.Deserialize <KeyStoreItem>(keyJson);
         return(keyData, Result.Success);
     }
     catch (Exception)
     {
         return(null, Result.Fail("Invalid key data format"));
     }
 }
예제 #5
0
        private Result Validate(KeyStoreItem keyStoreItem)
        {
            if (keyStoreItem.Crypto?.CipherParams == null || keyStoreItem.Crypto.KDFParams == null)
            {
                return(Result.Fail("Incorrect key"));
            }

            if (keyStoreItem.Version != Version)
            {
                return(Result.Fail("KeyStore version mismatch"));
            }

            return(Result.Success);
        }
예제 #6
0
        private Result PersistKey(Address address, KeyStoreItem keyData)
        {
            var serializedKey = _jsonSerializer.Serialize(keyData);

            try
            {
                var keyFileName    = _keyStoreIOSettingsProvider.GetFileName(address);
                var storeDirectory = _keyStoreIOSettingsProvider.StoreDirectory;
                var path           = Path.Combine(storeDirectory, keyFileName);
                File.WriteAllText(path, serializedKey, _keyStoreEncoding);
                return(new Result {
                    ResultType = ResultType.Success
                });
            }
            catch (Exception e)
            {
                var msg = $"Error during persisting key for address: {address}";
                if (_logger.IsError)
                {
                    _logger.Error(msg, e);
                }
                return(Result.Fail(msg));
            }
        }
예제 #7
0
 public Result StoreKey(Address address, KeyStoreItem keyStoreItem)
 {
     return(PersistKey(address, keyStoreItem));
 }
예제 #8
0
 public Result StoreKey(Address address, KeyStoreItem keyStoreItem)
 {
     throw new System.NotImplementedException();
 }