-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchecker.go
92 lines (80 loc) · 1.67 KB
/
checker.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
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
package main
import (
"crypto/sha256"
"crypto/sha512"
_ "embed"
"errors"
"fmt"
"log"
"os"
"path/filepath"
"regexp"
"github.com/number571/go-peer/pkg/crypto/asymmetric"
"github.com/number571/go-peer/pkg/encoding"
)
var (
//go:embed README.md
readme string
)
var (
re = regexp.MustCompile(`list/(\w+\.key)[\S\s]*?sha256:\s(\w+)[\S\s]*?sha384:\s(\w+)[\S\s]*?sha512:\s(\w+)`)
mp = make(map[string][3]string, 512)
)
var (
hashFuncs = []func(string) string{
func(v string) string {
s := sha256.Sum256([]byte(v))
return encoding.HexEncode(s[:])
},
func(v string) string {
s := sha512.Sum384([]byte(v))
return encoding.HexEncode(s[:])
},
func(v string) string {
s := sha512.Sum512([]byte(v))
return encoding.HexEncode(s[:])
},
}
)
func init() {
match := re.FindAllStringSubmatch(readme, -1)
for _, m := range match {
if len(m) != 5 {
log.Fatal("len(m) != 5")
}
mp[m[1]] = [3]string{m[2], m[3], m[4]}
}
}
func main() {
entries, err := os.ReadDir("./list")
if err != nil {
log.Fatal(err)
}
for _, e := range entries {
if e.IsDir() {
continue
}
pkFile := e.Name()
if err := pubKeyIsValid(pkFile); err != nil {
log.Fatalf("'%s' is invalid: %s", pkFile, err.Error())
}
}
}
func pubKeyIsValid(pkFile string) error {
pk, err := os.ReadFile(filepath.Join("list", pkFile))
if err != nil {
return err
}
pubKey := asymmetric.LoadPubKey(string(pk))
if pubKey == nil {
return errors.New("read public key")
}
pubKeyStr := pubKey.ToString()
for i, hashsum := range hashFuncs {
wantHash := mp[pkFile][i]
if h := hashsum(pubKeyStr); h != wantHash {
return fmt.Errorf("want:'%s'; got:'%s'", wantHash, h)
}
}
return nil
}