-
-
Notifications
You must be signed in to change notification settings - Fork 128
/
main.go
147 lines (130 loc) · 3.17 KB
/
main.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// The rsagen command generates rsa.PublicKey variables from PEM-encoded
// RSA public keys.
package main
import (
"bufio"
"bytes"
"crypto/rsa"
"embed"
"flag"
"fmt"
"go/format"
"io"
"io/fs"
"log"
"os"
"text/template"
"github.com/go-faster/errors"
"github.com/gotd/td/internal/crypto"
)
//go:embed _template/*.tmpl
var embedFS embed.FS
var funcs = template.FuncMap{
"fingerprint": func(pubkey *rsa.PublicKey) string {
return fmt.Sprintf("%08x", uint64(crypto.RSAFingerprint(pubkey)))
},
"chunks": func(b []byte, size int) [][]byte {
var chunks [][]byte
for len(b) > size {
chunks = append(chunks, b[:size])
b = b[size:]
}
if len(b) != 0 {
chunks = append(chunks, b)
}
return chunks
},
"single": func(keys []*rsa.PublicKey) (*rsa.PublicKey, error) {
if count := len(keys); count != 1 {
return nil, errors.Errorf("expected single key, got %d keys", count)
}
return keys[0], nil
},
}
func main() {
if err := run(); err != nil {
os.Exit(1)
}
}
func run() error {
log.SetFlags(log.Llongfile)
var (
inPath = flag.String("f", "", "input path (defaults to stdin)")
outPath = flag.String("o", "", "output path (defaults to stdout)")
tplPath = flag.String("templates", "", "templates directory (defaults to embed)")
tplName = flag.String("exec", "main.tmpl", "template name")
pkgName = flag.String("pkg", "main", "package name")
varName = flag.String("var", "PK", "variable name")
singleMode = flag.Bool("single", false, "emit single key instead of slice")
formatOutput = flag.Bool("format", true, "run gofmt on output")
testFunc = flag.String("test", "", "test function name")
)
flag.Parse()
if *testFunc == "" {
*testFunc = "TestFingerprint" + *varName
}
var err error
var in []byte
if *inPath != "" {
in, err = os.ReadFile(*inPath)
} else {
in, err = io.ReadAll(os.Stdin)
}
if err != nil {
log.Printf("read input: %v", err)
return err
}
keys, err := crypto.ParseRSAPublicKeys(in)
if err != nil {
log.Printf("parse public keys: %v", err)
return err
}
fsys, _ := fs.Sub(embedFS, "_template")
if *tplPath != "" {
fsys = os.DirFS(*tplPath)
}
tpl, err := template.New("").Funcs(funcs).ParseFS(fsys, "*.tmpl")
if err != nil {
log.Printf("parse templates: %v", err)
return err
}
var inLines [][]byte
for sc := bufio.NewScanner(bytes.NewReader(in)); sc.Scan(); {
line := sc.Bytes()
line = bytes.TrimSpace(line)
if len(line) == 0 {
continue
}
inLines = append(inLines, line)
}
buf := bytes.NewBuffer(nil)
if err := tpl.ExecuteTemplate(buf, *tplName, map[string]interface{}{
"Package": *pkgName,
"Variable": *varName,
"Keys": keys,
"Single": *singleMode,
"InputLines": inLines,
"TestFunc": *testFunc,
}); err != nil {
log.Printf("execute template: %v", err)
return err
}
if *formatOutput {
p, err := format.Source(buf.Bytes())
if err != nil {
log.Printf("format output: %v", err)
return err
}
buf = bytes.NewBuffer(p)
}
if *outPath != "" {
err = os.WriteFile(*outPath, buf.Bytes(), 0o600)
} else {
_, err = buf.WriteTo(os.Stdout)
}
if err != nil {
log.Printf("write output: %v", err)
return err
}
return nil
}