-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy patharmor.go
52 lines (45 loc) · 1.1 KB
/
armor.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package armor
import (
"bytes"
"fmt"
"io"
"golang.org/x/crypto/openpgp/armor" //nolint: staticcheck
)
// ErrEncode represents an error from calling [EncodeArmor].
type ErrEncode struct {
Err error
}
func (e ErrEncode) Error() string {
return fmt.Sprintf("armor: could not encode ASCII armor: %v", e.Err)
}
func (e ErrEncode) Unwrap() error {
return e.Err
}
func EncodeArmor(blockType string, headers map[string]string, data []byte) (string, error) {
buf := new(bytes.Buffer)
w, err := armor.Encode(buf, blockType, headers)
if err != nil {
return "", ErrEncode{Err: err}
}
_, err = w.Write(data)
if err != nil {
return "", ErrEncode{Err: err}
}
err = w.Close()
if err != nil {
return "", ErrEncode{Err: err}
}
return buf.String(), nil
}
func DecodeArmor(armorStr string) (blockType string, headers map[string]string, data []byte, err error) {
buf := bytes.NewBufferString(armorStr)
block, err := armor.Decode(buf)
if err != nil {
return "", nil, nil, err
}
data, err = io.ReadAll(block.Body)
if err != nil {
return "", nil, nil, err
}
return block.Type, block.Header, data, nil
}