| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- package utils
- import (
- "crypto/aes"
- "crypto/cipher"
- "crypto/md5"
- "encoding/base64"
- "encoding/hex"
- "net/url"
- )
- func PKCS7UnPadding(origData []byte) []byte {
- length := len(origData)
- unpadding := int(origData[length-1])
- return origData[:(length - unpadding)]
- }
- func AesCBCDecrypt(encryptData, key, iv []byte) ([]byte, error) {
- block, err := aes.NewCipher(key)
- if err != nil {
- panic(err)
- }
- blockSize := block.BlockSize()
- if len(encryptData) < blockSize {
- panic("ciphertext too short")
- }
- if len(encryptData)%blockSize != 0 {
- panic("ciphertext is not a multiple of the block size")
- }
- mode := cipher.NewCBCDecrypter(block, iv)
- decryptedData := make([]byte, len(encryptData))
- mode.CryptBlocks(decryptedData, encryptData)
- decryptedData = PKCS7UnPadding(decryptedData)
- return decryptedData, nil
- }
- func Md5(str string) string {
- h := md5.New()
- h.Write([]byte(str))
- return hex.EncodeToString(h.Sum(nil))
- }
- func Base64Encode(str string) string {
- return base64.StdEncoding.EncodeToString([]byte(str))
- }
- func Base64Decode(str string) string {
- decodestr, _ := base64.StdEncoding.DecodeString(str)
- return string(decodestr)
- }
- func UrlEncode(str string) (string, error) {
- u, err := url.Parse(str)
- if err != nil {
- return "", err
- }
- return u.String(), nil
- }
|