以下Golang代码的加密结果与Java语言结果一致,需要注意结果大小写问题。
仅供参考,拿去不谢!
package secret
import (
"appback/src/logger"
"bytes"
"crypto/aes"
"crypto/cipher"
"crypto/hmac"
"crypto/md5"
"crypto/rand"
"crypto/rsa"
"crypto/sha1"
"crypto/sha256"
"crypto/sha512"
"crypto/x509"
"encoding/base64"
"encoding/hex"
"encoding/pem"
"fmt"
"strings"
)
// md5验证
func MD5Str(src string) string {
h := md5.New()
h.Write([]byte(src)) // 需要加密的字符串为
// fmt.Printf("%s\n", hex.EncodeToString(h.Sum(nil))) // 输出加密结果
return hex.EncodeToString(h.Sum(nil))
}
// hmacsha256验证
func HMAC_SHA256(src, key string) string {
m := hmac.New(sha256.New, []byte(key))
m.Write([]byte(src))
return hex.EncodeToString(m.Sum(nil))
}
// hmacsha512验证
func HMAC_SHA512(src, key string) string {
m := hmac.New(sha512.New, []byte(key))
m.Write([]byte(src))
return hex.EncodeToString(m.Sum(nil))
}
func HMAC_SHA1(src, key string) string {
m := hmac.New(sha1.New, []byte(key))
m.Write([]byte(src))
return hex.EncodeToString(m.Sum(nil))
}
// sha256验证
func SHA256Str(src string) string {
h := sha256.New()
h.Write([]byte(src)) // 需要加密的字符串为
// fmt.Printf("%s\n", hex.EncodeToString(h.Sum(nil))) // 输出加密结果
return hex.EncodeToString(h.Sum(nil))
}
// sha512验证
func SHA512Str(src string) string {
h := sha512.New()
h.Write([]byte(src)) // 需要加密的字符串为
// fmt.Printf("%s\n", hex.EncodeToString(h.Sum(nil))) // 输出加密结果
return hex.EncodeToString(h.Sum(nil))
}
// base编码
func BASE64EncodeStr(src string) string {
return string(base64.StdEncoding.EncodeToString([]byte(src)))
}
// base解码
func BASE64DecodeStr(src string) string {
a, err := base64.StdEncoding.DecodeString(src)
if err != nil {
return ""
}
return string(a)
}
var ivspec = []byte("0000000000000000")
func AESEncodeStr(src, key string) string {
block, err := aes.NewCipher([]byte(key))
if err != nil {
fmt.Println("key error1", err)
}
if src == "" {
fmt.Println("plain content empty")
}
ecb := cipher.NewCBCEncrypter(block, ivspec)
content := []byte(src)
content = PKCS5Padding(content, block.BlockSize())
crypted := make([]byte, len(content))
ecb.CryptBlocks(crypted, content)
return hex.EncodeToString(crypted)
}
func AESDecodeStr(crypt, key string) string {
crypted, err := hex.DecodeString(strings.ToLower(crypt))
if err != nil || len(crypted) == 0 {
fmt.Println("plain content empty")
}
block, err := aes.NewCipher([]byte(key))
if err != nil {
fmt.Println("key error1", err)
}
ecb := cipher.NewCBCDecrypter(block, ivspec)
decrypted := make([]byte, len(crypted))
ecb.CryptBlocks(decrypted, crypted)
return string(PKCS5Trimming(decrypted))
}
func PKCS5Padding(ciphertext []byte, blockSize int) []byte {
padding := blockSize - len(ciphertext)%blockSize
padtext := bytes.Repeat([]byte{byte(padding)}, padding)
return append(ciphertext, padtext...)
}
func PKCS5Trimming(encrypt []byte) []byte {
padding := encrypt[len(encrypt)-1]
return encrypt[:len(encrypt)-int(padding)]
}
func RsaEncrypt(src, key string) string {
block, _ := pem.Decode([]byte(key))
if block == nil {
return ""
}
pubInterface, err := x509.ParsePKIXPublicKey(block.Bytes)
if err != nil {
logger.SysLogger.Err(err.Error())
return ""
}
pub := pubInterface.(*rsa.PublicKey)
crypted, err := rsa.EncryptPKCS1v15(rand.Reader, pub, []byte(src))
if err != nil {
logger.SysLogger.Err(err.Error())
return ""
}
return hex.EncodeToString(crypted)
}