public static bool VerifyHash(string publicKey, byte[] data, string signature) { using (var rsa = new RSAOpenSsl()) { rsa.ImportParameters(GetParameters(publicKey)); return(rsa.VerifyData(data, Convert.FromBase64String(signature), HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1)); } }
public static void VerifyDuplicateKey_ValidHandle() { byte[] data = ByteUtils.RepeatByte(0x71, 11); using (RSAOpenSsl first = new RSAOpenSsl()) using (SafeEvpPKeyHandle firstHandle = first.DuplicateKeyHandle()) { using (RSA second = new RSAOpenSsl(firstHandle)) { byte[] signed = second.SignData(data, HashAlgorithmName.SHA512, RSASignaturePadding.Pkcs1); Assert.True(first.VerifyData(data, signed, HashAlgorithmName.SHA512, RSASignaturePadding.Pkcs1)); } } }
public static void VerifyDuplicateKey_RefCounts() { byte[] data = ByteUtils.RepeatByte(0x74, 11); byte[] signature; RSA second; using (RSAOpenSsl first = new RSAOpenSsl()) using (SafeEvpPKeyHandle firstHandle = first.DuplicateKeyHandle()) { signature = first.SignData(data, HashAlgorithmName.SHA384, RSASignaturePadding.Pkcs1); second = new RSAOpenSsl(firstHandle); } // Now show that second still works, despite first and firstHandle being Disposed. using (second) { Assert.True(second.VerifyData(data, signature, HashAlgorithmName.SHA384, RSASignaturePadding.Pkcs1)); } }
public bool VerifyWithRsa( JwsAlg algorithm, string serializedKeys, string input, byte[] signature) { if (!_supportedAlgs.Contains(algorithm)) { return(false); } if (string.IsNullOrWhiteSpace(serializedKeys)) { throw new ArgumentNullException("serializedKeys"); } var plainBytes = ASCIIEncoding.ASCII.GetBytes(input); if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { using (var rsa = new RSACryptoServiceProvider()) { var hashMethod = _mappingWinJwsAlgorithmToRsaHashingAlgorithms[algorithm]; rsa.FromXmlStringNetCore(serializedKeys); return(rsa.VerifyData(plainBytes, hashMethod, signature)); } } else { using (var rsa = new RSAOpenSsl()) { var hashMethod = _mappingLinuxJwsAlgorithmToRsaHashingAlgorithms[algorithm]; rsa.FromXmlStringNetCore(serializedKeys); return(rsa.VerifyData(plainBytes, signature, hashMethod, RSASignaturePadding.Pkcs1)); } } }
public bool VerifyWithRsa( JwsAlg algorithm, string serializedKeys, string input, byte[] signature) { if (!_mappingJwsAlgorithmToRsaHashingAlgorithms.ContainsKey(algorithm)) { return(false); } if (string.IsNullOrWhiteSpace(serializedKeys)) { throw new ArgumentNullException("serializedKeys"); } var plainBytes = ASCIIEncoding.ASCII.GetBytes(input); var hashMethod = _mappingJwsAlgorithmToRsaHashingAlgorithms[algorithm]; #if UAP // TODO : Implement return(false); #elif NET46 || NET45 using (var rsa = new RSACryptoServiceProvider()) { rsa.FromXmlString(serializedKeys); return(rsa.VerifyData(plainBytes, hashMethod, signature)); } #elif NETSTANDARD using (var rsa = new RSAOpenSsl()) { rsa.FromXmlString(serializedKeys); return(rsa.VerifyData(plainBytes, signature, hashMethod, RSASignaturePadding.Pkcs1)); } #endif }