go语言加密大全(md5、HmacSHA256、获取uuid、时间戳)

目录

一、MD5加解密

go 复制代码
import (
	"crypto/md5"
	"encoding/hex"
)

func md5Encrypt(txt string) string {
	m5 := md5.New()
	m5.Write([]byte(txt))
	txtHash := hex.EncodeToString(m5.Sum(nil))
	return txtHash
}

二、HmacSHA256加解密

go 复制代码
import (
	"crypto/hmac"
	"crypto/sha256"
	"encoding/base64"
)

func HmacSha256ToBase64(key string, data string) string {
	mac := hmac.New(sha256.New, []byte(key))
	_, _ = mac.Write([]byte(data))
	encode := mac.Sum(nil)
	return base64.StdEncoding.EncodeToString(encode)
}

三、获取uuid、时间戳

go 复制代码
import (
	"github.com/google/uuid"
	"strconv"
	"time"
)
func main() {
	now := time.Now()
	time_1 := strconv.FormatInt(now.Unix(), 10)      // 10位时间戳
	time_2 := strconv.FormatInt(now.UnixMilli(), 10) // 13位时间戳
	println(time_1)
	println(time_2)
	println(uuid.New().String())
}
相关推荐
程序员爱钓鱼2 小时前
Go语言实战案例 — 项目实战篇:简易博客系统(支持评论)
前端·后端·go
追逐时光者8 小时前
精选 4 款基于 .NET 开源、功能强大的 Windows 系统优化工具
后端·.net
TF男孩8 小时前
ARQ:一款低成本的消息队列,实现每秒万级吞吐
后端·python·消息队列
AAA修煤气灶刘哥10 小时前
别让Redis「歪脖子」!一次搞定数据倾斜与请求倾斜的捉妖记
redis·分布式·后端
AAA修煤气灶刘哥10 小时前
后端人速藏!数据库PD建模避坑指南
数据库·后端·mysql
你的人类朋友10 小时前
什么是API签名?
前端·后端·安全
昵称为空C12 小时前
SpringBoot3 http接口调用新方式RestClient + @HttpExchange像使用Feign一样调用
spring boot·后端
架构师沉默13 小时前
设计多租户 SaaS 系统,如何做到数据隔离 & 资源配额?
java·后端·架构
RoyLin13 小时前
TypeScript设计模式:适配器模式
前端·后端·node.js
该用户已不存在13 小时前
Mojo vs Python vs Rust: 2025年搞AI,该学哪个?
后端·python·rust