-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathencryption.go
49 lines (44 loc) · 1.16 KB
/
encryption.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
package main
import (
"crypto/aes"
"crypto/cipher"
"crypto/md5"
"encoding/base64"
)
type Encryption struct {
block cipher.Block
}
func NewEncryption(key []byte) (*Encryption, error) {
e := new(Encryption)
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
e.block = block
return e, nil
}
func (e *Encryption) encrypt(value []byte) ([]byte, error) {
hash := md5.Sum(value)
iv := hash[:]
stream := cipher.NewCTR(e.block, iv)
encrypted := make([]byte, len(value))
stream.XORKeyStream(encrypted, value)
ciphertext := make([]byte, 0, len(encrypted)+len(iv))
ciphertext = append(ciphertext, iv...)
ciphertext = append(ciphertext, encrypted...)
encoded := base64.StdEncoding.EncodeToString(ciphertext)
return []byte(encoded), nil
}
func (e *Encryption) decrypt(value []byte) ([]byte, error) {
decoded, err := base64.StdEncoding.DecodeString(string(value))
if err != nil {
return nil, err
}
encrypted := []byte(decoded)
iv := encrypted[:e.block.BlockSize()]
ciphertext := encrypted[e.block.BlockSize():]
stream := cipher.NewCTR(e.block, iv)
plain := make([]byte, len(ciphertext))
stream.XORKeyStream(plain, ciphertext)
return plain, nil
}