-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathparser.go
53 lines (49 loc) · 1.23 KB
/
parser.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
53
package dolores
import (
"bufio"
"fmt"
"io"
"os"
"regexp"
"filippo.io/age"
)
func ParseIdentities(keyFile string) ([]age.Identity, error) {
// process identity from keyfile
f, err := os.Open(keyFile)
if err != nil {
return nil, fmt.Errorf("error opening keyfile %s: %w", keyFile, err)
}
defer f.Close()
ids, err := age.ParseIdentities(f)
if err != nil {
return nil, fmt.Errorf("failed to parse identity: %w", err)
}
return ids, nil
}
// revive:disable function-length
func ReadPublicKey(fname string) (string, error) {
keyFile, err := os.Open(fname)
if err != nil {
return "", fmt.Errorf("error opening keyfile %s: %w", fname, err)
}
const recipientFileSizeLimit = 1 << 24 // 16 MiB
scanner := bufio.NewScanner(io.LimitReader(keyFile, recipientFileSizeLimit))
var n int
re := regexp.MustCompile(`^#\s+public key.*(age1.*)`)
for scanner.Scan() {
n++
line := scanner.Text()
if line == "" {
continue
}
match := re.FindStringSubmatch(line)
if len(match) > 1 {
r, err := age.ParseX25519Recipient(match[1])
if err != nil {
return "", fmt.Errorf("malformed recipient at line %d %w", n, err)
}
return r.String(), nil
}
}
return "", fmt.Errorf("unable to extract public key: %w", ErrInvalidKeyFile)
}