-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrypto.go
183 lines (162 loc) · 4.17 KB
/
crypto.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package utils
import (
"crypto/rsa"
"crypto/x509"
"encoding/hex"
"encoding/pem"
"errors"
"golang.org/x/crypto/bcrypt"
"golang.org/x/crypto/sha3"
"io"
"io/ioutil"
"os"
"strings"
)
// LoadKeyFileRSA Loads RSA key from .pem file
//
// path - string, filepath to the .pem file
//
// Returns:
//
// block - *rsa.PrivateKey, RSA private key loaded from file
// rest - *rsa.PublicKey, RSA public key loaded from file
func LoadKeyFileRSA(buf io.ReadCloser) (*rsa.PrivateKey, *rsa.PublicKey, error) {
// read raw file content
r, err := io.ReadAll(buf)
if err != nil {
return nil, nil, err
}
// decode content into block
block, _ := pem.Decode(r)
// create variables to store results in
var ok bool
var privateKey *rsa.PrivateKey
var publicKey *rsa.PublicKey
if strings.Contains(block.Type, "PRIVATE") {
// decode private key directly
// try PKCS1 format first
privateKey, err = x509.ParsePKCS1PrivateKey(block.Bytes)
if err != nil && !strings.Contains(err.Error(), "ParsePKCS8PrivateKey") {
return nil, nil, err
}
// try PKCS8 format
if err != nil {
pkey, err := x509.ParsePKCS8PrivateKey(block.Bytes)
if err != nil {
return nil, nil, err
}
privateKey = pkey.(*rsa.PrivateKey)
}
} else {
// decode public key
pKey, err := x509.ParsePKIXPublicKey(block.Bytes)
if err != nil {
return nil, nil, err
}
// attempt to cast interface{} to public key
publicKey, ok = pKey.(*rsa.PublicKey)
if !ok {
return nil, nil, errors.New("failed to cast interface to public key")
}
}
return privateKey, publicKey, nil
}
// LoadToken Loads authentication token from the specified path
// Args:
//
// path - string, the file path of the authentication token
//
// Returns:
//
// out - string, JWT token loaded from the passed file
func LoadToken(path string) (string, error) {
// read raw file content
tokenBytes, err := ioutil.ReadFile(path)
if err != nil {
return "", err
}
// load into string
token := string(tokenBytes)
return token, nil
}
// HashPassword Hashes a password with bcrypt
//
// password - string, the password that will be hashed
//
// Returns:
//
// out - string, a string representation of the hashed password
func HashPassword(password string) (string, error) {
hash, err := bcrypt.GenerateFromPassword([]byte(password), 8)
if err != nil {
return "", err
}
return string(hash), nil
}
// CheckPassword Checks a hashed password against a string
//
// password - string, the password that will be compared
// hash - string, the hashed password that will be compared
//
// Returns:
//
// out - bool, whether the passed password matches the hash
func CheckPassword(password string, hash string) (bool, error) {
err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password))
if err != nil {
// return false with nil error is password does not match
if err.Error() == "crypto/bcrypt: hashedPassword is not the hash of the given password" {
return false, nil
}
return false, err
}
return true, nil
}
// HashFile Hashes the contents of a file with SHA3-256
//
// fp - string, the file path for the file that will be hashes
//
// Returns:
//
// out - string, hex encoded SHA3-256 hash made of passed file's contents
func HashFile(fp string) (string, error) {
// open file
file, err := os.Open(fp)
if err != nil {
return "", err
}
// delay file close
defer file.Close()
// create SHA256 hasher
hasher := sha3.New256()
// copy file data to hasher
_, err = io.Copy(hasher, file)
if err != nil {
return "", err
}
// create output buffer
buff := make([]byte, 32)
// sum hash slices into buffer
hasher.Sum(buff[:0])
// hex encode hash and return
return hex.EncodeToString(buff), nil
}
// HashData Hashes the passed data with SHA3-256
//
// data - []byte, the data that will be hashed
//
// Returns:
//
// out - string, hex encoded SHA3-256 hash made of the passed data
func HashData(data []byte) (string, error) {
// create SHA256 hasher
hasher := sha3.New256()
// add data to hasher
hasher.Write(data)
// create output buffer
buff := make([]byte, 32)
// sum hash slices into buffer
hasher.Sum(buff[:0])
// hex encode hash and return
return hex.EncodeToString(buff), nil
}