public void CreateDetachedSecretBoxBadNonce() { SecretBox.CreateDetached( Encoding.UTF8.GetBytes("Adam Caudill"), Encoding.UTF8.GetBytes("ABCDEFGHIJKLMNOPQRSTUVW"), Encoding.UTF8.GetBytes("12345678901234567890123456789012")); }
public void CreateDetachedSecretBoxBadNonce() { Assert.Throws <NonceOutOfRangeException>(() => { SecretBox.CreateDetached( Encoding.UTF8.GetBytes("Adam Caudill"), Encoding.UTF8.GetBytes("ABCDEFGHIJKLMNOPQRSTUVW"), Encoding.UTF8.GetBytes("12345678901234567890123456789012")); }); }
public void OpenDetachedSecretBoxBadNonce() { var actual = SecretBox.CreateDetached( Encoding.UTF8.GetBytes("Adam Caudill"), Encoding.UTF8.GetBytes("ABCDEFGHIJKLMNOPQRSTUVWX"), Encoding.UTF8.GetBytes("12345678901234567890123456789012")); SecretBox.OpenDetached(actual.CipherText, actual.Mac, Encoding.UTF8.GetBytes("ABCDEFGHIJKLMNOPQRSTUVW"), Encoding.UTF8.GetBytes("12345678901234567890123456789012")); }
public void OpenDetachedSecretBoxBadKey() { var actual = SecretBox.CreateDetached( Encoding.UTF8.GetBytes("Adam Caudill"), Encoding.UTF8.GetBytes("ABCDEFGHIJKLMNOPQRSTUVWX"), Encoding.UTF8.GetBytes("12345678901234567890123456789012")); Assert.Throws <KeyOutOfRangeException>(() => { SecretBox.OpenDetached(actual.CipherText, actual.Mac, Encoding.UTF8.GetBytes("ABCDEFGHIJKLMNOPQRSTUVWX"), Encoding.UTF8.GetBytes("123456789012345678901234567890")); }); }
public void DetachedSecretBox() { var expected = Utilities.HexToBinary("4164616d2043617564696c6c"); var actual = SecretBox.CreateDetached( Encoding.UTF8.GetBytes("Adam Caudill"), Encoding.UTF8.GetBytes("ABCDEFGHIJKLMNOPQRSTUVWX"), Encoding.UTF8.GetBytes("12345678901234567890123456789012")); var clear = SecretBox.OpenDetached(actual.CipherText, actual.Mac, Encoding.UTF8.GetBytes("ABCDEFGHIJKLMNOPQRSTUVWX"), Encoding.UTF8.GetBytes("12345678901234567890123456789012")); Assert.AreEqual(clear, expected); }
public static string ExportToKeystore(KdfType kdfType, Wallet wallet, byte[] address, JsonSerializerSettings jsonSettings) { Contract.Assert(!string.IsNullOrWhiteSpace(wallet.PasswordHash)); var passwordHashBytes = Encoding.UTF8.GetBytes(wallet.PasswordHash); var salt = CryptoUtil.RandomBytes(32); var kdfparams = new { dklen = 32, salt = salt.ToHex() }; byte[] derivedKey; switch (kdfType) { case KdfType.Scrypt: { derivedKey = PasswordHash.ScryptHashBinary(passwordHashBytes, salt, PasswordHash.Strength.Sensitive, kdfparams.dklen); break; } case KdfType.Argon: { derivedKey = PasswordHash.ArgonHashBinary(passwordHashBytes, salt, PasswordHash.StrengthArgon.Sensitive, kdfparams.dklen); break; } default: throw new NotSupportedException("Unsupported kdf"); } var iv = CryptoUtil.RandomBytes(32); var pKey = wallet.GetPrivateKeyByAddress(address); var nonce = derivedKey.Take(24).ToArray(); var box = SecretBox.CreateDetached(pKey, nonce, iv); var keystore = new Keystore { Version = KeystoreVersion, Id = wallet.Id, Address = address.ToHex(), Crypto = new KeystoreCrypto { CipherText = box.CipherText.ToHex(), CipherParameters = new KeystoreCryptoParameters { Iv = iv.ToHex() }, Cipher = "crypto_secretbox_detached", Kdf = kdfType.ToString().ToLowerInvariant(), KdfParameters = new KeystoreKdfParameters { Salt = kdfparams.salt, DerivedKeyLength = kdfparams.dklen, }, Mac = box.Mac.ToHex() } }; return(JsonConvert.SerializeObject(keystore, Formatting.None, jsonSettings)); }