Created
July 19, 2016 13:22
-
-
Save r10r/24d292222e4b3abe928a760dfe0c6755 to your computer and use it in GitHub Desktop.
Test golang OpenPGP message signing (clearsign).
This file contains 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
package repository | |
import ( | |
"bytes" | |
"crypto/rand" | |
"crypto/rsa" | |
"fmt" | |
"github.com/stretchr/testify/assert" | |
"golang.org/x/crypto/openpgp/clearsign" | |
"golang.org/x/crypto/openpgp/packet" | |
"testing" | |
"time" | |
) | |
var key *packet.PrivateKey | |
func init() { | |
// generate RSA privatekey | |
var err error | |
rsaPrivate, err := rsa.GenerateKey(rand.Reader, 1024) | |
if err != nil { | |
panic(err) | |
} | |
key = packet.NewRSAPrivateKey(time.Now(), rsaPrivate) | |
} | |
func TestSignature(t *testing.T) { | |
msg := []byte("hello world\n") | |
// sign message | |
signedMsg := bytes.NewBuffer(nil) | |
dec, err := clearsign.Encode(signedMsg, key, nil) | |
if err != nil { | |
panic(err) | |
} | |
dec.Write(msg) | |
dec.Close() | |
assert.NotEqual(t, msg, signedMsg.String()) | |
fmt.Println(signedMsg.String()) | |
// decode singed message and compare with original | |
blk, rest := clearsign.Decode(signedMsg.Bytes()) | |
assert.Zero(t, len(rest)) | |
assert.Equal(t, blk.Plaintext, msg) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment