1. URL 编码、解码
2. base64 编码、解码
3. hex 编码、解码
4. md5 编码
5. sha-1 编码
6. sha-256 编码
7. sha-512 编码
package mainimport ("crypto/md5""crypto/sha256""crypto/sha512""encoding/base64""encoding/hex""encoding/json""fmt""net/url"
)// URL 编码
func UrlEncode(original string) (encoded string) {encoded = url.QueryEscape(original)return encoded
}// URL 解码
func UrlDencode(original string) (decoded string, err error) {decoded, err = url.QueryUnescape(original)return
}// base64 编码
func Base64Encode(original string) (encoded string) {encoded = base64.StdEncoding.EncodeToString([]byte(original))return encoded
}// base64 解码
func Base64Decode(original string) (decoded string, err error) {dec_str, err := base64.StdEncoding.DecodeString(original)return string(dec_str), err
}// hex 编码
func HexEncode(original string) (encoded string) {encoded = hex.EncodeToString([]byte(original))return encoded
}// hex 解码
func HexDecode(original string) (decoded string, err error) {bytes, err := hex.DecodeString(original)return string(bytes), err
}// md5 编码
func MD5Encode(original string) (encoded string) {// 创建一个新的 MD5 哈希对象hasher := md5.New()// 写入数据hasher.Write([]byte(original))// 计算哈希值hash := hasher.Sum(nil)// 将哈希值转换为十六进制字符串hashString := hex.EncodeToString(hash)return hashString
}// sha1 编码
func Sha1Encode(original string) (encoded string) {// 创建一个新的 SHA-1 哈希对象hasher := sha1.New()// 写入数据hasher.Write([]byte(original))// 计算哈希值hash := hasher.Sum(nil)// 将哈希值转换为十六进制字符串hashString := hex.EncodeToString(hash)return hashString
}// sha256 编码
func Sha256Encode(original string) (encoded string) {// 创建一个新的 SHA-256 哈希对象hasher := sha256.New()// 写入数据hasher.Write([]byte(original))// 计算哈希值hash := hasher.Sum(nil)// 将哈希值转换为十六进制字符串hashString := hex.EncodeToString(hash)return hashString
}// sha512 编码
func Sha512Encode(original string) (encoded string) {// 创建一个新的 SHA-512 哈希对象hasher := sha512.New()// 写入数据hasher.Write([]byte(original))// 计算哈希值hash := hasher.Sum(nil)// 将哈希值转换为十六进制字符串hashString := hex.EncodeToString(hash)return hashString
}func main(){str := "123456+abc"fmt.Println("原始值:", str)// url编码urlEncode := UrlEncode(str)fmt.Println("url编码:", urlEncode)// url解码urlDencode, _ := UrlDencode(urlEncode)fmt.Println("url解码:", urlDencode)// base64编码base64Encode := Base64Encode(str)fmt.Println("base64编码:", base64Encode)// base64解码base64Dencoded, _ := Base64Decode(base64Encode)fmt.Println("base64解码:", base64Dencoded)// hex编码hexEncode := HexEncode(str)fmt.Println("hex编码:", hexEncode)// hex解码hexDencode, _ := HexDecode(hexEncode)fmt.Println("hex解码:", hexDencode)// md5编码fmt.Println("md5编码:", MD5Encode(str))// sha-1编码fmt.Println("sha-1编码:", Sha1Encode(str))// sha-256编码fmt.Println("sha-256编码:", Sha256Encode(str))// sha-512编码fmt.Println("sha-512编码:", Sha512Encode(str))}
输出结果:
原始值: 123456+abc
url编码: 123456%2Babc
url解码: 123456+abc
base64编码: MTIzNDU2K2FiYw==
base64解码: 123456+abc
hex编码: 3132333435362b616263
hex解码: 123456+abc
md5编码: 5e4f407d2a09d76203273bdf4be46815
sha-1编码: a2c6933ae32fbafb9ba9c824b0ae9aa9c86c9191
sha-256编码: b8f9ab210dd328fb4175af2e9dbbe1be36171367d06158ab180fe7d76bb8b1c8
sha-512编码: 9cd87122ad0e5784d51a4689373b03e9b01f0c7d0bff8160fdb1d8d18fd46f94027a56a444904d4b2179c8d3c447330f35126246804b96baff3fa433d658dfec