Go 加密算法工具方法

常用的工具方法:

1.将字节数组转成16进制字符串: []byte -> string

2.将16进制字符串串转成字节数组: hex string -> []byte

3.16进制字符串大端和小端颠倒

4.字节数组大端和小端颠倒

5.哈希函数

Go 复制代码
package main

import (
	"fmt"
	"encoding/hex"
	"crypto/md5"
	"crypto/sha1"
	"crypto/sha256"
	"crypto/sha512"
	"hash"
)


/**
* 将字节数组转成16进制字符串串: []byte -> string
*/
func BytesToHexString(arr []byte)string{
	return hex.EncodeToString(arr)
}

/**
* 将16进制字符串串转成字节数组: hex string -> []byte
*/
func HexStringToBytes(s string)([]byte,error){
	arr,err:=hex.DecodeString(s)
	return arr,err
}

/**
* 16进制字符串串⼤大端和⼩小端颠倒
*/
func ReverseHexString(hexStr string)string{
	arr,_:=hex.DecodeString(hexStr)
	ReverseBytes(arr)
	return hex.EncodeToString(arr)
}

/**
* 字节数组⼤大端和⼩小端颠倒
*/
func ReverseBytes(data []byte){
	for i,j:=0,len(data)-1;i<j;i,j=i+1,j-1{
		data[i],data[j] = data[j],data[i]
	}
}

//哈希函数
func SHA256HexString(text string)string{
	sha256Instance:=sha256.New()
	arr,_:=hex.DecodeString(text)
	sha256Instance.Write(arr)
	ciphertext:=sha256Instance.Sum(nil)
	return fmt.Sprintf("%x",ciphertext)
}

func SHA256Double(text string, isHex bool) string {
	sha256Instance := sha256.New()
	
	if isHex {
		arr, _ := hex.DecodeString(text)
		sha256Instance.Write(arr)
	} else {
		sha256Instance.Write([]byte(text))
	}
	
	hashBytes := sha256Instance.Sum(nil)
	sha256Instance.Reset()
	sha256Instance.Write(hashBytes)
	hashBytes = sha256Instance.Sum(nil)
	
	return fmt.Sprintf("%x", hashBytes)
}

func HASH(text string,hashType string,isHex bool)string{
	var hashInstance hash.Hash
	switch hashType{
	//case"md4":
	//	hashInstance = md4.New()
	case"md5":
		hashInstance = md5.New()
	case"sha1":
		hashInstance = sha1.New()
	case"sha256":
		hashInstance = sha256.New()
	case"sha512":
		hashInstance = sha512.New()
	//case"ripemd160":
	//	hashInstance = ripemd160.New()
	}

	if isHex{
		arr,_:=hex.DecodeString(text)
		hashInstance.Write(arr)
	}else{
		hashInstance.Write([]byte(text))
	}

	ciphertext := hashInstance.Sum(nil)
	
	fmt.Println("密文: ",ciphertext)
	return fmt.Sprint("%x",ciphertext)
} 

func main(){
	text := "我喜欢Go语音!"
        fmt.Println("原文:",text)
	//HASH(text,"md4",true)
	HASH(text,"md5",false)
	HASH(text,"sha1",true)
	HASH(text,"sha256",false)
	HASH(text,"sha512",true)
	//HASH(text,"ripemd160",false)
}

go run tools.go
原文: 我喜欢Go语音!
md5 密文:  [79 87 231 27 94 192 19 127 81 189 27 146 112 219 94 70]
sha1 密文:  [218 57 163 238 94 107 75 13 50 85 191 239 149 96 24 144 175 216 7 9]
sha256 密文:  [39 69 16 138 163 59 95 127 162 53 253 71 127 168 89 182 123 62 97 22 150 164 44 59 59 180 246 238 86 117 129 92]
sha512 密文:  [207 131 225 53 126 239 184 189 241 84 40 80 214 109 128 7 214 32 228 5 11 87 21 220 131 244 169 33 211 108 233 206 71 208 209 60 93 133 242 176 255 131 24 210 135 126 236 47 99 185 49 189 71 65 122 129 165 56 50 122 249 39 218 62]

6.填充函数

Go 复制代码
//填充函数
//对称加密需要的填充函数
func PKCS5Padding(data []byte,blockSize int)[]byte{
	padding := blockSize - len(data)%blockSize
	padtext := bytes.Repeat([]byte{byte(padding)},padding)

	return append(data,padtext...)
}

func PKCS5UnPadding(data []byte) []byte {
	length := len(data)
	//去掉最后一个字节 unpadding 次
	unpadding := int(data[length-1])
	return data[:(length - unpadding)]
}

func ZeroPadding(data []byte, blockSize int) []byte {
	padding := blockSize - len(data)%blockSize
	padtext := bytes.Repeat([]byte{0}, padding)
	return append(data, padtext...)
}

func ZeroUnPadding(data []byte) []byte {
	return bytes.TrimRightFunc(data, func(r rune) bool {
		return r == rune(0)
	})
}
相关推荐
长河8 小时前
Java开发者LLM实战——LangChain4j最新版教学知识库实战
java·开发语言
Cyan_RA98 小时前
SpringMVC @RequestMapping的使用演示和细节 详解
java·开发语言·后端·spring·mvc·ssm·springmvc
再见晴天*_*11 小时前
SpringBoot 中单独一个类中运行main方法报错:找不到或无法加载主类
java·开发语言·intellij idea
lqjun082712 小时前
Qt程序单独运行报错问题
开发语言·qt
hdsoft_huge14 小时前
Java & Spring Boot常见异常全解析:原因、危害、处理与防范
java·开发语言·spring boot
风中的微尘14 小时前
39.网络流入门
开发语言·网络·c++·算法
未来之窗软件服务15 小时前
幽冥大陆(二)RDIFSDK 接口文档:布草洗涤厂高效运营的技术桥梁C#—东方仙盟
开发语言·c#·rdif·仙盟创梦ide·东方仙盟
小冯记录编程15 小时前
C++指针陷阱:高效背后的致命危险
开发语言·c++·visual studio
1uther16 小时前
Unity核心概念⑨:Screen
开发语言·游戏·unity·c#·游戏引擎
C_Liu_16 小时前
C++:类和对象(下)
开发语言·c++