public WalletOptions AssignFrom(WalletJson wallet) { this.Iv = wallet.Iv; this.MasterKey = wallet.MasterKey; this.Version = wallet.Version; this.Scrypt = wallet.Scrypt; return(this); }
public Wallet(WalletOptions options = null) { options = options ?? new WalletOptions(); this.version = options.Version ?? Wallet.DefaultVersion; switch (this.version) { case 2: this.scryptParams = new ScryptParams(); this.scryptParams.Salt = this.scryptParams.Salt ?? PseudoRandom.RandomBytesAsHexString(this.scryptParams.SaltLength); break; default: break; } string passwordKey = default; if (options.PasswordKeys != null && options.PasswordKeys.ContainsKey(this.version)) { passwordKey = options.PasswordKeys[this.version]; } else { passwordKey = Wallet.ComputePasswordKey(new WalletOptions { Version = this.version, Password = options.Password, Scrypt = this.scryptParams }); } var account = new Account(options.SeedHex); var ivHex = options.Iv ?? PseudoRandom.RandomBytesAsHexString(16); var masterKeyHex = string.IsNullOrWhiteSpace(options.MasterKey) ? PseudoRandom.RandomBytesAsHexString(16) : options.MasterKey; this.Options = options; this.account = account; this.ivHex = ivHex; this.Address = this.account.Address; this.programHash = this.account.ProgramHash; this.masterKey = Aes.Encrypt(masterKeyHex, passwordKey, ivHex.FromHexString()); this.seedEncrypted = Aes.Encrypt(options.SeedHex, masterKeyHex, ivHex.FromHexString()); this.Options.Iv = null; this.Options.SeedHex = null; this.Options.Password = null; this.Options.MasterKey = null; this.Options.PasswordKey = null; this.Options.PasswordKeys.Clear(); }
public void GenerateKeyStore(string password, byte[] pk, string address) { var keyStoreService = new KeyStoreScryptService(); var scryptParams = new ScryptParams { Dklen = 32, N = 262144, R = 1, P = 8 }; KeyStore <ScryptParams> store = keyStoreService.EncryptAndGenerateKeyStore( password, pk, address, scryptParams ); string json = keyStoreService.SerializeKeyStoreToJson(store); GenerateFile(json, $"KeyStore_{DateTime.Now.ToShortDateString().Replace("/","-")}.json"); }
public void GenerateDerivedScryptKey() { ScryptWrapper scryptWrapper = new ScryptWrapper(); ScryptParams param = new ScryptParams(); param.DkLen = 32; param.Salt = "2c37db13a633c5a5e5b8c699109690e33860b7eb43bbc81bbab47d4e9c29f1b9"; param.N = 8192; param.R = 8; param.P = 1; byte[] bytes = scryptWrapper.GetDerivedKey(System.Text.Encoding.Default.GetBytes("stronk_password"), ByteUtil.HexStringToByteArray(param.Salt), param.N, param.R, param.P, param.DkLen); byte[] macArray = HashUtil.GenerateMac(bytes, ByteUtil.HexStringToByteArray("ecdf81453d031ac2fa068b7185ddac044fa4632d3b061400d3c07a86510b4823")); Console.WriteLine(ByteUtil.ByteArrayToHexString(macArray)); Assert.AreEqual("ed7fa37a4adbc8b7bbe0d43a329a047f89e2dcf7f2dfc96babfe79edd955f7a3", ByteUtil.ByteArrayToHexString(macArray).ToLower()); }
static void Main(string[] args) { var keyStoreService = new Nethereum.KeyStore.KeyStoreScryptService(); // lower cost than default N == 262144 as this is using wasm, the lower the easier to compute but also easier to crack var scryptParams = new ScryptParams { Dklen = 32, N = 32, R = 1, P = 8 }; var ecKey = Nethereum.Signer.EthECKey.GenerateKey(); Console.WriteLine("Private key:" + ecKey.GetPrivateKey()); var password = "******"; // encrypting using our custome scrypt params var keyStore = keyStoreService.EncryptAndGenerateKeyStore(password, ecKey.GetPrivateKeyAsBytes(), ecKey.GetPublicAddress(), scryptParams); var json = keyStoreService.SerializeKeyStoreToJson(keyStore); Console.WriteLine(json); //decrypting our key var key = keyStoreService.DecryptKeyStoreFromJson(password, json); Console.WriteLine("Private key decrypted:" + key.ToHex(true)); }
public static string EncryptKeyStoreV3(PrivateKey key, string password, ScryptParams kdfParams) { return(EncryptKey(key.ToByteArray(), key.Address, password, kdfParams).ToJson()); }
private static KeyStoreV3 <ScryptParams> EncryptKey(byte[] key, string address, string password, ScryptParams kdfParams) { if (key.IsNullOrEmpty()) { throw new ArgumentNullException(nameof(key)); } if (password.IsNullOrEmpty()) { throw new ArgumentNullException(nameof(password)); } // random values ( salt, iv ) var salt = kdfParams.salt; var cipherParams = new CipherParams(); // derivedKey -> cipherKey -> cipherText -> mac var derivedKey = PbkdfCrypt.GenerateDerivedScryptKey(password, salt.ToByteArray(), kdfParams.n, kdfParams.r, kdfParams.p, 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 <ScryptParams>() { version = Version, id = Guid.NewGuid().ToString(), address = address, crypto = { ciphertext = cipherText.ToHexString(), cipherparams = cipherParams, cipher = CIPHER, kdf = KdfType.scrypt.ToString(), kdfparams = kdfParams, mac = mac.ToHexString() } }); }
internal static KeyStoreV3 <ScryptParams> EncryptKey(PrivateKey key, string password, ScryptParams 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"); } // random values ( salt, iv ) var salt = kdfParams.salt; var cipherParams = new CipherParams(); // derivedKey -> cipherKey -> cipherText -> mac var derivedKey = PbkdfCrypt.GenerateDerivedScryptKey(password, salt.HexToBytes(), kdfParams.n, kdfParams.r, kdfParams.p, 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 <ScryptParams>() { version = Version, id = Guid.NewGuid().ToString(), address = key.Address.HexAddress.ToLower(), crypto = { ciphertext = cipherText.ToHex(), cipherparams = cipherParams, cipher = CIPHER, kdf = KdfType.scrypt.ToString(), kdfparams = kdfParams, mac = mac.ToHex() } }); }
/// <summary> /// Encrypt PrivateKey to KeyStoreV3 json string with password using Scrypt /// </summary> /// <exception cref="KdfException"></exception> public static string EncryptKeyStoreV3AsJson(PrivateKey key, string password, ScryptParams kdfParams) { try { return(PbkdfService.EncryptKey(key, password, kdfParams).ToJson()); } catch (KdfException e) { blog.error($"WrongHmacException! e={e.ErrorMessage}"); throw; } }