@@ -10,6 +10,7 @@ import (
1010 "encoding/base64"
1111 "encoding/hex"
1212 "errors"
13+ "fmt"
1314 "io"
1415
1516 "github.com/minio/sha256-simd"
@@ -19,13 +20,13 @@ import (
1920func AesEncrypt (key , text []byte ) ([]byte , error ) {
2021 block , err := aes .NewCipher (key )
2122 if err != nil {
22- return nil , err
23+ return nil , fmt . Errorf ( "AesEncrypt invalid key: %v" , err )
2324 }
2425 b := base64 .StdEncoding .EncodeToString (text )
2526 ciphertext := make ([]byte , aes .BlockSize + len (b ))
2627 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 )
2930 }
3031 cfb := cipher .NewCFBEncrypter (block , iv )
3132 cfb .XORKeyStream (ciphertext [aes .BlockSize :], []byte (b ))
@@ -39,15 +40,15 @@ func AesDecrypt(key, text []byte) ([]byte, error) {
3940 return nil , err
4041 }
4142 if len (text ) < aes .BlockSize {
42- return nil , errors .New ("ciphertext too short" )
43+ return nil , errors .New ("AesDecrypt ciphertext too short" )
4344 }
4445 iv := text [:aes .BlockSize ]
4546 text = text [aes .BlockSize :]
4647 cfb := cipher .NewCFBDecrypter (block , iv )
4748 cfb .XORKeyStream (text , text )
4849 data , err := base64 .StdEncoding .DecodeString (string (text ))
4950 if err != nil {
50- return nil , err
51+ return nil , fmt . Errorf ( "AesDecrypt invalid decrtyped base64 string: %w" , err )
5152 }
5253 return data , nil
5354}
@@ -58,21 +59,21 @@ func EncryptSecret(key, str string) (string, error) {
5859 plaintext := []byte (str )
5960 ciphertext , err := AesEncrypt (keyHash [:], plaintext )
6061 if err != nil {
61- return "" , err
62+ return "" , fmt . Errorf ( "failed to encrypt by secret: %w" , err )
6263 }
6364 return hex .EncodeToString (ciphertext ), nil
6465}
6566
6667// DecryptSecret decrypts a previously encrypted hex string
67- func DecryptSecret (key , cipherhex string ) (string , error ) {
68+ func DecryptSecret (key , cipherHex string ) (string , error ) {
6869 keyHash := sha256 .Sum256 ([]byte (key ))
69- ciphertext , err := hex .DecodeString (cipherhex )
70+ ciphertext , err := hex .DecodeString (cipherHex )
7071 if err != nil {
71- return "" , err
72+ return "" , fmt . Errorf ( "failed to decrtyp by secret, invalid hex string: %w" , err )
7273 }
7374 plaintext , err := AesDecrypt (keyHash [:], ciphertext )
7475 if err != nil {
75- return "" , err
76+ return "" , fmt . Errorf ( "failed to decrtyp by secret, secret key (SECRET_KEY) might be incorrect: %w" , err )
7677 }
7778 return string (plaintext ), nil
7879}
0 commit comments