码迷,mamicode.com
首页 > 编程语言 > 详细

go语言练习:文件哈希

时间:2018-05-18 22:51:59      阅读:212      评论:0      收藏:0      [点我收藏+]

标签:fail   div   encoding   pac   import   语言   str   ==   nil   

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

  

go语言练习:文件哈希

标签:fail   div   encoding   pac   import   语言   str   ==   nil   

原文地址:https://www.cnblogs.com/chenadong/p/9058259.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!