This repository was archived by the owner on Apr 11, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgcrypto.go
More file actions
108 lines (88 loc) · 2.47 KB
/
Copy pathgcrypto.go
File metadata and controls
108 lines (88 loc) · 2.47 KB
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
package gcrypto
import (
"crypto/aes"
"crypto/cipher"
"crypto/hmac"
"crypto/rand"
"crypto/sha256"
"encoding/hex"
"errors"
"fmt"
"io"
/* #nosec */
"crypto/md5"
)
// GenerateRandomKey will generate a new random key.
func GenerateRandomKey(len int) ([]byte, error) {
key := make([]byte, len)
_, err := rand.Read(key)
if err != nil {
return nil, err
}
return key, nil
}
// HMACSHA256 returns a SHA-256 of the given message signed by the secret.
func HMACSHA256(secret string, message string) (string, error) {
if secret == "" {
return "", errors.New("secret must not be empty")
}
h := hmac.New(sha256.New, []byte(secret))
_, err := h.Write([]byte(message))
if err != nil {
return "", err
}
return hex.EncodeToString(h.Sum(nil)), nil
}
// SHA256 returns a SHA-256 of the given message.
func SHA256(message string) (string, error) {
h := sha256.New()
_, err := h.Write([]byte(message))
if err != nil {
return "", err
}
return hex.EncodeToString(h.Sum(nil)), nil
}
// MD5 returns the MD5 checksum of a given message.
func MD5(message string) (string, error) {
/* #nosec */
h := md5.New()
_, err := h.Write([]byte(message))
if err != nil {
return "", err
}
return hex.EncodeToString(h.Sum(nil)), nil
}
// AES256Encrypt will run AES-256 excryption on the provided message.
func AES256Encrypt(message []byte, key []byte) ([]byte, error) {
block, err := aes.NewCipher(key)
if err != nil {
return nil, fmt.Errorf("unable to generate cipher with key: %s", err.Error())
}
gcm, err := cipher.NewGCM(block)
if err != nil {
return nil, fmt.Errorf("unable to generate block cipher: %s", err.Error())
}
nonce := make([]byte, gcm.NonceSize())
if _, err = io.ReadFull(rand.Reader, nonce); err != nil {
return nil, fmt.Errorf("unable to generate nonce: %s", err.Error())
}
return gcm.Seal(nonce, nonce, message, nil), nil
}
// AES256Decrypt will run AES-256 decryption on the provided data.
func AES256Decrypt(data []byte, key []byte) ([]byte, error) {
block, err := aes.NewCipher(key)
if err != nil {
return nil, fmt.Errorf("unable to generate cipher with key: %s", err.Error())
}
gcm, err := cipher.NewGCM(block)
if err != nil {
return nil, fmt.Errorf("unable to generate block cipher: %s", err.Error())
}
nonceSize := gcm.NonceSize()
nonce, ciphertext := data[:nonceSize], data[nonceSize:]
message, err := gcm.Open(nil, nonce, ciphertext, nil)
if err != nil {
return nil, fmt.Errorf("unable to decrypt data: %s", err.Error())
}
return message, nil
}