-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
86 changed files
with
4,104 additions
and
304 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package utils | ||
|
||
import ( | ||
"crypto/aes" | ||
"crypto/cipher" | ||
"crypto/rand" | ||
"encoding/base64" | ||
"errors" | ||
"io" | ||
) | ||
|
||
// AesEncode aes加密 | ||
// | ||
// Deprecated: AesEncode is deprecated,it will be removed in the future. | ||
// | ||
// Please use Aes().Encode() instead. | ||
// | ||
// 使用CFB | ||
// | ||
// key应该是一个16或24或32位长度的字符 | ||
func AesEncode(rawString, key string) (string, error) { | ||
plainText := []byte(rawString) | ||
|
||
block, err := aes.NewCipher([]byte(key)) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
cipherText := make([]byte, aes.BlockSize+len(plainText)) | ||
iv := cipherText[:aes.BlockSize] | ||
if _, err := io.ReadFull(rand.Reader, iv); err != nil { | ||
return "", err | ||
} | ||
|
||
stream := cipher.NewCFBEncrypter(block, iv) | ||
stream.XORKeyStream(cipherText[aes.BlockSize:], plainText) | ||
|
||
return base64.StdEncoding.EncodeToString(cipherText), nil | ||
|
||
} | ||
|
||
// AesDecode aes解密 | ||
// | ||
// Deprecated: AesDecode is deprecated,it will be removed in the future. | ||
// | ||
// Please use Aes().Decode() instead. | ||
// | ||
// 使用CFB | ||
// | ||
// key应该是一个16或24或32位长度的字符 | ||
func AesDecode(encryptedString, key string) (string, error) { | ||
cipherText, err := base64.StdEncoding.DecodeString(encryptedString) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
block, err := aes.NewCipher([]byte(key)) | ||
if err != nil { | ||
return "", nil | ||
} | ||
|
||
if len(cipherText) < aes.BlockSize { | ||
return "", errors.New("CipherText block size is too short") | ||
} | ||
|
||
iv := cipherText[:aes.BlockSize] | ||
cipherText = cipherText[aes.BlockSize:] | ||
|
||
stream := cipher.NewCFBDecrypter(block, iv) | ||
stream.XORKeyStream(cipherText, cipherText) | ||
|
||
return string(cipherText), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package utils | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
var rawStringDeprecated = "hello world!" | ||
|
||
// KeyForTestDeprecated 测试密钥 | ||
const KeyForTestDeprecated = "fakeKeyChangeMe!" | ||
|
||
func TestAesEncode(t *testing.T) { | ||
encodedString, err := AesEncode(rawStringDeprecated, KeyForTestDeprecated) | ||
t.Log(encodedString) | ||
assert.NoError(t, err) | ||
} | ||
|
||
func TestAesDecode(t *testing.T) { | ||
encodedString, err := AesEncode(rawStringDeprecated, KeyForTestDeprecated) | ||
t.Log(encodedString) | ||
assert.NoError(t, err) | ||
decodedString, err := AesDecode(encodedString, KeyForTestDeprecated) | ||
t.Log(decodedString) | ||
assert.NoError(t, err) | ||
assert.Equal(t, decodedString, rawStringDeprecated) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package utils | ||
|
||
import "encoding/base64" | ||
|
||
// Base64Encode base64编码 | ||
// | ||
// Deprecated: Base64Encode is deprecated,it will be removed in the future. | ||
// | ||
// Please use Base64().Encode() instead. | ||
func Base64Encode(rawBytes []byte) string { | ||
return base64.StdEncoding.EncodeToString(rawBytes) | ||
} | ||
|
||
// Base64Decode base64解码 | ||
// | ||
// Deprecated: Base64Decode is deprecated,it will be removed in the future. | ||
// | ||
// Please use Base64().Decode() instead. | ||
func Base64Decode(encodedString string) ([]byte, error) { | ||
return base64.StdEncoding.DecodeString(encodedString) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package utils | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestBase64Encode(t *testing.T) { | ||
encodedString := Base64Encode([]byte(rawString)) | ||
t.Log(encodedString) | ||
assert.Equal(t, "aGVsbG8gd29ybGQh", encodedString) | ||
} | ||
|
||
func TestBase64Decode(t *testing.T) { | ||
encodedString := Base64Encode([]byte(rawString)) | ||
|
||
decodeBytes, err := Base64Decode(encodedString) | ||
t.Log(rawString, encodedString, string(decodeBytes)) | ||
assert.NoError(t, err) | ||
assert.Equal(t, string(decodeBytes), rawString) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.