public bool ECDHTest_Signature(string svcProvider) { try { using (ECDsaCryptoServiceProvider csp = new ECDsaCryptoServiceProvider(svcProvider)) { byte[] dataToSign = System.Text.UTF8Encoding.UTF8.GetBytes("This is a string to sign"); byte[] sig = csp.SignData(dataToSign); return csp.VerifyData(dataToSign, sig); } } catch { return false; } }
private static bool TestSignDataStream(Session session) { bool passed = true; byte[] data = new byte[10000]; for (int i = 0; i < data.Length; i++) data[i] = (byte)(i % 256); MechanismType[] hashAlgorithms = new MechanismType[] { //MechanismType.MD5, currently not supported in OpenSSL for DSA MechanismType.SHA_1, MechanismType.SHA256, MechanismType.SHA384, MechanismType.SHA512 }; int[] keySizes = new int[] { 256, 384, 521 }; foreach (int keySize in keySizes) { WriteLine("Using key size " + keySize); foreach (MechanismType hashAlgorithm in hashAlgorithms) { WriteLine("Using hash algorithm " + hashAlgorithm.ToString()); using (ECDsaCryptoServiceProvider sign = new ECDsaCryptoServiceProvider(session)) { sign.KeySize = keySize; using (ECDsaCryptoServiceProvider verify1 = new ECDsaCryptoServiceProvider(sign.KeyPair)) using (ECDsaCryptoServiceProvider verify2 = new ECDsaCryptoServiceProvider(sign.KeyPair)) //using (MemoryStream ms = new MemoryStream(data)) { sign.HashAlgorithm = hashAlgorithm; verify1.HashAlgorithm = hashAlgorithm; verify2.HashAlgorithm = hashAlgorithm; byte[] signature = sign.SignData(data); //ms); //ms.Position = 0; bool verified = verify1.VerifyData(data, signature); if (!verified) WriteLine("Did not verify via stream"); passed &= verified; verified = verify2.VerifyData(data, signature); if (!verified) WriteLine("Did not verify via array"); passed &= verified; } } } } return passed; }
private static bool TestSignDataBuffer(Session session) { bool passed = true; byte[] data = new byte[10000]; for (int i = 0; i < data.Length; i++) data[i] = (byte)(i % 256); MechanismType[] hashAlgorithms = new MechanismType[] { // MechanismType.MD5, - currently not supported in OpenSSL for ECDSA MechanismType.SHA_1, MechanismType.SHA256, MechanismType.SHA384, MechanismType.SHA512, }; int[] keySizes = new int[] { 256, 384, 521 }; foreach (int keySize in keySizes) { WriteLine("Using key size " + keySize); foreach (MechanismType hashAlgorithm in hashAlgorithms) { WriteLine("Using hash algorithm " + hashAlgorithm.ToString()); using (ECDsaCryptoServiceProvider sign = new ECDsaCryptoServiceProvider(session, keySize)) using (ECDsaCryptoServiceProvider verify = new ECDsaCryptoServiceProvider(sign.KeyPair)) { sign.HashAlgorithm = hashAlgorithm; verify.HashAlgorithm = hashAlgorithm; try { byte[] signature = sign.SignData(data); bool verified = verify.VerifyData(data, signature); if (!verified) WriteLine("Did not verify"); passed &= verified; } catch { passed = false; } } } } return passed; }
private static bool TestSignHash(Session session) { bool passed = true; byte[] hashValue = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19 }; using (ECDsaCryptoServiceProvider dsa = new ECDsaCryptoServiceProvider(session)) { byte[] signature = dsa.SignHash(hashValue, MechanismType.SHA_1); passed &= dsa.VerifyHash(hashValue, MechanismType.SHA_1, signature); } return passed; }
private static bool TestCreate(Session session) { bool passed = true; using (ECDsaCryptoServiceProvider dsa = new ECDsaCryptoServiceProvider(session)) passed &= dsa != null && dsa is ECDsaCryptoServiceProvider; return passed; }