-
Notifications
You must be signed in to change notification settings - Fork 9
/
keys.go
104 lines (91 loc) · 1.96 KB
/
keys.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
package main
import (
"crypto/rsa"
"crypto/x509"
"encoding/asn1"
"encoding/json"
"encoding/pem"
"io/ioutil"
"os"
"time"
)
type Key struct {
ID string `json:"id"`
PrivK string `json:"privK"` //path of the PrivK file
PubK string `json:"pubK"` //path of the PubK file
Date time.Time `json:"date"`
Verified bool `json:"verified"`
Signed string `json:"signed"`
}
func savePEMKey(fileName string, key *rsa.PrivateKey) {
outFile, err := os.Create(fileName)
check(err)
defer outFile.Close()
var privateKey = &pem.Block{
Type: "PRIVATE KEY",
Bytes: x509.MarshalPKCS1PrivateKey(key),
}
err = pem.Encode(outFile, privateKey)
check(err)
}
func savePublicPEMKey(fileName string, pubkey rsa.PublicKey) {
asn1Bytes, err := asn1.Marshal(pubkey)
check(err)
var pemkey = &pem.Block{
Type: "PUBLIC KEY",
Bytes: asn1Bytes,
}
pemfile, err := os.Create(fileName)
check(err)
defer pemfile.Close()
err = pem.Encode(pemfile, pemkey)
check(err)
}
func openPEMKey(path string) (key rsa.PrivateKey) {
return
}
func openPublicPEMKey(path string) (key rsa.PublicKey) {
return
}
func readKeys() []Key {
path := keysDir + "/keys.json"
var keys []Key
file, err := ioutil.ReadFile(path)
check(err)
content := string(file)
json.Unmarshal([]byte(content), &keys)
return keys
}
func saveKeys(keys []Key) {
jsonKeys, err := json.Marshal(keys)
check(err)
err = ioutil.WriteFile(keysDir+"/keys.json", jsonKeys, 0644)
check(err)
}
func getKeyByKeyID(keyID string) (k Key) {
keys := readKeys()
for _, key := range keys {
if key.ID == keyID {
k = key
}
}
return k
}
/*
func getServerPubK(url string) ownrsa.RSAPublicKey {
r, err := http.Get(url + "/")
check(err)
fmt.Println(r)
decoder := json.NewDecoder(r.Body)
//var sigmaString string
var pubK ownrsa.RSAPublicKey
err = decoder.Decode(&pubK)
if err != nil {
panic(err)
}
defer r.Body.Close()
color.Blue("received server pubK:")
fmt.Println(pubK)
return pubK
}
*/