@@ -10,6 +10,7 @@ import (
10
10
"encoding/base64"
11
11
"encoding/hex"
12
12
"errors"
13
+ "fmt"
13
14
"io"
14
15
15
16
"github.com/minio/sha256-simd"
@@ -19,13 +20,13 @@ import (
19
20
func AesEncrypt (key , text []byte ) ([]byte , error ) {
20
21
block , err := aes .NewCipher (key )
21
22
if err != nil {
22
- return nil , err
23
+ return nil , fmt . Errorf ( "AesEncrypt invalid key: %v" , err )
23
24
}
24
25
b := base64 .StdEncoding .EncodeToString (text )
25
26
ciphertext := make ([]byte , aes .BlockSize + len (b ))
26
27
iv := ciphertext [:aes .BlockSize ]
27
- if _ , err : = io .ReadFull (rand .Reader , iv ); err != nil {
28
- return nil , err
28
+ if _ , err = io .ReadFull (rand .Reader , iv ); err != nil {
29
+ return nil , fmt . Errorf ( "AesEncrypt unable to read iv: %w" , err )
29
30
}
30
31
cfb := cipher .NewCFBEncrypter (block , iv )
31
32
cfb .XORKeyStream (ciphertext [aes .BlockSize :], []byte (b ))
@@ -39,15 +40,15 @@ func AesDecrypt(key, text []byte) ([]byte, error) {
39
40
return nil , err
40
41
}
41
42
if len (text ) < aes .BlockSize {
42
- return nil , errors .New ("ciphertext too short" )
43
+ return nil , errors .New ("AesDecrypt ciphertext too short" )
43
44
}
44
45
iv := text [:aes .BlockSize ]
45
46
text = text [aes .BlockSize :]
46
47
cfb := cipher .NewCFBDecrypter (block , iv )
47
48
cfb .XORKeyStream (text , text )
48
49
data , err := base64 .StdEncoding .DecodeString (string (text ))
49
50
if err != nil {
50
- return nil , err
51
+ return nil , fmt . Errorf ( "AesDecrypt invalid decrtyped base64 string: %w" , err )
51
52
}
52
53
return data , nil
53
54
}
@@ -58,21 +59,21 @@ func EncryptSecret(key, str string) (string, error) {
58
59
plaintext := []byte (str )
59
60
ciphertext , err := AesEncrypt (keyHash [:], plaintext )
60
61
if err != nil {
61
- return "" , err
62
+ return "" , fmt . Errorf ( "failed to encrypt by secret: %w" , err )
62
63
}
63
64
return hex .EncodeToString (ciphertext ), nil
64
65
}
65
66
66
67
// DecryptSecret decrypts a previously encrypted hex string
67
- func DecryptSecret (key , cipherhex string ) (string , error ) {
68
+ func DecryptSecret (key , cipherHex string ) (string , error ) {
68
69
keyHash := sha256 .Sum256 ([]byte (key ))
69
- ciphertext , err := hex .DecodeString (cipherhex )
70
+ ciphertext , err := hex .DecodeString (cipherHex )
70
71
if err != nil {
71
- return "" , err
72
+ return "" , fmt . Errorf ( "failed to decrtyp by secret, invalid hex string: %w" , err )
72
73
}
73
74
plaintext , err := AesDecrypt (keyHash [:], ciphertext )
74
75
if err != nil {
75
- return "" , err
76
+ return "" , fmt . Errorf ( "failed to decrtyp by secret, secret key (SECRET_KEY) might be incorrect: %w" , err )
76
77
}
77
78
return string (plaintext ), nil
78
79
}
0 commit comments