计算文件sha256

package main

import (
	"os"
	"crypto/sha256"
	"io"
	"encoding/hex"
	"fmt"
)

func sha256str(filepath string) (string,error) {
	file,err:=os.Open(filepath)
	if err!=nil {
		return "",err
	}
	defer file.Close()
	mhash:=sha256.New()
	_,err=io.Copy(mhash,file)
	if err!=nil {
		return "",err
	}
	y:=mhash.Sum(nil)
	return hex.EncodeToString(y),nil
}

func main()  {
	str,err:=sha256str("/Users/zmx/181112")
	if err!=nil {
		fmt.Print(err)
		return
	}
	fmt.Print(str)
}

猜你喜欢

转载自blog.csdn.net/baidu_25845567/article/details/84102114
今日推荐