2019-05-14 22:53:39 +02:00
|
|
|
# GopenPGP
|
|
|
|
|
|
|
|
|
|
GopenPGP is a high-level OpenPGP library built on top of [a fork of the golang
|
|
|
|
|
crypto library](https://github.com/ProtonMail/crypto).
|
|
|
|
|
|
|
|
|
|
**Table of Contents**
|
|
|
|
|
|
|
|
|
|
<!-- TOC depthFrom:2 -->
|
|
|
|
|
|
|
|
|
|
- [Download/Install](#downloadinstall)
|
|
|
|
|
- [Documentation](#documentation)
|
|
|
|
|
- [Using with Go Mobile](#using-with-go-mobile)
|
|
|
|
|
- [Other notes](#other-notes)
|
|
|
|
|
- [Examples](#examples)
|
|
|
|
|
- [Set up](#set-up)
|
|
|
|
|
- [Encrypt and decrypt](#encrypt-and-decrypt)
|
|
|
|
|
- [Encrypt / Decrypt with password](#encrypt--decrypt-with-password)
|
|
|
|
|
- [Encrypt / Decrypt with PGP keys](#encrypt--decrypt-with-pgp-keys)
|
|
|
|
|
- [Generate key](#generate-key)
|
2019-05-14 19:18:12 +02:00
|
|
|
- [Sign plain text messages](#sign-plain-text-messages)
|
|
|
|
|
- [Detached signatures for binary data](#detached-signatures-for-binary-data)
|
2019-05-14 22:53:39 +02:00
|
|
|
|
|
|
|
|
<!-- /TOC -->
|
2019-03-07 13:48:45 +01:00
|
|
|
|
2019-05-13 13:45:24 +02:00
|
|
|
## Download/Install
|
2019-03-07 13:48:45 +01:00
|
|
|
|
2019-05-14 22:53:39 +02:00
|
|
|
1. Run `go get -u github.com/ProtonMail/gopenpgp`, or manually `git clone` this
|
|
|
|
|
repository into `$GOPATH/src/github.com/ProtonMail/gopenpgp`.
|
|
|
|
|
|
|
|
|
|
2. [Install Glide](https://github.com/Masterminds/glide#install):
|
|
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
curl https://glide.sh/get | sh
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
3. Install dependencies using glide:
|
|
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
cd $GOPATH/src/github.com/ProtonMail/gopenpgp
|
|
|
|
|
glide install
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## Documentation
|
2019-03-07 13:48:45 +01:00
|
|
|
|
2019-05-14 22:53:39 +02:00
|
|
|
https://godoc.org/github.com/ProtonMail/gopenpgp/crypto
|
2019-03-07 13:48:45 +01:00
|
|
|
|
2019-05-13 13:45:24 +02:00
|
|
|
## Using with Go Mobile
|
2018-06-04 16:05:14 -07:00
|
|
|
|
2019-05-13 13:45:24 +02:00
|
|
|
Setup Go Mobile and build/bind the source code:
|
2018-06-04 16:05:14 -07:00
|
|
|
|
2019-05-14 22:53:39 +02:00
|
|
|
Go Mobile repo: https://github.com/golang/mobile
|
2019-05-13 13:45:24 +02:00
|
|
|
Go Mobile wiki: https://github.com/golang/go/wiki/Mobile
|
2018-06-04 16:05:14 -07:00
|
|
|
|
2019-05-13 13:45:24 +02:00
|
|
|
1. Install Go: `brew install go`
|
|
|
|
|
2. Install Gomobile: `go get -u golang.org/x/mobile/cmd/gomobile`
|
|
|
|
|
3. Install Gobind: `go install golang.org/x/mobile/cmd/gobind`
|
|
|
|
|
4. Install Android SDK and NDK using Android Studio
|
|
|
|
|
5. Set env: `export ANDROID_HOME="/AndroidSDK"` (path to your SDK)
|
|
|
|
|
6. Init gomobile: `gomobile init -ndk /AndroidSDK/ndk-bundle/` (path to your NDK)
|
2018-06-04 16:05:14 -07:00
|
|
|
|
2019-05-14 22:53:39 +02:00
|
|
|
7. Build examples:
|
2019-05-13 13:45:24 +02:00
|
|
|
`gomobile build -target=android #or ios`
|
2018-06-04 16:05:14 -07:00
|
|
|
|
2019-05-14 22:53:39 +02:00
|
|
|
Bind examples:
|
|
|
|
|
`gomobile bind -target ios -o frameworks/name.framework`
|
2019-05-13 13:45:24 +02:00
|
|
|
`gomobile bind -target android`
|
2018-06-04 16:05:14 -07:00
|
|
|
|
2019-05-13 13:45:24 +02:00
|
|
|
The bind will create framework for iOS and jar&aar files for Android (x86_64 and ARM).
|
2018-06-04 16:05:14 -07:00
|
|
|
|
2019-05-13 13:45:24 +02:00
|
|
|
## Other notes
|
2018-06-04 16:05:14 -07:00
|
|
|
|
2019-05-14 22:53:39 +02:00
|
|
|
If you wish to use build.sh, you may need to modify the paths in it.
|
2018-06-04 16:05:14 -07:00
|
|
|
|
2019-05-13 13:45:24 +02:00
|
|
|
Interfacing between Go and Swift:
|
|
|
|
|
https://medium.com/@matryer/tutorial-calling-go-code-from-swift-on-ios-and-vice-versa-with-gomobile-7925620c17a4.
|
2018-06-04 16:05:14 -07:00
|
|
|
|
2019-05-14 08:07:56 +00:00
|
|
|
## Examples
|
|
|
|
|
|
|
|
|
|
### Set up
|
|
|
|
|
|
2019-05-14 22:53:39 +02:00
|
|
|
```go
|
|
|
|
|
import "github.com/ProtonMail/gopenpgp/crypto"
|
|
|
|
|
```
|
|
|
|
|
|
2019-05-14 08:07:56 +00:00
|
|
|
### Encrypt and decrypt
|
|
|
|
|
|
2019-05-14 15:39:35 +02:00
|
|
|
Encryption and decryption will use the AES256 algorithm by default.
|
|
|
|
|
|
|
|
|
|
#### Encrypt / Decrypt with password
|
2019-05-14 22:53:39 +02:00
|
|
|
|
|
|
|
|
```go
|
|
|
|
|
var pgp = crypto.GopenPGP{}
|
2019-05-14 15:39:35 +02:00
|
|
|
|
|
|
|
|
const password = "my secret password"
|
|
|
|
|
|
|
|
|
|
// Encrypt data with password
|
2019-05-14 18:05:01 +02:00
|
|
|
armor, err := pgp.EncryptMessageWithPassword("my message", password)
|
2019-05-14 15:39:35 +02:00
|
|
|
|
|
|
|
|
// Decrypt data with password
|
2019-05-14 18:05:01 +02:00
|
|
|
message, err := pgp.DecryptMessageWithPassword(armor, password)
|
2019-05-14 15:39:35 +02:00
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### Encrypt / Decrypt with PGP keys
|
2019-05-14 22:53:39 +02:00
|
|
|
|
|
|
|
|
```go
|
2019-05-14 15:39:35 +02:00
|
|
|
// put keys in backtick (``) to avoid errors caused by spaces or tabs
|
|
|
|
|
const pubkey = `-----BEGIN PGP PUBLIC KEY BLOCK-----
|
|
|
|
|
...
|
|
|
|
|
-----END PGP PUBLIC KEY BLOCK-----`
|
|
|
|
|
|
|
|
|
|
const privkey = `-----BEGIN PGP PRIVATE KEY BLOCK-----
|
|
|
|
|
...
|
|
|
|
|
-----END PGP PRIVATE KEY BLOCK-----` // encrypted private key
|
|
|
|
|
|
|
|
|
|
const passphrase = `the passphrase of the private key` // what the privKey is encrypted with
|
|
|
|
|
|
|
|
|
|
publicKeyRing, err := crypto.ReadArmoredKeyRing(strings.NewReader(pubkey))
|
|
|
|
|
|
2019-05-15 14:56:49 +02:00
|
|
|
privateKeyRing, err := crypto.ReadArmoredKeyRing(strings.NewReader(privkey))
|
2019-05-14 15:39:35 +02:00
|
|
|
privateKeyRing.Unlock([]byte(passphrase)) // if private key is locked with passphrase
|
|
|
|
|
|
2019-05-15 14:56:49 +02:00
|
|
|
// encrypt message using public key, can be optionally signed using private key
|
|
|
|
|
armor, err := publicKeyRing.EncryptMessage("plain text", privateKeyRing)
|
|
|
|
|
|
|
|
|
|
// decrypt armored encrypted message using the private key
|
|
|
|
|
signedText, err := privateKeyRing.DecryptMessage(armor)
|
2019-05-14 15:39:35 +02:00
|
|
|
plainText = signedText.String
|
|
|
|
|
|
2019-05-15 14:56:49 +02:00
|
|
|
// verify signature (optional)
|
|
|
|
|
signed = signedText.Signed.IsBy(publicKeyRing)
|
2019-05-14 15:39:35 +02:00
|
|
|
```
|
|
|
|
|
|
2019-05-14 08:07:56 +00:00
|
|
|
### Generate key
|
2019-05-14 22:53:39 +02:00
|
|
|
|
|
|
|
|
Keys are generated with the `GenerateKey` function, that returns the armored key as a string and a potential error.
|
2019-05-14 10:54:27 +02:00
|
|
|
The library supports RSA with different key lengths or Curve25519 keys.
|
2019-05-14 22:53:39 +02:00
|
|
|
|
|
|
|
|
```go
|
|
|
|
|
var pgp = crypto.GopenPGP{}
|
2019-05-14 10:54:27 +02:00
|
|
|
|
2019-05-15 14:56:49 +02:00
|
|
|
const (
|
2019-05-14 10:54:27 +02:00
|
|
|
localPart = "name.surname"
|
|
|
|
|
domain = "example.com"
|
|
|
|
|
passphrase = "LongSecret"
|
|
|
|
|
rsaBits = 2048
|
|
|
|
|
ecBits = 256
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// RSA
|
2019-05-14 18:05:01 +02:00
|
|
|
rsaKey, err := pgp.GenerateKey(localPart, domain, passphrase, "rsa", rsaBits)
|
2019-05-14 10:54:27 +02:00
|
|
|
|
2019-05-14 22:53:39 +02:00
|
|
|
// Curve25519
|
2019-05-14 18:05:01 +02:00
|
|
|
ecKey, err := pgp.GenerateKey(localPart, domain, passphrase, "x25519", ecBits)
|
2019-05-14 10:54:27 +02:00
|
|
|
```
|
2019-05-14 08:07:56 +00:00
|
|
|
|
2019-05-14 19:18:12 +02:00
|
|
|
### Sign plain text messages
|
2019-05-14 08:07:56 +00:00
|
|
|
|
2019-05-14 19:18:12 +02:00
|
|
|
To sign plain text data either an unlocked private keyring or a passphrase must be provided.
|
|
|
|
|
The output is an armored signature.
|
|
|
|
|
|
|
|
|
|
```go
|
|
|
|
|
const privkey = `-----BEGIN PGP PRIVATE KEY BLOCK-----
|
|
|
|
|
...
|
|
|
|
|
-----END PGP PRIVATE KEY BLOCK-----` // encrypted private key
|
|
|
|
|
passphrase = "LongSecret"
|
|
|
|
|
const trimNewlines = false
|
|
|
|
|
|
2019-05-15 14:56:49 +02:00
|
|
|
signingKeyRing, err := crypto.ReadArmoredKeyRing(strings.NewReader(privkey))
|
2019-05-14 19:18:12 +02:00
|
|
|
|
2019-05-15 13:48:47 +02:00
|
|
|
signature, err := signingKeyRing.SignTextDetached(plaintext, passphrase, trimNewlines)
|
|
|
|
|
// passphrase is optional if the key is already unlocked
|
2019-05-14 19:18:12 +02:00
|
|
|
```
|
|
|
|
|
|
|
|
|
|
To verify a signature either private or public keyring can be provided.
|
|
|
|
|
The newlines in the text are never trimmed in the verification process.
|
|
|
|
|
The function outputs a bool, if the verification fails `verified` will be false, and the error will be not `nil`.
|
|
|
|
|
|
|
|
|
|
```go
|
|
|
|
|
const pubkey = `-----BEGIN PGP PUBLIC KEY BLOCK-----
|
|
|
|
|
...
|
|
|
|
|
-----END PGP PUBLIC KEY BLOCK-----`
|
|
|
|
|
|
|
|
|
|
const signature = `-----BEGIN PGP SIGNATURE-----
|
|
|
|
|
...
|
|
|
|
|
-----END PGP SIGNATURE-----`
|
|
|
|
|
|
|
|
|
|
const verifyTime = 0
|
2019-05-15 13:48:47 +02:00
|
|
|
const trimNewlines = false
|
2019-05-14 19:18:12 +02:00
|
|
|
|
2019-05-15 14:56:49 +02:00
|
|
|
signingKeyRing, err := crypto.ReadArmoredKeyRing(strings.NewReader(pubkey))
|
2019-05-14 19:18:12 +02:00
|
|
|
|
2019-05-15 13:48:47 +02:00
|
|
|
verified, err := signingKeyRing.VerifyTextDetachedSig(signature, signedPlainText, verifyTime, trimNewlines)
|
2019-05-14 19:18:12 +02:00
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### Detached signatures for binary data
|
|
|
|
|
|
|
|
|
|
To sign binary data either an unlocked private keyring or a passphrase must be provided.
|
|
|
|
|
The output is an armored signature.
|
|
|
|
|
|
|
|
|
|
```go
|
|
|
|
|
const privkey = `-----BEGIN PGP PRIVATE KEY BLOCK-----
|
|
|
|
|
...
|
|
|
|
|
-----END PGP PRIVATE KEY BLOCK-----` // encrypted private key
|
|
|
|
|
passphrase = "LongSecret"
|
|
|
|
|
|
2019-05-15 14:56:49 +02:00
|
|
|
signingKeyRing, err := crypto.ReadArmoredKeyRing(strings.NewReader(privkey))
|
2019-05-14 19:18:12 +02:00
|
|
|
|
2019-05-15 13:48:47 +02:00
|
|
|
signature, err := signingKeyRing.SignBinDetached(data, passphrase)
|
|
|
|
|
// passphrase is optional if the key is already unlocked
|
2019-05-14 19:18:12 +02:00
|
|
|
```
|
|
|
|
|
|
|
|
|
|
To verify a signature either private or public keyring can be provided.
|
|
|
|
|
The newlines in the text are never trimmed in the verification process.
|
|
|
|
|
The function outputs a bool, if the verification fails `verified` will be false, and the error will be not `nil`.
|
|
|
|
|
|
|
|
|
|
```go
|
|
|
|
|
const pubkey = `-----BEGIN PGP PUBLIC KEY BLOCK-----
|
|
|
|
|
...
|
|
|
|
|
-----END PGP PUBLIC KEY BLOCK-----`
|
|
|
|
|
|
|
|
|
|
const signature = `-----BEGIN PGP SIGNATURE-----
|
|
|
|
|
...
|
|
|
|
|
-----END PGP SIGNATURE-----`
|
|
|
|
|
|
|
|
|
|
const verifyTime = 0
|
|
|
|
|
|
2019-05-15 14:56:49 +02:00
|
|
|
signingKeyRing, err := crypto.ReadArmoredKeyRing(strings.NewReader(pubkey))
|
2019-05-14 19:18:12 +02:00
|
|
|
|
2019-05-15 13:48:47 +02:00
|
|
|
verified, err := signingKeyRing.VerifyBinDetachedSig(signature, data, verifyTime)
|
2019-05-14 19:18:12 +02:00
|
|
|
```
|