Skip to content

Instantly share code, notes, and snippets.

@Grrrben
Last active September 26, 2023 14:01
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Grrrben/96f833648bd8371408dfa3f6641bb7da to your computer and use it in GitHub Desktop.
Save Grrrben/96f833648bd8371408dfa3f6641bb7da 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