public static void TestNegativeVerify384() { CngKey key = TestData.s_ECDsa384Key; ECDsaCng e = new ECDsaCng(key); byte[] tamperedSig = ("7805c494b17bba8cba09d3e5cdd16d69ce785e56c4f2d9d9061d549fce0a6860cca1cb9326bd534da21ad4ff326a1e0810d8" + "f366eb6afc66ede0d1ffe345f6b37ac622ed77838b42825ceb96cd3996d3d77fd6a248357ae1ae6cb85f048b1b04").HexToByteArray(); bool verified = e.VerifyHash(TestData.s_hashSha512, tamperedSig); Assert.False(verified); }
public static bool VerifyData( this ECDsaCng ecdsaCng, byte[] data, int offset, int count, byte[] signature) { if (data == null) { throw new ArgumentNullException("data"); } if (offset < 0 || offset > data.Length) { throw new ArgumentOutOfRangeException("offset"); } if (count < 0 || count > data.Length - offset) { throw new ArgumentOutOfRangeException("count"); } if (signature == null) { throw new ArgumentNullException("signature"); } using (BCryptHashAlgorithm hashAlgorithm = new BCryptHashAlgorithm(CngAlgorithm.Sha256, BCryptNative.ProviderName.MicrosoftPrimitiveProvider)) { hashAlgorithm.HashCore(data, offset, count); byte[] hashValue = hashAlgorithm.HashFinal(); return(ecdsaCng.VerifyHash(hashValue, signature)); } }
public static void TestNegativeVerify256() { CngKey key = TestData.s_ECDsa256Key; ECDsaCng e = new ECDsaCng(key); byte[] tamperedSig = ("998791331eb2e1f4259297f5d9cb82fa20dec98e1cb0900e6b8f014a406c3d02cbdbf5238bde471c3155fc25565524301429" + "e8713dad9a67eb0a5c355e9e23dc").HexToByteArray(); bool verified = e.VerifyHash(TestData.s_hashSha512, tamperedSig); Assert.False(verified); }
public static void TestPositiveVerify521() { CngKey key = TestData.s_ECDsa521Key; ECDsaCng e = new ECDsaCng(key); byte[] sig = ("0084461450745672df85735fbf89f2dccef804d6b56e86ca45ea5c366a05a5de96327eddb75582821c6315c8bb823c875845" + "b6f25963ddab70461b786261507f971401fdc300697824129e0a84e0ba1ab4820ac7b29e7f8248bc2e29d152a9190eb3fcb7" + "6e8ebf1aa5dd28ffd582a24cbfebb3426a5f933ce1d995b31c951103d24f6256").HexToByteArray(); bool verified = e.VerifyHash(TestData.s_hashSha512, sig); Assert.True(verified); }
private static bool VerifySignature(byte[] hash, byte[] signature, byte[] pubkey) { const int ECDSA_PUBLIC_P256_MAGIC = 0x31534345; try { pubkey = ECPoint.DecodePoint(pubkey, ECCurve.Secp256r1).EncodePoint(false).Skip(1).ToArray(); } catch { return false; } pubkey = BitConverter.GetBytes(ECDSA_PUBLIC_P256_MAGIC).Concat(BitConverter.GetBytes(32)).Concat(pubkey).ToArray(); using (CngKey key = CngKey.Import(pubkey, CngKeyBlobFormat.EccPublicBlob)) using (ECDsaCng ecdsa = new ECDsaCng(key)) { return ecdsa.VerifyHash(hash, signature); } }
/// <inheritdoc /> protected internal override bool VerifyHash(byte[] data, byte[] signature) { using (var cng = new ECDsaCng(this.key)) { return cng.VerifyHash(data, signature); } }
public static bool? VerifySignatureCng(byte[] pkParameters, byte[] pkKey, byte[] hash, byte[] signature) { #if !__MonoCS__ EllipticCurve curve = null; var ecsngHeader = new byte[8] { (byte)'E', (byte)'C', (byte)'S', 0, 0, 0, 0, 0 }; curve = GetCurveFromParameters(pkParameters); if (curve == null) return null; if (curve == EllipticCurve.P256) ecsngHeader[3] = (byte)'1'; else if (curve == EllipticCurve.P384) ecsngHeader[3] = (byte)'3'; else // P512 ecsngHeader[3] = (byte)'5'; ecsngHeader[4] = (byte)curve.curveByteLen; var pubKeyBlob = new byte[8 + curve.curveByteLen * 2]; Buffer.BlockCopy(ecsngHeader, 0, pubKeyBlob, 0, 8); Buffer.BlockCopy(pkKey, 1, pubKeyBlob, 8, curve.curveByteLen * 2); var ecdsa = new ECDsaCng(CngKey.Import(pubKeyBlob, CngKeyBlobFormat.EccPublicBlob)); // We must decode the signature from DER format to raw format (to coordinates on the curve) var decodedSignature = Utils.DecodeDERSignature(signature, 0, signature.Length, curve.curveByteLen); bool ok = ecdsa.VerifyHash(hash, decodedSignature); return ok; #else throw new NotSupportedException(); #endif }
/// <summary> /// Is the signed hash belong to the hash and public key /// </summary> /// <param name="hash">transfer info hash</param> /// <param name="publicKey">public key</param> /// <returns>true if the signed hash belong to the hash and public key</returns> public bool IsValidSignedHash(byte[] hash, byte[] publicKey) { bool ret; using (var dsa = new ECDsaCng(CngKey.Import(publicKey, CngKeyBlobFormat.EccPublicBlob))) { dsa.HashAlgorithm = Global.HashAlgorithm; //// verifying hashed message ////bReturn = dsa.VerifyHash(dataHash, SignedMsg); ret = dsa.VerifyHash(hash, this.sgndData); } return ret; }
private bool ValidateHash(byte[] hash, byte[] signature) { const string publicKey = "RUNTNUIAAAABLcbMBFKtpFlBY0j6TnD5DTeDjy2UoTt3ik2yhY9RGB8AdkZ44DTLG3L3e8S7g+bRmhwygtlsGUFEUMGiJ2SBeGAABS5nBJRfQhAA+vmishz0EWYXD5FUYClQaRHlrH9clkB9pDakzPSvPbGnpMuHgCWa6LniAb1zExIPbYv9zHlfqOA="; using (var key = CngKey.Import(Convert.FromBase64String(publicKey), CngKeyBlobFormat.EccPublicBlob)) { using (var ecdsa = new ECDsaCng(key)) { return ecdsa.VerifyHash(hash, signature); } } }
/// <summary> /// Проверка ЭЦП /// </summary> /// <param name="data">Массив данных.</param> /// <param name="signature">ЭЦП.</param> /// <param name="publicKey">Открытый ключ для проверки ЭЦП.</param> /// <returns>Булевский флаг проверки ЭЦП.</returns> private bool VerifyHash(byte[] data, byte[] signature, byte[] publicKey) { if(!IsInitialized) { throw new Exception("EcdhP521::VerifyHash() ==> EcdhP521 is not initialized!"); } using(var eECDsaCng = new ECDsaCng(ImportKeyBinData(publicKey, true, true))) // public, DS { eECDsaCng.HashAlgorithm = CngAlgorithm.Sha512; return eECDsaCng.VerifyHash(data, signature); } }
internal bool Verify(byte[] hash, byte[] signature) { using (ECDsaCng sig = new ECDsaCng(_cngKey)) { sig.HashAlgorithm = CngAlgorithm.ECDsaP256; return sig.VerifyHash(hash, signature); } }
public static bool DeCDsa(this string data, CngKey key, string originalData) { ECDsaCng ecdsa = new ECDsaCng(key); SHA1 sha1 = System.Security.Cryptography.SHA1.Create(); return ecdsa.VerifyHash(sha1.ComputeHash(Convert.FromBase64String(originalData)), Convert.FromBase64String(data)); }