public byte[] Sign(byte[] data, ISignatureKey signatureKey) { if (signatureKey is PrivateKey) { var privateKey = signatureKey as PrivateKey; var hash = this.Hash(data); if (new BigInteger(hash) > privateKey.N) { throw new Exception("Hash function is not suitable for signature."); } BigInteger c = new BigInteger(hash); var sign = c.modPow(privateKey.D, privateKey.N); return(sign.getBytes()); } else { throw new Exception("SignatureKey parameter format was not RsaPrivateKey!"); } }
public bool Verify(byte[] data, byte[] sign, ISignatureKey verificationKey) { if (verificationKey is PublicKey) { var publicKey = verificationKey as PublicKey; var hash = this.Hash(data); if (new BigInteger(hash) > publicKey.N) { throw new Exception("Hash function is not suitable for signature validation."); } BigInteger m = new BigInteger(sign); var c = m.modPow(publicKey.E, publicKey.N); var decryptedHash = c.getBytes(); return(ByteArrayCompare(decryptedHash, hash)); } else { throw new Exception("VerificationKey parameter format was not RsaPublicKey!"); } }
/// <summary> /// Constructor /// </summary> /// <param name="signKey"></param> public RsaSigner(ISignatureKey signKey, ISignatureKey verifyKey) { this.SignatureKey = signKey; this.VerificationKey = verifyKey; }