forked from xinliangnote/go-gin-api
-
Notifications
You must be signed in to change notification settings - Fork 0
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
1 parent
155f5fc
commit 34f85ad
Showing
12 changed files
with
345 additions
and
118 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 was deleted.
Oops, something went wrong.
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,80 @@ | ||
package aes | ||
|
||
import ( | ||
"bytes" | ||
cryptoAes "crypto/aes" | ||
"crypto/cipher" | ||
"encoding/base64" | ||
) | ||
|
||
var _ Aes = (*aes)(nil) | ||
|
||
type Aes interface { | ||
i() | ||
// Encrypt 加密 | ||
Encrypt(encryptStr string) (string, error) | ||
|
||
// Decrypt 解密 | ||
Decrypt(decryptStr string) (string, error) | ||
} | ||
|
||
type aes struct { | ||
key string | ||
iv string | ||
} | ||
|
||
func New(key, iv string) Aes { | ||
return &aes{ | ||
key: key, | ||
iv: iv, | ||
} | ||
} | ||
|
||
func (a *aes) i() {} | ||
|
||
func (a *aes) Encrypt(encryptStr string) (string, error) { | ||
encryptBytes := []byte(encryptStr) | ||
block, err := cryptoAes.NewCipher([]byte(a.key)) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
blockSize := block.BlockSize() | ||
encryptBytes = pkcs5Padding(encryptBytes, blockSize) | ||
|
||
blockMode := cipher.NewCBCEncrypter(block, []byte(a.iv)) | ||
encrypted := make([]byte, len(encryptBytes)) | ||
blockMode.CryptBlocks(encrypted, encryptBytes) | ||
return base64.URLEncoding.EncodeToString(encrypted), nil | ||
} | ||
|
||
func (a *aes) Decrypt(decryptStr string) (string, error) { | ||
decryptBytes, err := base64.URLEncoding.DecodeString(decryptStr) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
block, err := cryptoAes.NewCipher([]byte(a.key)) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
blockMode := cipher.NewCBCDecrypter(block, []byte(a.iv)) | ||
decrypted := make([]byte, len(decryptBytes)) | ||
|
||
blockMode.CryptBlocks(decrypted, decryptBytes) | ||
decrypted = pkcs5UnPadding(decrypted) | ||
return string(decrypted), nil | ||
} | ||
|
||
func pkcs5Padding(cipherText []byte, blockSize int) []byte { | ||
padding := blockSize - len(cipherText)%blockSize | ||
padText := bytes.Repeat([]byte{byte(padding)}, padding) | ||
return append(cipherText, padText...) | ||
} | ||
|
||
func pkcs5UnPadding(decrypted []byte) []byte { | ||
length := len(decrypted) | ||
unPadding := int(decrypted[length-1]) | ||
return decrypted[:(length - unPadding)] | ||
} |
Oops, something went wrong.