/
common.go
285 lines (231 loc) · 7.11 KB
/
common.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/*
Copyright 2014 Huawei Technologies Co., Ltd. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package utils
import (
"bytes"
"crypto"
"crypto/md5"
"crypto/rand"
"crypto/rsa"
"crypto/sha256"
"crypto/x509"
"encoding/base64"
"encoding/hex"
"encoding/json"
"encoding/pem"
"errors"
"fmt"
"os"
"reflect"
"regexp"
"strings"
"time"
"github.com/fernet/fernet-go"
)
type EncryptMethod string
const (
EncryptRSA = "rsa"
EncryptNone = "none"
EncryptNotSupported = "not-supported"
)
func NewEncryptMethod(method string) EncryptMethod {
switch method {
case string(EncryptRSA):
return EncryptRSA
case "":
return EncryptNone
case string(EncryptNone):
return EncryptNone
}
return EncryptNotSupported
}
// IsDirExist checks if a path is an existed dir
func IsDirExist(path string) bool {
fi, err := os.Stat(path)
if err != nil {
return os.IsExist(err)
}
return fi.IsDir()
}
// IsFileExist checks if a file url is an exist file
func IsFileExist(filename string) bool {
_, err := os.Stat(filename)
return err == nil || os.IsExist(err)
}
// Containe checks if a target item exists in the list/map
func Contain(obj interface{}, target interface{}) (bool, error) {
targetValue := reflect.ValueOf(target)
switch reflect.TypeOf(target).Kind() {
case reflect.Slice, reflect.Array:
for i := 0; i < targetValue.Len(); i++ {
if targetValue.Index(i).Interface() == obj {
return true, nil
}
}
case reflect.Map:
if targetValue.MapIndex(reflect.ValueOf(obj)).IsValid() {
return true, nil
}
}
return false, errors.New("not in array")
}
// ValidatePassword verifies if a password is valid
func ValidatePassword(password string) error {
if valida, _ := regexp.MatchString("[:alpha:]", password); valida != true {
return fmt.Errorf("No alpha character in the password.")
}
if valida, _ := regexp.MatchString("[:digit:]", password); valida != true {
return fmt.Errorf("No digital character in the password.")
}
if len(password) < 5 || len(password) > 30 {
return fmt.Errorf("Password characters length should be between 5 - 30.")
}
return nil
}
// EncodeBasicAuth encode username and password into a base64 string
func EncodeBasicAuth(username string, password string) string {
auth := username + ":" + password
msg := []byte(auth)
authorization := make([]byte, base64.StdEncoding.EncodedLen(len(msg)))
base64.StdEncoding.Encode(authorization, msg)
return string(authorization)
}
// DecodeBasicAuth decode a base64 string into a username and a password
func DecodeBasicAuth(authorization string) (username string, password string, err error) {
basic := strings.Split(strings.TrimSpace(authorization), " ")
if len(basic) <= 1 {
return "", "", err
}
decLen := base64.StdEncoding.DecodedLen(len(basic[1]))
decoded := make([]byte, decLen)
authByte := []byte(basic[1])
n, err := base64.StdEncoding.Decode(decoded, authByte)
if err != nil {
return "", "", err
}
if n > decLen {
return "", "", fmt.Errorf("Something went wrong decoding auth config")
}
arr := strings.SplitN(string(decoded), ":", 2)
if len(arr) != 2 {
return "", "", fmt.Errorf("Invalid auth configuration file")
}
username = arr[0]
password = strings.Trim(arr[1], "\x00")
return username, password, nil
}
// MD5 generates a md value of a key automaticly
func MD5(key string) string {
md5String := fmt.Sprintf("dockyard %s is a container %d hub", key, time.Now().Unix())
h := md5.New()
h.Write([]byte(md5String))
return hex.EncodeToString(h.Sum(nil))
}
// GenerateRSAKeyPair generate a private key and a public key
func GenerateRSAKeyPair(bits int) ([]byte, []byte, error) {
privKey, err := rsa.GenerateKey(rand.Reader, bits)
if err != nil {
return nil, nil, err
}
privBytes := x509.MarshalPKCS1PrivateKey(privKey)
pubBytes, err := x509.MarshalPKIXPublicKey(&privKey.PublicKey)
if err != nil {
return nil, nil, err
}
privBlock := &pem.Block{Type: "RSA PRIVATE KEY", Bytes: privBytes}
pubBlock := &pem.Block{Type: "RSA PUBLIC KEY", Bytes: pubBytes}
return pem.EncodeToMemory(privBlock), pem.EncodeToMemory(pubBlock), nil
}
// RSAEncrypt encrypts a content by a public key
func RSAEncrypt(keyBytes []byte, contentBytes []byte) ([]byte, error) {
pubKey, err := getPubKey(keyBytes)
if err != nil {
return nil, err
}
return rsa.EncryptPKCS1v15(rand.Reader, pubKey, contentBytes)
}
// RSADecrypt decrypts content by a private key
func RSADecrypt(keyBytes []byte, contentBytes []byte) ([]byte, error) {
privKey, err := getPrivKey(keyBytes)
if err != nil {
return nil, err
}
return rsa.DecryptPKCS1v15(rand.Reader, privKey, contentBytes)
}
// SHA256Sign signs a content by a private key
func SHA256Sign(keyBytes []byte, contentBytes []byte) ([]byte, error) {
privKey, err := getPrivKey(keyBytes)
if err != nil {
return nil, err
}
hashed := sha256.Sum256(contentBytes)
return rsa.SignPKCS1v15(rand.Reader, privKey, crypto.SHA256, hashed[:])
}
// SHA256Verify verifies if a content is valid by a signed data an a public key
func SHA256Verify(keyBytes []byte, contentBytes []byte, signBytes []byte) error {
pubKey, err := getPubKey(keyBytes)
if err != nil {
return err
}
signStr := hex.EncodeToString(signBytes)
newSignBytes, _ := hex.DecodeString(signStr)
hashed := sha256.Sum256(contentBytes)
return rsa.VerifyPKCS1v15(pubKey, crypto.SHA256, hashed[:], newSignBytes)
}
func getPrivKey(privBytes []byte) (*rsa.PrivateKey, error) {
block, _ := pem.Decode(privBytes)
if block == nil {
return nil, errors.New("Fail to decode private key")
}
return x509.ParsePKCS1PrivateKey(block.Bytes)
}
func getPubKey(pubBytes []byte) (*rsa.PublicKey, error) {
block, _ := pem.Decode(pubBytes)
if block == nil {
return nil, errors.New("Fail to decode public key")
}
pubInterface, err := x509.ParsePKIXPublicKey(block.Bytes)
if err != nil {
return nil, err
}
pubKey, ok := pubInterface.(*rsa.PublicKey)
if !ok {
return nil, errors.New("Fail get public key from public interface")
}
return pubKey, nil
}
// TokenMarshal encrypts data in `v`
func TokenMarshal(v interface{}, key string) ([]byte, error) {
var buf bytes.Buffer
err := json.NewEncoder(&buf).Encode(v)
if err != nil {
return nil, err
}
k, err := fernet.DecodeKey(key)
if err != nil {
return nil, err
}
return fernet.EncryptAndSign(buf.Bytes(), k)
}
// TokenUnmarshal decryptes a token and save the original data to `v`.
func TokenUnmarshal(token string, key string, v interface{}) error {
k, err := fernet.DecodeKey(key)
if err != nil {
return err
}
msg := fernet.VerifyAndDecrypt([]byte(token), time.Hour, []*fernet.Key{k})
if msg == nil {
return errors.New("invalid or expired token")
}
return json.NewDecoder(bytes.NewBuffer(msg)).Decode(&v)
}