diff options
author | Gibheer <gibheer@gmail.com> | 2015-07-12 15:50:29 +0200 |
---|---|---|
committer | Gibheer <gibheer@gmail.com> | 2015-07-12 15:50:29 +0200 |
commit | edfac4e7244d7c73963c40049c124fa6d955a7d1 (patch) | |
tree | 8e5c4a7052e3f7792a2ac6570365726271f6df0d | |
parent | 19136823e1bd2284562ce4e2073fd27bd1230a1b (diff) |
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.
-rw-r--r-- | rsa.go | 14 |
1 files changed, 12 insertions, 2 deletions
@@ -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 } |