首页 文章资讯内容详情

golang中的检验hash

2026-06-01 2 花语

本文内容纲要:

1.对字符串进行hash

大家可以看一下,SHA1Hashes

GobyExample写道:

Thepatternforgeneratingahashissha1.New(),sha1.Write(bytes),thensha1.Sum([]byte{}).

附上golang代码

packagemain import( "crypto/sha1" "fmt" ) funcmain(){ s:="sha1thisstring" h:=sha1.New() h.Write([]byte(s)) bs:=h.Sum(nil) fmt.Println(s) fmt.Printf("%x\n",bs) }

结果输出为:

sha1thisstring cf23df2207d99a74fbe169e3eba035e633b65d94

而在godoc产生的文档使用io:WriteString代替sha1.Write(),测试2种方法都可以用。

有些文档说,使用io:WriteString,意思更加明显,而且不用像上面要进行类型转换。

h:=sha1.New() io.WriteString(h,"Hismoneyistwicetainted:taintyoursandtaintmine.") fmt.Printf("%x",h.Sum(nil))

说个有趣的现象,使用上面代码产生的hash值和命令行中sha1sum的值不一致。

$echo"sha1thisstring"|sha1sum 0faabfb58d5c522f47944173f2953f40ecfc2975- $ $cata.txt sha1thisstring $sha1suma.txt 0faabfb58d5c522f47944173f2953f40ecfc2975a.txt $

可以看到,上面2个结果是一致的,但与我们上面golang代码的输出不一致。

原因是,命令行echo会在字符串后面添加一个换行符,导致整个hash值变化。大家可以自行在golang的代码中,在验证的字符串中添加换行符测试看看。

2.对文本进行hash

参考google论坛

模式是,os.Open(file),io.Copy(dst,src),sha1.Sum()

摘录2个github代码,代码在原来的基础上有修改。

gomd5/sha1example

/* Hash-GuillermoEstrada SimpleutilitytoobtaintheMD5and/orSHA-1 ofafilefromthecommandline. packagemain import( "io" "os" "fmt" "flag" "crypto/md5" "crypto/sha1" ) funcmain(){ md5f:=flag.Bool("md5",false,"-md5calculatemd5hashoffile") sha1f:=flag.Bool("sha1",false,"-sha1calculatesha1hashoffile") flag.Parse() if!*md5f&&!*sha1f{ fmt.Println("err:Nohashspecified.Use-md5or-sha1orboth.") os.Exit(1) } infile,inerr:=os.Open(flag.Arg(0)) ifinerr==nil{ if*md5f{ md5h:=md5.New() io.Copy(md5h,infile) fmt.Printf("%x%s\n",md5h.Sum(nil),flag.Arg(0)) } if*sha1f{ sha1h:=sha1.New() io.Copy(sha1h,infile) fmt.Printf("%x%s\n",sha1h.Sum(nil),flag.Arg(0)) } }else{ fmt.Println(inerr) os.Exit(1) } }

命令行调用:

#forainmd5sha1;doecho${a}sum;./hash-$a/bin/ls;${a}sum/bin/ls;echo;done md5sum b691e28e120f6989e37c7db21cb51931/bin/ls b691e28e120f6989e37c7db21cb51931/bin/ls sha1sum 502202e177bb8677c8c3b059cc1401d1524806c8/bin/ls 502202e177bb8677c8c3b059cc1401d1524806c8/bin/ls #

hashes.go

/* Hash-GuillermoEstrada SimpleutilitytoobtaintheMD5and/orSHA-1 ofafilefromthecommandline. 2011 Edited:MarkoMikulicic2011 */ packagemain import( "io" "os" "fmt" "flag" "hash" "crypto/md5" "crypto/sha1" "crypto/sha256" "crypto/sha512" //"crypto/ripemd160" ) funcmain(){ algos:=[...]string{"md5","sha1","sha256","sha512"} impls:=[...]hash.Hash{md5.New(),sha1.New(),sha256.New(),sha512.New()} flags:=make([]*bool,len(algos)) fori,a:=rangealgos{ flags[i]=flag.Bool(a,false,fmt.Sprintf("-%scalculate%shashoffile",a,a)) } flag.Parse() any:=false for_,f:=rangeflags{ any=any||*f } ifany==false{ fmt.Println("err:Nohashspecified.Pleaserunwith--helptoseelistofsupportedhashalgos") os.Exit(1) } infile,err:=os.Open(flag.Arg(0)) iferr!=nil{ fmt.Println(err) os.Exit(1) } writers:=make([]io.Writer,0,len(impls)) fori,flag:=rangeflags{ if*flag{ writers=append(writers,impls[i]) } } dest:=io.MultiWriter(writers...) io.Copy(dest,infile) fori,flag:=rangeflags{ if*flag{ fmt.Printf("%s:\n%x\n",algos[i],impls[i].Sum(nil)) } } }

命令行调用:

#forainmd5sha1sha256sha512;do./hashes-$a/bin/ls;${a}sum/bin/ls;echo;done md5: b691e28e120f6989e37c7db21cb51931 b691e28e120f6989e37c7db21cb51931/bin/ls sha1: 502202e177bb8677c8c3b059cc1401d1524806c8 502202e177bb8677c8c3b059cc1401d1524806c8/bin/ls sha256: 1e87d99599ddea2a93f060b50a54066e8b756d752158e6147cbb99b06eb11d99 1e87d99599ddea2a93f060b50a54066e8b756d752158e6147cbb99b06eb11d99/bin/ls sha512: 343b38486ad17c5813026c9df7e1ce7e268d7c64e70439aebddcafdfe10a0dfc9f55bf8169c3e592942f50bb408a852dfd1fb08e0bcadf214bc89dbf72d693f8 343b38486ad17c5813026c9df7e1ce7e268d7c64e70439aebddcafdfe10a0dfc9f55bf8169c3e592942f50bb408a852dfd1fb08e0bcadf214bc89dbf72d693f8/bin/ls #

本文内容总结:

原文链接:https://www.cnblogs.com/getong/p/3791230.html