-
Notifications
You must be signed in to change notification settings - Fork 150
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
create pgptest package and fix broken tests
- Loading branch information
Showing
3 changed files
with
91 additions
and
40 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package pgptest | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
"golang.org/x/crypto/openpgp" | ||
"golang.org/x/crypto/openpgp/armor" | ||
) | ||
|
||
// Sing signs data using RSA. It creates the key, sings data and returns the | ||
// ASCII armored public key and detached signature. | ||
func Sing(t *testing.T, data io.Reader) ([]byte, []byte) { | ||
pub := &bytes.Buffer{} | ||
asc := &bytes.Buffer{} | ||
|
||
// Create a new key. The openpgp.Entity hold the private and public keys. | ||
entity, err := openpgp.NewEntity("somekey", "", "", nil) | ||
|
||
// Create an encoder to serialize the public key. | ||
wPubKey, err := armor.Encode(pub, openpgp.PublicKeyType, nil) | ||
require.NoError(t, err, "could not create PGP ASCII Armor encoder") | ||
|
||
// Writes the public key to the io.Writer padded to armor.Encode. | ||
// Use entity.SerializePrivate if you need the private key. | ||
err = entity.Serialize(wPubKey) | ||
require.NoError(t, err, "could not serialize the public key") | ||
// cannot use defer as it needs to be closed before pub.Bytes() is invoked. | ||
wPubKey.Close() | ||
|
||
err = openpgp.ArmoredDetachSign(asc, entity, data, nil) | ||
require.NoError(t, err, "failed signing the data") | ||
|
||
return pub.Bytes(), asc.Bytes() | ||
} |