Skip to content

Commit 64f99e9

Browse files
committed
Update xutil AESMode
1 parent c731e3a commit 64f99e9

2 files changed

Lines changed: 9 additions & 7 deletions

File tree

‎src/xutil/README.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ go get github.com/mix-go/xutil
2525
| xutil.StructToMap(i interface{}) map[string]interface{} | Convert struct to map. |
2626
| xutil.StringToBytes(s string) []byte | Convert string to bytes (0 copy). |
2727
| xutil.BytesToString(b []byte) string | Convert bytes to bytes (0 copy). |
28-
| xutil.AESEncrypt(plainText, mode, key, iv string) (string, error) | AES encryption. |
29-
| xutil.AESDecrypt(cipherText, mode, key, iv string) (string, error) | AES decryption. |
28+
| xutil.AESEncrypt(plainText string, mode AESMode, key, iv string) (string, error) | AES encryption. |
29+
| xutil.AESDecrypt(cipherText string, mode AESMode, key, iv string) (string, error) | AES decryption. |
3030
| xutil.MD5Hash(b []byte) string | MD5 encryption. |
3131

3232
## License

‎src/xutil/aes.go‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@ import (
88
"errors"
99
)
1010

11+
type AESMode string
12+
1113
const (
12-
AESModeCBC = "CBC"
13-
AESModeCFB = "CFB"
14-
AESModeOFB = "OFB"
14+
AESModeCBC = AESMode("CBC")
15+
AESModeCFB = AESMode("CFB")
16+
AESModeOFB = AESMode("OFB")
1517
)
1618

1719
var ErrUnknownMode = errors.New("unknown mode")
@@ -20,7 +22,7 @@ var ErrUnknownMode = errors.New("unknown mode")
2022
// key128 := "abcdefghijklmnop" // 16 bytes = 128 bits
2123
// key192 := "abcdefghijklmnopqrstuvwx" // 24 bytes = 192 bits
2224
// key256 := "abcdefghijklmnopabcdefghijklmnop" // 32 bytes = 256 bits
23-
func AESEncrypt(plainText, mode, key, iv string) (string, error) {
25+
func AESEncrypt(plainText string, mode AESMode, key, iv string) (string, error) {
2426
block, err := aes.NewCipher([]byte(key))
2527
if err != nil {
2628
return "", err
@@ -46,7 +48,7 @@ func AESEncrypt(plainText, mode, key, iv string) (string, error) {
4648
return base64.StdEncoding.EncodeToString(cipherText), nil
4749
}
4850

49-
func AESDecrypt(cipherText, mode, key, iv string) (string, error) {
51+
func AESDecrypt(cipherText string, mode AESMode, key, iv string) (string, error) {
5052
cipherTextBytes, err := base64.StdEncoding.DecodeString(cipherText)
5153
if err != nil {
5254
return "", err

0 commit comments

Comments
 (0)