Standard library / crypto

crypto.Decrypter

Interface for opaque private keys that decrypt. Public returns the corresponding public key; Decrypt accepts algorithm-specific options. This example uses ephemeral RSA OAEP with SHA-256 for a small message. Use authenticated hybrid encryption for bulk data. Expected stdout: 'demo true\n'.

Formal Syntax & Structure

go syntaxgo
type Decrypter interface {  Public() PublicKey
  Decrypt(rand io.Reader, msg []byte, opts DecrypterOpts) (plaintext []byte, err error)}

Working Example

Example Snippetgo
package main
import (  "crypto"  "crypto/rand"  "crypto/rsa"  "crypto/sha256"  "fmt")
func main() {  key, e := rsa.GenerateKey(rand.Reader, 2048)  if e != nil {    panic(e)  }  var d crypto.Decrypter = key  c, e := rsa.EncryptOAEP(sha256.New(), rand.Reader, &key.PublicKey, []byte("demo"), nil)  if e != nil {    panic(e)  }  m, e := d.Decrypt(rand.Reader, c, &rsa.OAEPOptions{Hash: crypto.SHA256})  if e != nil {    panic(e)  }  fmt.Println(string(m), d.Public().(*rsa.PublicKey).Equal(&key.PublicKey))}
Go Reference