package main

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

func gethash(path string) (hash string) {
	file, err := os.Open(path)
	if err == nil {
		h_ob := sha256.New()
		_, err := io.Copy(h_ob, file)
		if err == nil {
			hash := h_ob.Sum(nil)
			hashvalue := hex.EncodeToString(hash)
			return hashvalue
		} else {
			return "something wrong when use sha256 interface..."
		}
	} else {
		fmt.Printf("failed to open %s\n", path)
	}
	defer file.Close()
	return
}
func main() {
	path := "test"
	//path:="md5.go"
	hash := gethash(path)
	fmt.Printf("%s hash: %s", path, hash)
}

//test hash: 599593e4bd8f877acf8f00805e52eb0ffac4c662bc65349bf9eb3e3c9871a2bb

  

相关文章:

  • 2021-11-25
  • 2022-03-07
  • 2022-12-23
  • 2021-05-28
  • 2022-12-28
  • 2021-09-27
  • 2021-04-20
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-28
  • 2021-08-29
  • 2021-06-12
相关资源
相似解决方案