public Object verify(String mode, jwkPublicKey publicKey, byte[] plainBytes, string hashAlgorithm, string curveName, byte[] signatureBytes) { bool verified = false; if (mode == "ecdsa") { EllipticCurveFp curve = selectCamelotCurve(curveName); ecPublicKey ecPublicKey = new ecPublicKey(publicKey); EllipticCurvePointFp point = new EllipticCurvePointFp(curve, false, ecPublicKey.X, ecPublicKey.Y); camelot.ECKeyPair ecKeyPair = new camelot.ECKeyPair(curve, SEC1EncodingFp.EncodePoint(point)); camelot.HashAlgorithm h = selectCamelotHashAlgorithm(hashAlgorithm); byte[] digest = h.ComputeHash(plainBytes); verified = verifyCamelotEcdsa(ecKeyPair, digest, signatureBytes); } else { rsaPublicKey rsaKey = new rsaPublicKey(publicKey); RSACryptoServiceProvider rsaCsp = new RSACryptoServiceProvider(); RSAParameters rsaParams = rsaKey.toRSAParameters(); rsaCsp.ImportParameters(rsaParams); if (mode == "rsa-pss") { verified = verifyCamelotPSS(rsaParams, hashAlgorithm, plainBytes, signatureBytes); } else if (mode == "rsassa-pkcs1-v1_5") { verified = rsaCsp.VerifyData(plainBytes, selectCSPHashAlgorithm(hashAlgorithm), signatureBytes); } else { throw new InvalidOperationException("Unsupported mode"); } } return(verified); }
public jwkPublicKey(camelot.ECKeyPair ecKeyPair, string curveName) { EllipticCurvePointFp point = SEC1EncodingFp.DecodePoint(ecKeyPair.ExportPublicKey(), ecKeyPair.Curve); this.crv = curveName; this.kty = "EC"; this.extractable = true; byte[] xBytes = point.X.ToByteArrayUnsigned(); Array.Reverse(xBytes); byte[] yBytes = point.Y.ToByteArrayUnsigned(); Array.Reverse(yBytes); this.x = Base64Url.to(xBytes); this.y = Base64Url.to(yBytes); }