Skip to content

Instantly share code, notes, and snippets.

@nasa9084
Last active November 28, 2018 06:22
Show Gist options
  • Select an option

  • Save nasa9084/e14a96f73d6bf798b446c972d2c165c1 to your computer and use it in GitHub Desktop.

Select an option

Save nasa9084/e14a96f73d6bf798b446c972d2c165c1 to your computer and use it in GitHub Desktop.
how to symmetrically encrypt/decrypt using x/crypto/openpgp
func promptFn(passphrase []byte) func([]openpgp.Key, bool) ([]byte, error) {
var alreadyCalled bool
return func([]openpgp.Key, bool) ([]byte, error) {
if alreadyCalled {
return nil, errors.New("the passphrase is incorrect")
}
alreadyCalled = true
return passphrase, nil
}
}
func decrypt(in io.Reader, out io.Writer, passphrase []byte) error {
md, err := openpgp.ReadMessage(in, nil, promptFn(passphrase), nil)
if err != nil {
return trace.Wrap(err)
}
_, err = io.Copy(out, md.UnverifiedBody)
if err != nil {
return trace.Wrap(err)
}
return nil
}
func encrypt(in io.Reader, out io.Writer, passphrase []byte) error {
w, err := openpgp.SymmetricallyEncrypt(out, []byte(passphrase), nil, nil)
if err != nil {
return trace.Wrap(err)
}
defer w.Close()
_, err = io.Copy(w, in)
if err != nil {
return trace.Wrap(err)
}
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment