golang中的檢驗hash

1.對字符串進行hashgit

你們能夠看一下, SHA1 Hashesgithub

Go by Example寫道:golang

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

附上golang代碼app

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種方法均可以用。ui

有些文檔說,使用io:WriteString,意思更加明顯,並且不用像上面要進行類型轉換。this

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

說個有趣的現象,使用上面代碼產生的hash值和命令行中sha1sum的值不一致。google

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

能夠看到,上面2個結果是一致的,但與咱們上面golang代碼的輸出不一致。spa

緣由是,命令行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

#
相關文章
相關標籤/搜索