12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- package crypt
- import (
- "bytes"
- "crypto/aes"
- "crypto/cipher"
- "crypto/md5"
- "encoding/hex"
- "errors"
- "math/rand"
- "time"
- )
- //en
- func AesEncrypt(origData, key []byte) ([]byte, error) {
- block, err := aes.NewCipher(key)
- if err != nil {
- return nil, err
- }
- blockSize := block.BlockSize()
- origData = PKCS5Padding(origData, blockSize)
- blockMode := cipher.NewCBCEncrypter(block, key[:blockSize])
- crypted := make([]byte, len(origData))
- blockMode.CryptBlocks(crypted, origData)
- return crypted, nil
- }
- //de
- func AesDecrypt(crypted, key []byte) ([]byte, error) {
- block, err := aes.NewCipher(key)
- if err != nil {
- return nil, err
- }
- blockSize := block.BlockSize()
- blockMode := cipher.NewCBCDecrypter(block, key[:blockSize])
- origData := make([]byte, len(crypted))
- blockMode.CryptBlocks(origData, crypted)
- err, origData = PKCS5UnPadding(origData)
- return origData, err
- }
- //Completion when the length is insufficient
- func PKCS5Padding(ciphertext []byte, blockSize int) []byte {
- padding := blockSize - len(ciphertext)%blockSize
- padtext := bytes.Repeat([]byte{byte(padding)}, padding)
- return append(ciphertext, padtext...)
- }
- //Remove excess
- func PKCS5UnPadding(origData []byte) (error, []byte) {
- length := len(origData)
- unpadding := int(origData[length-1])
- if (length - unpadding) < 0 {
- return errors.New("len error"), nil
- }
- return nil, origData[:(length - unpadding)]
- }
- //Generate 32-bit MD5 strings
- func Md5(s string) string {
- h := md5.New()
- h.Write([]byte(s))
- return hex.EncodeToString(h.Sum(nil))
- }
- //Generating Random Verification Key
- func GetRandomString(l int) string {
- str := "0123456789abcdefghijklmnopqrstuvwxyz"
- bytes := []byte(str)
- result := []byte{}
- r := rand.New(rand.NewSource(time.Now().UnixNano()))
- for i := 0; i < l; i++ {
- result = append(result, bytes[r.Intn(len(bytes))])
- }
- return string(result)
- }
|