李斯特 发表于 2018-9-20 06:08:05

golang中的检验hash

  1.对字符串进行hash
  大家可以看一下, SHA1 Hashes
  Go by Example写道:
  

The pattern for generating a hash is sha1.New(), sha1.Write(bytes), then sha1.Sum([]byte{}).   

  附上golang代码
  

package main  

  
import (
  

"crypto/sha1"  
"fmt"
  
)
  

  

  
func main() {
  
s := "sha1 this string"
  

  
h := sha1.New()
  

  
h.Write([]byte(s))
  

  
bs := h.Sum(nil)
  

  
fmt.Println(s)
  
fmt.Printf("%x\n", bs)
  

  
}
  

  结果输出为:
  

sha1 this string  
cf23df2207d99a74fbe169e3eba035e633b65d94
  

  而在godoc产生的文档使用io:WriteString代替sha1.Write(),测试2种方法都可以用。
  有些文档说,使用io:WriteString,意思更加明显,而且不用像上面要进行类型转换。
  

h := sha1.New()  
io.WriteString(h
, "His money is twice tainted: 'taint yours and 'taint mine.")  
fmt.Printf(
"% x", h.Sum(nil))  

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

$echo "sha1 this string" | sha1sum  
0faabfb58d5c522f47944173f2953f40ecfc2975
-  
$
  
$
cat a.txt  
sha1 this
string  
$sha1sum a.txt
  
0faabfb58d5c522f47944173f2953f40ecfc2975a.txt
  
$
  

  可以看到,上面2个结果是一致的,但与我们上面golang代码的输出不一致。
  原因是,命令行echo会在字符串后面添加一个换行符,导致整个hash值变化。大家可以自行在golang的代码中,在验证的字符串中添加换行符测试看看。
  2.对文本进行hash
  参考 google论坛
  模式是,os.Open(file), io.Copy(dst,src), sha1.Sum()
  摘录2个github代码,代码在原来的基础上有修改。
  go md5/sha1 example
  

/*  
Hash - Guillermo Estrada
  

  
Simple utility to obtain the MD5 and/or SHA-
1  
of a file from the command line.
  

  
package main
  

  
import (
  

"io"  
"os"
  
"fmt"
  
"flag"
  
"crypto/md5"
  
"crypto/sha1"
  
)
  

  
func main() {
  

  
md5f := flag.Bool("md5", false, "-md5 calculate md5 hash of file")
  
sha1f := flag.Bool("sha1", false, "-sha1 calculate sha1 hash of file")
  
flag.Parse()
  

  
if !*md5f && !*sha1f {
  
fmt.Println("err: No hash specified. Use -md5 or -sha1 or both.")
  
os.Exit(1)
  
}
  

  

  

  
infile, inerr := os.Open(flag.Arg(0))
  
if inerr == 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)
  
}
  
}
  

  命令行调用:
  

#for a in md5 sha1 ; do echo ${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 - Guillermo Estrada
  

  
Simple utility to obtain the MD5 and/or SHA-
1  
of a file from the command line.
  

  

2011  

  
Edited: Marko Mikulicic
2011  
*/
  

  
package main
  

  
import (
  

"io"  
"os"
  
"fmt"
  
"flag"
  
"hash"
  
"crypto/md5"
  
"crypto/sha1"
  
"crypto/sha256"
  
"crypto/sha512"
  
//"crypto/ripemd160"
  
)
  

  
func main() {
  

  
algos := [...]string{"md5", "sha1", "sha256", "sha512" }
  
impls := [...]hash.Hash{md5.New(), sha1.New(), sha256.New(), sha512.New() }
  
flags := make([]*bool, len(algos))
  
for i, a := range algos {
  
flags = flag.Bool(a, false, fmt.Sprintf("-%s calculate %s hash of file", a, a))
  
}
  

  
flag.Parse()
  

  

  
any := false
  
for _, f := range flags {
  
any = any || *f
  
}
  
if any == false {
  
fmt.Println("err: No hash specified. Please run with --help to see list of supported hash algos")
  
os.Exit(1)
  
}
  

  

  
infile, err := os.Open(flag.Arg(0))
  
if err != nil {
  
fmt.Println(err)
  
os.Exit(1)
  
}
  

  

  
writers := make([]io.Writer, 0, len(impls))
  
for i, flag := range flags {
  
if *flag {
  
writers = append(writers, impls)
  
}
  
}
  

  
dest := io.MultiWriter(writers...)
  

  
io.Copy(dest, infile)
  

  
for i, flag := range flags {
  
if *flag {
  
fmt.Printf("%s: \n%x\n", algos, impls.Sum(nil))
  
}
  
}
  

  
}
  

  命令行调用:
  

#for a in md5 sha1 sha256 sha512 ; 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  

  
#
  



页: [1]
查看完整版本: golang中的检验hash