-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathcrypto.go
More file actions
116 lines (107 loc) · 3.05 KB
/
crypto.go
File metadata and controls
116 lines (107 loc) · 3.05 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
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
package main
import (
"crypto/rand"
"encoding/hex"
"golang.org/x/crypto/ed25519"
)
// ED25519Keys This is a struct for holding keys and a signature.
type ED25519Keys struct {
publicKey string
privateKey string
signedKey string
selfCert string
}
func initKeys() *ED25519Keys {
if !fileExists(privKeyFilePath) {
generateKeys()
}
keys := ED25519Keys{}
keyspublicKey := readFile(pubKeyFilePath)
keysprivateKey := readFile(privKeyFilePath)
keyssignedKey := readFile(signedKeyFilePath)
keysselfCert := readFile(selfCertFilePath)
keys.publicKey = keyspublicKey[:64]
keys.privateKey = keysprivateKey[:64]
keys.signedKey = keyssignedKey[:64]
keys.selfCert = keysselfCert[:64]
return &keys
}
func generateKeys() *ED25519Keys {
keys := ED25519Keys{}
pubKey, privKey, err := ed25519.GenerateKey(rand.Reader)
if err != nil {
handle("error: ", err)
}
keys.privateKey = hex.EncodeToString(privKey[0:32])
keys.publicKey = hex.EncodeToString(pubKey)
signedKey := ed25519.Sign(privKey, pubKey)
keys.signedKey = hex.EncodeToString(signedKey)
keys.selfCert = keys.publicKey + keys.signedKey
createFile(pubKeyFilePath)
createFile(privKeyFilePath)
createFile(signedKeyFilePath)
createFile(selfCertFilePath)
writeFile(pubKeyFilePath, keys.publicKey[:64])
writeFile(privKeyFilePath, keys.privateKey[:64])
writeFile(signedKeyFilePath, keys.signedKey[:64])
writeFile(selfCertFilePath, keys.selfCert[:64])
return &keys
}
func sign(myKeys *ED25519Keys, msg string) string {
messageBytes := []byte(msg)
privateKey, err := hex.DecodeString(myKeys.privateKey)
if err != nil {
handle("private key error: ", err)
}
publicKey, err := hex.DecodeString(myKeys.publicKey)
if err != nil {
handle("public key error: ", err)
}
privateKey = append(privateKey, publicKey...)
signature := ed25519.Sign(privateKey, messageBytes)
return hex.EncodeToString(signature)
}
func signKey(myKeys *ED25519Keys, publicKey string) string {
messageBytes, err := hex.DecodeString(publicKey)
if err != nil {
handle("error: ", err)
}
privateKey, err := hex.DecodeString(myKeys.privateKey)
if err != nil {
handle("error: ", err)
}
pubKey, err := hex.DecodeString(myKeys.publicKey)
if err != nil {
handle("error: ", err)
}
privateKey = append(privateKey, pubKey...)
signature := ed25519.Sign(privateKey, messageBytes)
return hex.EncodeToString(signature)
}
func verifySignature(publicKey string, msg string, signature string) bool {
pubKey, err := hex.DecodeString(publicKey)
if err != nil {
handle("error: ", err)
}
messageBytes := []byte(msg)
sig, err := hex.DecodeString(signature)
if err != nil {
handle("error: ", err)
}
return ed25519.Verify(pubKey, messageBytes, sig)
}
func verifySignedKey(publicKey string, publicSigningKey string, signature string) bool {
pubKey, err := hex.DecodeString(publicKey)
if err != nil {
handle("error: ", err)
}
pubSignKey, err := hex.DecodeString(publicSigningKey)
if err != nil {
handle("error: ", err)
}
sig, err := hex.DecodeString(signature)
if err != nil {
handle("error: ", err)
}
return ed25519.Verify(pubSignKey, pubKey, sig)
}