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)
	})
}
相关推荐
花酒锄作田4 天前
Gin 框架中的规范响应格式设计与实现
golang·gin
郑州光合科技余经理4 天前
代码展示:PHP搭建海外版外卖系统源码解析
java·开发语言·前端·后端·系统架构·uni-app·php
feifeigo1234 天前
matlab画图工具
开发语言·matlab
dustcell.4 天前
haproxy七层代理
java·开发语言·前端
norlan_jame4 天前
C-PHY与D-PHY差异
c语言·开发语言
多恩Stone4 天前
【C++入门扫盲1】C++ 与 Python:类型、编译器/解释器与 CPU 的关系
开发语言·c++·人工智能·python·算法·3d·aigc
QQ4022054964 天前
Python+django+vue3预制菜半成品配菜平台
开发语言·python·django
遥遥江上月4 天前
Node.js + Stagehand + Python 部署
开发语言·python·node.js
m0_531237174 天前
C语言-数组练习进阶
c语言·开发语言·算法
Railshiqian4 天前
给android源码下的模拟器添加两个后排屏的修改
android·开发语言·javascript