Skip to content

Commit 27e84f1

Browse files
committed
Implement KeyRing.Armor()
This allows for ASCII encoding of keyrings the same way as already implemented for individual keys. Signed-off-by: Georg Pfuetzenreuter <georg.pfuetzenreuter@suse.com>
1 parent d3fe807 commit 27e84f1

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
## Unreleased
8+
### Added
9+
- API to ASCII encode (armor) KeyRings:
10+
```go
11+
func (keyRing *KeyRing) Armor() (string, error)
12+
```
13+
714
## [3.1.0] 2024-11-25
815
### Added
916
- Add decryption option to allow disabling the integrity tag requirement.

crypto/keyring.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import (
77

88
"github.com/ProtonMail/go-crypto/openpgp/packet"
99
openpgp "github.com/ProtonMail/go-crypto/openpgp/v2"
10+
"github.com/ProtonMail/gopenpgp/v2/armor"
11+
"github.com/ProtonMail/gopenpgp/v2/constants"
1012
"github.com/pkg/errors"
1113
)
1214

@@ -133,6 +135,16 @@ func (keyRing *KeyRing) Serialize() ([]byte, error) {
133135
return buffer.Bytes(), nil
134136
}
135137

138+
// Armor returns the armored keyring as a string with default gopenpgp headers.
139+
func (keyRing *KeyRing) Armor() (string, error) {
140+
serialized, err := keyRing.Serialize()
141+
if err != nil {
142+
return "", err
143+
}
144+
145+
return armor.ArmorWithType(serialized, constants.PublicKeyHeader)
146+
}
147+
136148
// --- Extract info from key
137149

138150
// CountEntities returns the number of entities in the keyring.

crypto/keyring_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package crypto
22

33
import (
44
"crypto/rsa"
5+
"regexp"
56
"testing"
67

78
"github.com/stretchr/testify/assert"
@@ -160,6 +161,14 @@ func TestSerializeParse(t *testing.T) {
160161
}
161162
}
162163

164+
func TestArmor(t *testing.T) {
165+
armoredRing, err := keyRingTestMultiple.Armor()
166+
assert.Nil(t, err)
167+
168+
rTest := regexp.MustCompile(`(?s)^-----BEGIN PGP PUBLIC KEY BLOCK-----.*Version: GopenPGP [0-9]+\.[0-9]+\.[0-9]+.*-----END PGP PUBLIC KEY BLOCK-----$`)
169+
assert.Regexp(t, rTest, armoredRing)
170+
}
171+
163172
func TestClearPrivateKey(t *testing.T) {
164173
keyRingCopy, err := keyRingTestMultiple.Copy()
165174
if err != nil {

0 commit comments

Comments
 (0)