-
Notifications
You must be signed in to change notification settings - Fork 2
/
crypt.go
166 lines (136 loc) · 3.3 KB
/
crypt.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
162
163
164
165
166
package main
import (
"bytes"
"crypto/rand"
"crypto/rsa"
"crypto/sha1"
"crypto/x509"
"encoding/base64"
"encoding/binary"
"encoding/pem"
"fmt"
"io/ioutil"
"math/big"
"strings"
)
type Crypt struct {
publicKey *rsa.PublicKey
privateKeyBlock *pem.Block
}
func NewCrypt(pubFile, privFile string) (Crypt, error) {
c := Crypt{}
pubData, err := ioutil.ReadFile(pubFile)
if err != nil {
return c, fmt.Errorf("error while reading public key file %s: %s", pubFile, err.Error())
}
err = c.bytesToPublicKey(pubData)
if err != nil {
return c, err
}
privData, err := ioutil.ReadFile(privFile)
if err != nil {
return c, fmt.Errorf("error while reading private key file %s: %s", privFile, err.Error())
}
err = c.bytesToPrivateKeyBlock(privData)
if err != nil {
return c, err
}
return c, nil
}
func (c Crypt) Encrypt(data []byte) ([]byte, error) {
return rsa.EncryptOAEP(sha1.New(), rand.Reader, c.publicKey, data, []byte("scum file"))
}
func (c Crypt) Decrypt(data, pass []byte) ([]byte, error) {
priv, err := c.getPrivateKey(pass)
if err != nil {
return []byte{}, err
}
return rsa.DecryptOAEP(sha1.New(), rand.Reader, priv, data, []byte("scum file"))
}
func (c *Crypt) bytesToPrivateKeyBlock(priv []byte) error {
block, _ := pem.Decode(priv)
if block == nil {
return fmt.Errorf("private key could not be decoded")
}
c.privateKeyBlock = block
return nil
}
func (c Crypt) getPrivateKey(password []byte) (*rsa.PrivateKey, error) {
enc := x509.IsEncryptedPEMBlock(c.privateKeyBlock)
b := c.privateKeyBlock.Bytes
var err error
if enc {
b, err = x509.DecryptPEMBlock(c.privateKeyBlock, password)
if err != nil {
return nil, err
}
}
key, err := x509.ParsePKCS1PrivateKey(b)
if err != nil {
return nil, err
}
return key, nil
}
func (c *Crypt) bytesToPublicKey(pub []byte) error {
tokens := strings.Split(string(pub), " ")
if len(tokens) < 2 {
return fmt.Errorf("invalid key format; must contain at least two fields (keytype data [comment])")
}
key_type := tokens[0]
data, err := base64.StdEncoding.DecodeString(tokens[1])
if err != nil {
return err
}
format, e, n, err := c.getRSAValues(data)
if err != nil {
return err
}
if format != key_type {
return fmt.Errorf("key type said %s, but encoded format said %s. these should match", key_type, format)
}
c.publicKey = &rsa.PublicKey{
N: n,
E: int(e.Int64()),
}
return nil
}
func (c Crypt) readLength(data []byte) ([]byte, uint32, error) {
l_buf := data[0:4]
buf := bytes.NewBuffer(l_buf)
var length uint32
err := binary.Read(buf, binary.BigEndian, &length)
if err != nil {
return nil, 0, err
}
return data[4:], length, nil
}
func (c Crypt) readBigInt(data []byte, length uint32) ([]byte, *big.Int, error) {
var bigint = new(big.Int)
bigint.SetBytes(data[0:length])
return data[length:], bigint, nil
}
func (c Crypt) getRSAValues(data []byte) (format string, e *big.Int, n *big.Int, err error) {
data, length, err := c.readLength(data)
if err != nil {
return
}
format = string(data[0:length])
data = data[length:]
data, length, err = c.readLength(data)
if err != nil {
return
}
data, e, err = c.readBigInt(data, length)
if err != nil {
return
}
data, length, err = c.readLength(data)
if err != nil {
return
}
_, n, err = c.readBigInt(data, length)
if err != nil {
return
}
return
}