-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
55 lines (45 loc) · 1.25 KB
/
utils.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
package main
import (
"bytes"
"crypto/sha512"
"encoding/hex"
"fmt"
"github.com/wuhan-support/certgen/keys"
"golang.org/x/crypto/openpgp"
"os"
"strings"
)
func GenerateCertID(record Record) string {
message := fmt.Sprintf("[%s] %s <%s>", record.Group, record.Name, record.Email)
hash := sha512.Sum512([]byte(message))
return hex.EncodeToString(hash[:])
}
func GenerateSignMessage(record Record) string {
return fmt.Sprintf("兹证明:%s 于 wuhan.support %s 做出其贡献", record.Name, record.Group)
}
// PGPSignString sign message and returns an ASCII-armored signed message
func PGPSignString(message string) string {
// Read in public key
keyringFileBuffer, _ := os.Open(keys.SecretKey)
defer keyringFileBuffer.Close()
entityList, err := openpgp.ReadArmoredKeyRing(keyringFileBuffer)
if err != nil {
panic(err)
}
entity := entityList[0]
err = entity.PrivateKey.Decrypt(keys.SecretKeyPassphrase)
if err != nil {
panic(err)
}
writeBuf := new(bytes.Buffer)
readBuf := strings.NewReader(message)
err = openpgp.ArmoredDetachSign(writeBuf, entity, readBuf, nil)
if err != nil {
panic(err)
}
return writeBuf.String()
}
func sanitize(message string) string {
m := strings.TrimSpace(message)
return strings.ReplaceAll(m, "\uFEFF", "")
}