Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement KeyRing.Armor() #281

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Unreleased

### Added
- API to ASCII encode (armor) KeyRings:
```go
func (keyRing *KeyRing) Armor() (string, error)
```

## [2.8.0-alpha.1] 2024-04-09

### Added
Expand Down
12 changes: 12 additions & 0 deletions crypto/keyring.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (

"github.com/ProtonMail/go-crypto/openpgp"
"github.com/ProtonMail/go-crypto/openpgp/packet"
"github.com/ProtonMail/gopenpgp/v2/armor"
"github.com/ProtonMail/gopenpgp/v2/constants"
"github.com/pkg/errors"
)

Expand Down Expand Up @@ -130,6 +132,16 @@ func (keyRing *KeyRing) Serialize() ([]byte, error) {
return buffer.Bytes(), nil
}

// Armor returns the armored keyring as a string with default gopenpgp headers.
func (keyRing *KeyRing) Armor() (string, error) {
serialized, err := keyRing.Serialize()
if err != nil {
return "", err
}

return armor.ArmorWithType(serialized, constants.PublicKeyHeader)
}

// --- Extract info from key

// CountEntities returns the number of entities in the keyring.
Expand Down
9 changes: 9 additions & 0 deletions crypto/keyring_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package crypto
import (
"crypto/rsa"
"errors"
"regexp"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -163,6 +164,14 @@ func TestSerializeParse(t *testing.T) {
}
}

func TestArmor(t *testing.T) {
armoredRing, err := keyRingTestMultiple.Armor()
assert.Nil(t, err)

rTest := regexp.MustCompile(`(?s)^-----BEGIN PGP PUBLIC KEY BLOCK-----.*Version: GopenPGP [0-9]+\.[0-9]+\.[0-9]+.*-----END PGP PUBLIC KEY BLOCK-----$`)
assert.Regexp(t, rTest, armoredRing)
}

func TestClearPrivateKey(t *testing.T) {
keyRingCopy, err := keyRingTestMultiple.Copy()
if err != nil {
Expand Down