/// <summary> /// Create KeyPair from private key /// </summary> /// <param name="privateKey">The private key</param> /// <returns></returns> public static KeyPair CreateFromPrivateKey(string privateKey) { Guard.NotNullOrEmpty(privateKey, nameof(privateKey)); Guard.NotEqualTo(privateKey.Length, 64, new ArgumentOutOfRangeException(nameof(privateKey))); // var privateKeyArray = privateKey.FromHex(); // var publicKey = Ed25519.PublicKeyFromSeed(privateKeyArray).ToHexUpper(); var privateKeyArray = CryptoBytes.FromHexString(privateKey); var publicKey = CryptoBytes.ToHexStringUpper(Ed25519.PublicKeyFromSeed(privateKeyArray)); return(new KeyPair(privateKey, publicKey)); }
/// <summary> /// Generates new keys using a given seed (private key). /// </summary> /// <param name="PrivateKey">32 byte private key seed to generate public key and extended private key from.</param> /// <returns>Structure holding newly generated keys.</returns> public static KeysEd25519 GenerateKeys(byte[] PrivateKey) { byte[] publicKey; byte[] expandedPrivateKey; Chaos.NaCl.Ed25519.KeyPairFromSeed(out publicKey, out expandedPrivateKey, PrivateKey); KeysEd25519 res = new KeysEd25519(); res.PublicKey = publicKey; res.PublicKeyHex = CryptoBytes.ToHexStringUpper(res.PublicKey); res.PrivateKey = PrivateKey; res.PrivateKeyHex = CryptoBytes.ToHexStringUpper(res.PrivateKey); res.ExpandedPrivateKey = expandedPrivateKey; res.ExpandedPrivateKeyHex = CryptoBytes.ToHexStringUpper(res.ExpandedPrivateKey); return(res); }
internal ByteArrayWtihSignature Transaction(byte[] bytes) { if (null == bytes) { throw new ArgumentNullException(nameof(bytes)); } // produce the signature var r = CryptoBytes.ToHexStringUpper(new Signature(bytes, PrivateKey)._Signature); ByteArrayWtihSignature byteArrayWithSignature = new ByteArrayWtihSignature { data = CryptoBytes.ToHexStringUpper(bytes), signature = r.ToUpper() }; return(byteArrayWithSignature); }
public void Should_Sign_Data_And_Verify_Signature_SHA3_256() { var pk = "A574ECE8F79DE11A39C6BADF0EF87C2C88730A5EA4CF2C0BD7E27103390BC4F4"; var pubKey = "2B0FF0CADE0D945A23D1AF7AF266A0BAB7E07B163756F5F16CC75F24A4EEF23B"; var keyPair = KeyPair.CreateFromPrivateKey(pk); var message = "This is a test data"; var data = Encoding.UTF8.GetBytes(message); var encryptedData = CryptoUtils.Sha3_256(data); var encryptedHex = CryptoBytes.ToHexStringUpper(encryptedData); var signature = keyPair.Sign(encryptedData); var sigHex = CryptoBytes.ToHexStringUpper(signature); var sig2 = CryptoBytes.FromHexString(sigHex); var publicAccount = PublicAccount.CreateFromPublicKey(pubKey, NetworkType.MIJIN_TEST); var isValid = publicAccount.VerifySignature(encryptedData, sig2); isValid.Should().BeTrue(); }
/// <summary> /// Converts a binary data to an uppercase hexadecimal string representation. /// </summary> /// <param name="Data">Data to convert to hex string.</param> /// <returns>Uppercase hex string representing the data.</returns> public static string ToHex(byte[] Data) { return(CryptoBytes.ToHexStringUpper(Data)); }
public void ToHexStringUpperNull() { Assert.AreEqual(null, CryptoBytes.ToHexStringUpper(null)); }
public void ToHexStringUpper() { Assert.AreEqual(HexStringUpper, CryptoBytes.ToHexStringUpper(_bytes)); }
public static string BytesToHexString(byte[] bytes) { return(CryptoBytes.ToHexStringUpper(bytes)); }
public void ToHexStringUpperNull() { CryptoBytes.ToHexStringUpper(null).Should().BeNull(); }
public void ToHexStringUpper() { CryptoBytes.ToHexStringUpper(_bytes).Should().Be(HexStringUpper); }