/
rsa.go
161 lines (122 loc) · 3.2 KB
/
rsa.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package asymmetric
import (
"crypto"
"crypto/rand"
"crypto/rsa"
CryptoRSA "crypto/rsa"
"crypto/sha256"
"crypto/x509"
"encoding/pem"
"log"
"os"
"github.com/haliliceylan/gowood/utils"
)
const RSA_PUBLIC_KEY_NAME = "rsa_public.key"
const RSA_PRIVATE_KEY_NAME = "rsa_private.key"
type RSA struct {
privateKey *CryptoRSA.PrivateKey
}
func (r *RSA) CreateKeys() {
reader := rand.Reader
bitSize := 2048
privateKeyFile, err := os.Create(RSA_PRIVATE_KEY_NAME)
if err != nil {
log.Fatal(err)
}
defer privateKeyFile.Close()
publicKeyFile, err := os.Create(RSA_PUBLIC_KEY_NAME)
if err != nil {
log.Fatal(err)
}
defer publicKeyFile.Close()
privateKey, err := CryptoRSA.GenerateKey(reader, bitSize)
if err != nil {
log.Fatal(err)
}
var privateKeyPemBlock = &pem.Block{
Type: "RSA PRIVATE KEY",
Bytes: x509.MarshalPKCS1PrivateKey(privateKey),
}
err = pem.Encode(privateKeyFile, privateKeyPemBlock)
if err != nil {
log.Fatal(err)
}
publicKey := privateKey.PublicKey
var publicKeyPemBlock = &pem.Block{
Type: "RSA PUBLIC KEY",
Bytes: x509.MarshalPKCS1PublicKey(&publicKey),
}
err = pem.Encode(publicKeyFile, publicKeyPemBlock)
if err != nil {
log.Fatal(err)
}
}
func (r *RSA) ReadKeys() {
privateKeyFile, err := utils.ReadFile(RSA_PRIVATE_KEY_NAME)
if err != nil {
log.Fatal(err)
}
_, err = utils.ReadFile(RSA_PUBLIC_KEY_NAME)
if err != nil {
log.Fatal(err)
}
privatePemBlock, _ := pem.Decode(privateKeyFile)
privPemBytes := privatePemBlock.Bytes
parsedPrivateKey, err := x509.ParsePKCS1PrivateKey(privPemBytes)
if err != nil {
log.Fatal(err)
}
r.privateKey = parsedPrivateKey
}
func (r *RSA) Encrypt(input []byte) []byte {
encryptedBytes, err := CryptoRSA.EncryptOAEP(
sha256.New(),
rand.Reader,
&r.privateKey.PublicKey,
input,
nil)
if err != nil {
log.Fatal(err)
}
return encryptedBytes
}
func (r *RSA) Decrypt(input []byte) []byte {
decryptedBytes, err := r.privateKey.Decrypt(nil, input, &rsa.OAEPOptions{Hash: crypto.SHA256})
if err != nil {
log.Fatal(err)
}
return decryptedBytes
}
func (r *RSA) Sign(input []byte) []byte {
// Before signing, we need to hash our message
// The hash is what we actually sign
msgHash := sha256.New()
_, err := msgHash.Write(input)
if err != nil {
log.Fatal(err)
}
msgHashSum := msgHash.Sum(nil)
// In order to generate the signature, we provide a random number generator,
// our private key, the hashing algorithm that we used, and the hash sum
// of our message
signature, err := rsa.SignPSS(rand.Reader, r.privateKey, crypto.SHA256, msgHashSum, nil)
if err != nil {
panic(err)
}
return signature
}
func (r *RSA) Verify(input []byte, signature []byte) bool {
// Before signing, we need to hash our message
// The hash is what we actually sign
msgHash := sha256.New()
_, err := msgHash.Write(input)
if err != nil {
log.Fatal(err)
}
msgHashSum := msgHash.Sum(nil)
// To verify the signature, we provide the public key, the hashing algorithm
// the hash sum of our message and the signature we generated previously
// there is an optional "options" parameter which can omit for now
err = rsa.VerifyPSS(&r.privateKey.PublicKey, crypto.SHA256, msgHashSum, signature, nil)
return err == nil
}