public static EcKey CreateEcKeyFromHexString(string privateKey) { var pkey = new BigInteger(privateKey, 16); var key = new EcKey(pkey); return(key); }
/// <summary> /// Signs the input string with the provided key /// </summary> /// <param name="ecKey">The key object to sign with</param> /// <param name="input">The string to be signed</param> /// <returns>The signature</returns> public static string Sign(EcKey ecKey, string input) { // return ecKey.Sign(input); var hash = Sha256Hash(input); var hashBytes = HexToBytes(hash); var signature = ecKey.Sign(hashBytes); var bytesHex = BytesToHex(signature); return(bytesHex); }
public static async Task <EcKey> LoadEcKey() { using (var fs = File.OpenRead(PrivateKeyFile)) { var b = new byte[1024]; await fs.ReadAsync(b, 0, b.Length); var key = EcKey.FromAsn1(b); return(key); } }
/// <summary> /// Initialize the public/private key pair by either loading the existing one or by creating a new one /// </summary> /// <returns></returns> private async Task InitKeys() { if (KeyUtils.PrivateKeyExists(_configuration.GetSection("BitPayConfiguration:EnvConfig:" + _env + ":PrivateKeyPath").Value)) { _ecKey = await KeyUtils.LoadEcKey(); } else { _ecKey = KeyUtils.CreateEcKey(); await KeyUtils.SaveEcKey(_ecKey); } }
public static async Task SaveEcKey(EcKey ecKey) { var bytes = ecKey.ToAsn1(); if (!string.IsNullOrEmpty(Path.GetDirectoryName(PrivateKeyFile)) && !Directory.Exists(Path.GetDirectoryName(PrivateKeyFile))) { Directory.CreateDirectory(Path.GetDirectoryName(PrivateKeyFile)); } using (var fs = new FileStream(PrivateKeyFile, FileMode.Create, FileAccess.Write)) { await fs.WriteAsync(bytes, 0, bytes.Length); } }
public static string DeriveSin(EcKey ecKey) { if (_derivedSin != null) { return(_derivedSin); } // Get sha256 hash and then the RIPEMD-160 hash of the public key (this call gets the result in one step). var pubKey = ecKey.PublicKey; var hash = new SHA256Managed().ComputeHash(pubKey); var ripeMd160Digest = new RipeMD160Digest(); ripeMd160Digest.BlockUpdate(hash, 0, hash.Length); var output = new byte[20]; ripeMd160Digest.DoFinal(output, 0); var pubKeyHash = output; // Convert binary pubKeyHash, SINtype and version to Hex var version = "0F"; var siNtype = "02"; var pubKeyHashHex = BytesToHex(pubKeyHash); // Concatenate all three elements var preSin = version + siNtype + pubKeyHashHex; // Convert the hex string back to binary and double sha256 hash it leaving in binary both times var preSiNbyte = HexToBytes(preSin); var hash2Bytes = DoubleDigest(preSiNbyte); // Convert back to hex and take first four bytes var hashString = BytesToHex(hash2Bytes); var first4Bytes = hashString.Substring(0, 8); // Append first four bytes to fully appended SIN string var unencoded = preSin + first4Bytes; var unencodedBytes = new BigInteger(unencoded, 16).ToByteArray(); var encoded = Encode(unencodedBytes); _derivedSin = encoded; return(encoded); }