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)
	})
}
相关推荐
Elias不吃糖5 小时前
Java Lambda 表达式
java·开发语言·学习
guygg885 小时前
一级倒立摆MATLAB仿真程序
开发语言·matlab
情缘晓梦.6 小时前
C语言指针进阶
java·开发语言·算法
世转神风-6 小时前
qt-字符串版本与数值版本互转
开发语言·qt
极客代码6 小时前
深入解析C语言中的函数指针:原理、规则与实践
c语言·开发语言·指针·状态机·函数·函数指针
w-w0w-w7 小时前
C++模板参数与特化全解析
开发语言·c++
不绝1917 小时前
C#核心:继承
开发语言·c#
AI即插即用8 小时前
即插即用系列(代码实践)专栏介绍
开发语言·人工智能·深度学习·计算机视觉
码农水水8 小时前
蚂蚁Java面试被问:混沌工程在分布式系统中的应用
java·linux·开发语言·面试·职场和发展·php