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); }
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); }