Standard library / crypto/rsa
crypto/rsa.OAEPOptions
Hash sets OAEP hash, MGFHash optionally overrides MGF1 hash, Label must match encryption. Used with PrivateKey.Decrypt. Expected stdout: 'true\n'.
Formal Syntax & Structure
go syntaxgo
type OAEPOptions struct { Hash crypto.Hash
MGFHash crypto.Hash
Label []byte}Working Example
Example Snippetgo
package main
import ( "crypto" "crypto/rand" "crypto/rsa" "crypto/sha256" "fmt")
func main() { k := must(rsa.GenerateKey(rand.Reader, 2048)) ct := must(rsa.EncryptOAEP(sha256.New(), rand.Reader, &k.PublicKey, []byte("Go"), []byte("demo"))) p := must(k.Decrypt(nil, ct, &rsa.OAEPOptions{Hash: crypto.SHA256, Label: []byte("demo")})) fmt.Println(string(p) == "Go")}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) }}