Skip to content

Instantly share code, notes, and snippets.

@kennwhite
Last active January 20, 2023 18:22
Show Gist options
  • Save kennwhite/0bf93ba7912ac08ae812 to your computer and use it in GitHub Desktop.
Save kennwhite/0bf93ba7912ac08ae812 to your computer and use it in GitHub Desktop.
Simple file encryption with Go using pgp (AES128 + SHA256)
package main
import (
"golang.org/x/crypto/openpgp"
"io/ioutil"
"os"
)
func main() {
/*
Equivalent to:
gpg -c --symmetric --cipher-algo AES --s2k-digest-algo SHA256 -o test.txt.gpg test.txt
# Go 1.4.1/1.5.1 openpgp.SymetricallyEncrypt() docs: "sensible defaults will be used"
*/
password := []byte("password")
plaintext, _ := ioutil.ReadFile("test.txt")
dst, _ := os.Create("test.txt.gpg")
encryptor, _ := openpgp.SymmetricallyEncrypt(dst, password, nil, nil)
encryptor.Write(plaintext)
encryptor.Close()
dst.Close()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment