123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- package encoding
- import (
- "bytes"
- "crypto/aes"
- "crypto/cipher"
- "crypto/md5"
- "crypto/sha1"
- "crypto/sha256"
- "encoding/base64"
- "encoding/hex"
- "fmt"
- "git.sxidc.com/go-tools/utils/strutils"
- )
- func AESEncrypt(originData string, key string) (string, error) {
- if strutils.IsStringEmpty(originData) {
- return "", nil
- }
- if strutils.IsStringEmpty(key) {
- return originData, nil
- }
- keyBytes := []byte(key)
- block, err := aes.NewCipher(keyBytes)
- if err != nil {
- return "", err
- }
- originDataBytes := []byte(originData)
- blockSize := block.BlockSize()
- originDataBytes = pkcs7Padding(originDataBytes, blockSize)
- blockMode := cipher.NewCBCEncrypter(block, keyBytes[:blockSize])
- encrypted := make([]byte, len(originDataBytes))
- blockMode.CryptBlocks(encrypted, originDataBytes)
- return base64.StdEncoding.EncodeToString(encrypted), nil
- }
- func AESDecrypt(encrypted string, key string) (string, error) {
- if strutils.IsStringEmpty(encrypted) {
- return "", nil
- }
- if strutils.IsStringEmpty(key) {
- return encrypted, nil
- }
- keyBytes := []byte(key)
- block, err := aes.NewCipher(keyBytes)
- if err != nil {
- return "", err
- }
- decoded, err := base64.StdEncoding.DecodeString(encrypted)
- if err != nil {
- return "", err
- }
- blockSize := block.BlockSize()
- blockMode := cipher.NewCBCDecrypter(block, keyBytes[:blockSize])
- originData := make([]byte, len(decoded))
- blockMode.CryptBlocks(originData, decoded)
- originData = pkcs7UnPadding(originData)
- return string(originData), nil
- }
- func MD5(origin string, salt string) string {
- if salt != "" {
- origin = origin + salt
- }
- return fmt.Sprintf("%x", md5.New().Sum([]byte(origin)))
- }
- func SHA256(origin string, salt string) string {
- if salt != "" {
- origin = origin + salt
- }
- return fmt.Sprintf("%x", sha256.Sum256([]byte(origin)))
- }
- func SHA1(origin string, salt string) string {
- if salt != "" {
- origin = origin + salt
- }
- o := sha1.New()
- o.Write([]byte(origin))
- return hex.EncodeToString(o.Sum(nil))
- }
- func Base64Encode(content []byte) string {
- return base64.StdEncoding.EncodeToString(content)
- }
- func Base64Decode(content string) ([]byte, error) {
- return base64.StdEncoding.DecodeString(content)
- }
- func pkcs7Padding(ciphertext []byte, blockSize int) []byte {
- padding := blockSize - len(ciphertext)%blockSize
- padText := bytes.Repeat([]byte{byte(padding)}, padding)
- return append(ciphertext, padText...)
- }
- func pkcs7UnPadding(originData []byte) []byte {
- length := len(originData)
- unPadding := int(originData[length-1])
- return originData[:(length - unPadding)]
- }
|