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)
	})
}
相关推荐
Dream it possible!31 分钟前
LeetCode 热题 100_寻找重复数(100_287_中等_C++)(技巧)(暴力解法;哈希集合;二分查找)
c++·leetcode·哈希算法
BUG制造机.42 分钟前
Go 语言的 GMP 模型
golang
张帅涛_6661 小时前
golang读、写、复制、创建目录、删除、重命名,文件方法总结
golang
运维-大白同学1 小时前
go-数据库基本操作
开发语言·数据库·golang
你怎么知道我是队长1 小时前
GO语言语法---if语句
golang
张帅涛_6661 小时前
golang中的反射示例
golang
动感光博1 小时前
Unity(URP渲染管线)的后处理、动画制作、虚拟相机(Virtual Camera)
开发语言·人工智能·计算机视觉·unity·c#·游戏引擎
蚰蜒螟2 小时前
深入解析JVM字节码解释器执行流程(OpenJDK 17源码实现)
开发语言·jvm·python
keke102 小时前
Java【14_2】接口(Comparable和Comparator)、内部类
java·开发语言
思茂信息2 小时前
CST软件对OPERA&CST软件联合仿真汽车无线充电站对人体的影响
c语言·开发语言·人工智能·matlab·汽车·软件构建