-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
124 lines (105 loc) · 3.29 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
package main
import (
"crypto"
"crypto/md5"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"fmt"
"io/ioutil"
)
func rsaConfigSetup(rsaPrivateKeyLocation, rsaPrivateKeyPassword string) (*rsa.PrivateKey, error) {
if rsaPrivateKeyLocation == "" {
fmt.Println("no RSA Key given, generating temp one")
return GenRSA(4096)
}
priv, err := ioutil.ReadFile(rsaPrivateKeyLocation)
if err != nil {
fmt.Println("no RSA private key found, generating temp one")
return GenRSA(4096)
}
privPem, _ := pem.Decode(priv)
var privPemBytes []byte
if privPem.Type != "RSA PRIVATE KEY" {
fmt.Println("RSA private key is of the wrong type")
fmt.Println(privPem.Type)
}
if rsaPrivateKeyPassword != "" {
privPemBytes, err = x509.DecryptPEMBlock(privPem, []byte(rsaPrivateKeyPassword))
} else {
privPemBytes = privPem.Bytes
}
var parsedKey interface{}
if parsedKey, err = x509.ParsePKCS1PrivateKey(privPemBytes); err != nil {
if parsedKey, err = x509.ParsePKCS8PrivateKey(privPemBytes); err != nil { // note this returns type `interface{}`
fmt.Println("Unable to parse RSA private key, generating a temp one")
fmt.Println(err)
return GenRSA(4096)
}
}
var privateKey *rsa.PrivateKey
var ok bool
privateKey, ok = parsedKey.(*rsa.PrivateKey)
if !ok {
fmt.Println("Unable to parse RSA private key, generating a temp one")
fmt.Println(err)
return GenRSA(4096)
}
// pub, err := ioutil.ReadFile(rsaPublicKeyLocation)
// if err != nil {
// fmt.Println("No RSA public key found, generating temp one", nil)
// return GenRSA(4096)
// }
// pubPem, _ := pem.Decode(pub)
// if pubPem == nil {
// fmt.Println("Use `ssh-keygen -f id_rsa.pub -e -m pem > id_rsa.pem` to generate the pem encoding of your RSA public key")
// fmt.Println("rsa public key not in pem format")
// fmt.Println(rsaPublicKeyLocation)
// return GenRSA(4096)
// }
// if pubPem.Type != "RSA PUBLIC KEY" {
// fmt.Println("RSA public key is of the wrong type")
// fmt.Println(pubPem.Type)
// return GenRSA(4096)
// }
// if parsedKey, err = x509.ParsePKIXPublicKey(pubPem.Bytes); err != nil {
// fmt.Println("Unable to parse RSA public key, generating a temp one")
// fmt.Println(err)
// return GenRSA(4096)
// }
// var pubKey *rsa.PublicKey
// if pubKey, ok = parsedKey.(*rsa.PublicKey); !ok {
// fmt.Println("Unable to parse RSA public key, generating a temp one")
// fmt.Println(err)
// return GenRSA(4096)
// }
//privateKey.PublicKey = pubKey
return privateKey, nil
}
// GenRSA returns a new RSA key of bits length
func GenRSA(bits int) (*rsa.PrivateKey, error) {
key, err := rsa.GenerateKey(rand.Reader, bits)
fmt.Println("Failed to generate signing key")
fmt.Println(err)
return key, err
}
func main() {
privateKey, err := rsaConfigSetup("home/tomos/.ssh/id_rsa", "")
if err != nil {
panic(err)
}
signedMsg := "Yo, this message is approved by Tomos."
//unsignedMsg := "This message is not Tomos approved."
signedHash := md5.Sum([]byte(signedMsg))
//unsignedHash := md5.Sum([]byte(unsignedMsg))
//message := []byte("message to be signed")
//hashed := sha256.Sum256(message)
signature, err := rsa.SignPKCS1v15(rand.Reader, privateKey, crypto.MD5, signedHash[:])
if err != nil {
panic(err)
}
fmt.Println(signedHash)
fmt.Println(signature)
fmt.Printf("signed hash: %s", signedHash)
}