From edfac4e7244d7c73963c40049c124fa6d955a7d1 Mon Sep 17 00:00:00 2001 From: Gibheer Date: Sun, 12 Jul 2015 15:50:29 +0200 Subject: add support for signing for rsa This adds finally support to sign and verify messages using an RSA private and public key. The method used is PKCS1v15, as it was the easiest to implement first. There is also PSS available in go, so that could be implemented later. --- rsa.go | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) (limited to 'rsa.go') diff --git a/rsa.go b/rsa.go index 6622887..76296ed 100644 --- a/rsa.go +++ b/rsa.go @@ -47,7 +47,12 @@ func (pr *RsaPrivateKey) Public() PublicKey { } func (pr RsaPrivateKey) Sign(message []byte, hash crypto.Hash) ([]byte, error) { - return make([]byte, 0), errors.New("not implemented yet!") + if !hash.Available() { + return make([]byte, 0), errors.New("Hash method is not available!") + } + hashed_message := hash.New() + hashed_message.Write(message) + return rsa.SignPKCS1v15(rand.Reader, pr.private_key, hash, hashed_message.Sum(nil)) } // get the private key @@ -84,5 +89,10 @@ func (pu *RsaPublicKey) MarshalPem() (io.WriterTo, error) { // verify a message with a signature using the public key func (pu *RsaPublicKey) Verify(message []byte, signature []byte, hash crypto.Hash) (bool, error) { - return false, errors.New("not implemented yet!") + hashed_message := hash.New() + hashed_message.Write(message) + if err := rsa.VerifyPKCS1v15(pu.public_key, hash, hashed_message.Sum(nil), signature); err != nil { + return false, err + } + return true, nil } -- cgit v1.2.3-70-g09d2