golang 计算文件MD5

bytes 是文件数据流

xalgorithm.MD5ToUpper32(hex.EncodeToString(bytes))

xalgorithm.MD5ToUpper32(hex.EncodeToString(bytes))



package xalgorithm

import (
	"crypto/md5"
	"fmt"
	"io"
	"strings"
)

/*
MD5ToUpper32 将字符串,转为32位md5加密,返回大写字母
*/
func MD5ToUpper32(str string) string {
	w := md5.New()
	io.WriteString(w, str)                  //将str写入到w中
	md5Str := fmt.Sprintf("%x", w.Sum(nil)) //w.Sum(nil)将w的hash转成[]byte格式
	return strings.ToUpper(md5Str)
}

/*
MD5ToLower32 将字符串,转为32位md5加密,返回小写字母
*/
func MD5ToLower32(str string) string {
	w := md5.New()
	io.WriteString(w, str)                  //将str写入到w中
	md5Str := fmt.Sprintf("%x", w.Sum(nil)) //w.Sum(nil)将w的hash转成[]byte格式
	return md5Str
}

你可能感兴趣的:(Golang,golang,开发语言,后端)