分享
  1. 首页
  2. 文章

golang中的检验hash

getong · · 10925 次点击 · · 开始浏览
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

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
0faabfb58d5c522f47944173f2953f40ecfc2975 a.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[i] = 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[i])
 }
 }
 dest := io.MultiWriter(writers...)
 io.Copy(dest, infile)
 for i, flag := range flags {
 if *flag {
 fmt.Printf("%s: \n%x\n", algos[i], impls[i].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
#

有疑问加站长微信联系(非本文作者)

本文来自:博客园

感谢作者:getong

查看原文:golang中的检验hash

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

关注微信
10925 次点击
1 回复 | 直到 2018年09月01日 13:27:12
暂无回复
添加一条新回复 (您需要 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传

用户登录

没有账号?注册
(追記) (追記ここまで)

今日阅读排行

    加载中
(追記) (追記ここまで)

一周阅读排行

    加载中

关注我

  • 扫码关注领全套学习资料 关注微信公众号
  • 加入 QQ 群:
    • 192706294(已满)
    • 731990104(已满)
    • 798786647(已满)
    • 729884609(已满)
    • 977810755(已满)
    • 815126783(已满)
    • 812540095(已满)
    • 1006366459(已满)
    • 692541889

  • 关注微信公众号
  • 加入微信群:liuxiaoyan-s,备注入群
  • 也欢迎加入知识星球 Go粉丝们(免费)

给该专栏投稿 写篇新文章

每篇文章有总共有 5 次投稿机会

收入到我管理的专栏 新建专栏