-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathproof.go
More file actions
63 lines (56 loc) · 1.48 KB
/
proof.go
File metadata and controls
63 lines (56 loc) · 1.48 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
package fugl
import (
"encoding/json"
"errors"
"fmt"
"golang.org/x/crypto/openpgp"
"strings"
)
func OpenProof(entity *openpgp.Entity, proof string) (*Canary, string, error) {
// parse and verify signature
block, err := PGPVerify(entity, []byte(proof))
if err != nil {
return nil, "", err
}
// scan for seperator
start := 0
lines := strings.Split(string(block.Bytes), "\n")
for ; start < len(lines); start++ {
if strings.TrimRight(lines[start], "\n\r") == CANARY_SEPERATOR {
break
}
}
if start == len(lines) {
return nil, "", errors.New("Unable to find canary seperator")
}
// eat seperator and empty lines
des := strings.Join(lines[:start-1], "\n")
for start = start + 1; start < len(lines); start++ {
if strings.TrimRight(lines[start], "\n\r") != "" {
break
}
}
// load JSON structure
var canary Canary
ser := strings.Join(lines[start:], "\n")
err = json.Unmarshal([]byte(ser), &canary)
if err != nil {
return nil, "", errors.New("Unable to parse json structure")
}
return &canary, des, nil
}
func SealProof(entity *openpgp.Entity, canary Canary, description string) (string, error) {
// serialize canary
ser, err := json.MarshalIndent(canary, "", " ")
if err != nil {
return "", err
}
// add serperator and sign
var inner string
if description == "" {
inner = fmt.Sprintf("%s\n%s", CANARY_SEPERATOR, string(ser))
} else {
inner = fmt.Sprintf("%s\n%s\n\n%s", description, CANARY_SEPERATOR, string(ser))
}
return PGPSign(entity, []byte(inner))
}