-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformats.go
More file actions
107 lines (92 loc) · 2.91 KB
/
Copy pathformats.go
File metadata and controls
107 lines (92 loc) · 2.91 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package signing_key
import (
"fmt"
"io"
)
type Format string
type formatter interface {
encode(key *Key) ([]byte, error)
decode(data []byte) (*Key, error)
decodeAll(data []byte) ([]*Key, error)
}
var formats = make(map[Format]formatter)
func mustRegisterFormat(format Format, formatter formatter) {
if _, ok := formats[format]; ok {
panic("format already registered")
}
formats[format] = formatter
}
// Encode encodes the key in the given format, ready for writing to a file.
func (k *Key) Encode(format Format) ([]byte, error) {
k.PrivateKey.Public()
if f, ok := formats[format]; ok {
return f.encode(k)
}
return nil, fmt.Errorf("unknown format: %s", format)
}
// DecodeRawKey attempts to decode the given key in any of the supported formats. Expects a file's contents as input.
func DecodeRawKey(key []byte) (*Key, error) {
for _, f := range formats {
k, err := f.decode(key)
if err == nil {
return k, nil
}
}
return nil, fmt.Errorf("key is not in a known format")
}
// DecodeKey attempts to decode the given key in any of the supported formats. Expects a file's contents as input.
func DecodeKey(key io.Reader) (*Key, error) {
b, err := io.ReadAll(key)
if err != nil {
return nil, err
}
return DecodeRawKey(b)
}
// DecodeKeyWith attempts to decode the given key in the given format. Expects a file's contents as input.
func DecodeKeyWith(key io.Reader, format Format) (*Key, error) {
b, err := io.ReadAll(key)
if err != nil {
return nil, err
}
return DecodeRawKeyWith(b, format)
}
// DecodeRawKeyWith attempts to decode the given key in the given format. Expects a file's contents as input.
func DecodeRawKeyWith(key []byte, format Format) (*Key, error) {
if f, ok := formats[format]; ok {
return f.decode(key)
}
return nil, fmt.Errorf("unknown format: %s", format)
}
// DecodeAllKeys attempts to decode all keys in the given file. All contained keys must be in the same format.
func DecodeAllKeys(keys io.Reader) ([]*Key, error) {
b, err := io.ReadAll(keys)
if err != nil {
return nil, err
}
return DecodeAllRawKeys(b)
}
// DecodeAllRawKeys attempts to decode all keys in the given file. All contained keys must be in the same format.
func DecodeAllRawKeys(keys []byte) ([]*Key, error) {
for _, f := range formats {
ks, err := f.decodeAll(keys)
if err == nil {
return ks, nil
}
}
return nil, fmt.Errorf("keys are not in a known format")
}
// DecodeAllKeysWith attempts to decode all keys in the given file with the given format.
func DecodeAllKeysWith(keys io.Reader, format Format) ([]*Key, error) {
b, err := io.ReadAll(keys)
if err != nil {
return nil, err
}
return DecodeAllRawKeysWith(b, format)
}
// DecodeAllRawKeysWith attempts to decode all keys in the given file with the given format.
func DecodeAllRawKeysWith(keys []byte, format Format) ([]*Key, error) {
if f, ok := formats[format]; ok {
return f.decodeAll(keys)
}
return nil, fmt.Errorf("unknown format: %s", format)
}