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)
	})
}
相关推荐
踢足球09296 小时前
寒假打卡:2026-2-7
java·开发语言·javascript
薛定谔的猫喵喵7 小时前
基于PyQt5的视频答题竞赛系统设计与实现
开发语言·qt·音视频
岱宗夫up7 小时前
Python 数据分析入门
开发语言·python·数据分析
码界筑梦坊7 小时前
325-基于Python的校园卡消费行为数据可视化分析系统
开发语言·python·信息可视化·django·毕业设计
多恩Stone7 小时前
【RoPE】Flux 中的 Image Tokenization
开发语言·人工智能·python
李日灐7 小时前
C++进阶必备:红黑树从 0 到 1: 手撕底层,带你搞懂平衡二叉树的平衡逻辑与黑高检验
开发语言·数据结构·c++·后端·面试·红黑树·自平衡二叉搜索树
Risehuxyc7 小时前
备份三个PHP程序
android·开发语言·php
lly2024067 小时前
PHP Error: 常见错误及其解决方法
开发语言
网安墨雨7 小时前
Python自动化一------pytes与allure结合生成测试报告
开发语言·自动化测试·软件测试·python·职场和发展·自动化
毕设源码李师姐7 小时前
计算机毕设 java 基于 java 的图书馆借阅系统 智能图书馆借阅综合管理平台 基于 Java 的图书借阅与信息管理系统
java·开发语言·课程设计