Last active
November 28, 2018 06:22
-
-
Save nasa9084/e14a96f73d6bf798b446c972d2c165c1 to your computer and use it in GitHub Desktop.
how to symmetrically encrypt/decrypt using x/crypto/openpgp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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