-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaes.go
126 lines (112 loc) · 2.75 KB
/
aes.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
package aes
import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"crypto/sha256"
"encoding/base64"
"encoding/json"
"io"
"strings"
"time"
)
// TTLMessage struct for time to live message
type TTLMessage struct {
Message string `json:"message"`
TTL int64 `json:"ttl"`
}
// AasaamAES is encryption decryption
type AasaamAES struct {
key []byte
}
// GenerateKey Base64 encoded random bytes with length 32
func GenerateKey() string {
b := make([]byte, 32)
_, err := rand.Read(b)
if err != nil {
panic(err.Error())
}
return base64.StdEncoding.EncodeToString(b)
}
// GenerateHashKey Generate encryption key for special props
func GenerateHashKey(key string, props []string) string {
hash := sha256.Sum256([]byte(key + ":" + strings.Join(props, ":")))
return base64.StdEncoding.EncodeToString(hash[:])
}
// NewAasaamAES Create instance
func NewAasaamAES(key string) *AasaamAES {
byteKey, err := base64.StdEncoding.DecodeString(key)
if err != nil {
panic(err.Error())
}
aes := AasaamAES{key: byteKey}
return &aes
}
// EncryptTTL Encrypt message with time to live
func (a *AasaamAES) EncryptTTL(message string, ttl int64) string {
ttlMessage := TTLMessage{Message: message, TTL: time.Now().Unix() + ttl}
jsonMessage, _ := json.Marshal(ttlMessage)
return a.Encrypt(string(jsonMessage))
}
// DecryptTTL Decrypted message that contain time to liv
// return Original message or empty string on failure
func (a *AasaamAES) DecryptTTL(encryptedTTLMessage string) string {
jsonMessage := a.Decrypt(encryptedTTLMessage)
if jsonMessage == "" {
return ""
}
var ttlMessage TTLMessage
err := json.Unmarshal([]byte(jsonMessage), &ttlMessage)
if err != nil {
return ""
}
if ttlMessage.TTL >= time.Now().Unix() {
return ttlMessage.Message
}
return ""
}
// Encrypt Encrypt message
func (a *AasaamAES) Encrypt(message string) string {
block, err := aes.NewCipher(a.key)
if err != nil {
panic(err.Error())
}
iv := make([]byte, 12)
_, eio := io.ReadFull(rand.Reader, iv)
if eio != nil {
panic(eio.Error())
}
aes, err := cipher.NewGCM(block)
if err != nil {
panic(err.Error())
}
encrypted := aes.Seal(nil, iv, []byte(message), nil)
data := base64.StdEncoding.EncodeToString(append(iv, encrypted...))
return data
}
// Decrypt Decrypt message
// return Original message or empty string on failure
func (a *AasaamAES) Decrypt(message string) string {
if message == "" {
return ""
}
packet, err := base64.StdEncoding.DecodeString(message)
if err != nil {
return ""
}
iv := packet[:12]
encrypted := packet[12:]
block, err := aes.NewCipher(a.key)
if err != nil {
return ""
}
aes, err := cipher.NewGCM(block)
if err != nil {
return ""
}
plaintext, err := aes.Open(nil, iv, encrypted, nil)
if err != nil {
return ""
}
return string(plaintext)
}