Skip to content

Instantly share code, notes, and snippets.

@developer-guy
Forked from Grrrben/rsa_sign.go
Created April 15, 2021 20:00
Show Gist options
  • Save developer-guy/541f8bdd5bcaa8aec76cee9c1e90ff0d to your computer and use it in GitHub Desktop.
Save developer-guy/541f8bdd5bcaa8aec76cee9c1e90ff0d to your computer and use it in GitHub Desktop.
Signing and Verifying using Golang RSA
// Sign returns a signature made by combining the message and the signers private key
// With the r.Verify function, the signature can be checked.
func (r *RsaIdentity) Sign(msg []byte) (signature []byte, err error) {
hs := r.getHashSum(msg)
signature, err := rsa.SignPKCS1v15(rand.Reader, r.private, crypto.SHA256, hs)
return
}
// Verify checks if a message is signed by a given Public Key
func (r *RsaIdentity) Verify(msg []byte, sig []byte, pk *rsa.PublicKey) error {
hs := r.getHashSum(msg)
return rsa.VerifyPKCS1v15(pk, crypto.SHA256, hs, sig)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment