Standard library / crypto/rsa

crypto/rsa.VerifyPKCS1v15()

Checks a PKCS#1 v1.5 signature against the supplied digest and hash identifier. A nil error means success. Expected stdout: 'true\n'.

Formal Syntax & Structure

go syntaxgo
func VerifyPKCS1v15(pub *PublicKey, hash crypto.Hash, hashed []byte, sig []byte) error

Parameters & Attributes

NameTypeDescription
pub*PublicKeyPublic key used for the operation.
hashcrypto.HashHash algorithm, hash instance or digest as specified by the signature and description.
hashed[]byteDigest of the message.
sig[]byteSignature bytes to verify.

Return Value

error

Result as described above; check any returned error before use.

Working Example

Example Snippetgo
package main
import (  "crypto"  "crypto/rand"  "crypto/rsa"  "crypto/sha256"  "fmt")
func main() {  k := must(rsa.GenerateKey(rand.Reader, 2048))  h := sha256.Sum256([]byte("Go"))  s := must(rsa.SignPKCS1v15(rand.Reader, k, crypto.SHA256, h[:]))  fmt.Println(rsa.VerifyPKCS1v15(&k.PublicKey, crypto.SHA256, h[:], s) == nil)}func must[T any](v T, e error) T {  if e != nil {    panic(e)  }  return v}func check(e error) {  if e != nil {    panic(e)  }}
Go Reference