Skip to content

Commit 60b2555

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 2a07a34 commit 60b2555

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@ 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+
9+
### Added
10+
- API to ASCII encode (armor) KeyRings:
11+
```go
12+
func (keyRing *KeyRing) Armor() (string, error)
13+
```
14+
715
## [2.8.0-alpha.1] 2024-04-09
816

917
### Added

crypto/keyring.go

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

77
"github.com/ProtonMail/go-crypto/openpgp"
88
"github.com/ProtonMail/go-crypto/openpgp/packet"
9+
"github.com/ProtonMail/gopenpgp/v2/armor"
10+
"github.com/ProtonMail/gopenpgp/v2/constants"
911
"github.com/pkg/errors"
1012
)
1113

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

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

135147
// 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
@@ -3,6 +3,7 @@ package crypto
33
import (
44
"crypto/rsa"
55
"errors"
6+
"regexp"
67
"testing"
78

89
"github.com/stretchr/testify/assert"
@@ -163,6 +164,14 @@ func TestSerializeParse(t *testing.T) {
163164
}
164165
}
165166

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

0 commit comments

Comments
 (0)