generated from NdoleStudio/go-http-client
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcipher_service.go
66 lines (53 loc) · 1.83 KB
/
cipher_service.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
package httpsms
import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"crypto/sha256"
"encoding/base64"
"errors"
)
// CipherService is used to encrypt and decrypt SMS messages using the AES-256 algorithm
type CipherService service
// Encrypt the message content using the encryption key
func (service *CipherService) Encrypt(encryptionKey string, content string) (string, error) {
block, err := aes.NewCipher(service.hash(encryptionKey))
if err != nil {
return "", errors.Join(err, errors.New("failed to create new cipher"))
}
text := []byte(content)
iv, err := service.initializationVector()
if err != nil {
return "", errors.Join(err, errors.New("failed to create initialization vector"))
}
stream := cipher.NewCFBEncrypter(block, iv)
cipherText := make([]byte, len(text))
stream.XORKeyStream(cipherText, text)
return base64.StdEncoding.EncodeToString(append(iv, cipherText...)), nil
}
// Decrypt the message content using the encryption key
func (service *CipherService) Decrypt(encryptionKey string, cipherText string) (string, error) {
content, err := base64.StdEncoding.DecodeString(cipherText)
if err != nil {
return "", errors.Join(err, errors.New("failed to decode cipher in base64"))
}
block, err := aes.NewCipher(service.hash(encryptionKey))
if err != nil {
return "", errors.Join(err, errors.New("failed to create new cipher"))
}
// Decrypt the message
cipherTextBytes := content[16:]
stream := cipher.NewCFBDecrypter(block, content[:16])
stream.XORKeyStream(cipherTextBytes, cipherTextBytes)
return string(cipherTextBytes), nil
}
// hash a key using the SHA-256 algorithm
func (service *CipherService) hash(key string) []byte {
sha := sha256.Sum256([]byte(key))
return sha[:]
}
func (service *CipherService) initializationVector() ([]byte, error) {
iv := make([]byte, 16)
_, err := rand.Read(iv)
return iv, err
}