Generating the SHA hash of a string using golang

后端 未结 8 1543
清酒与你
清酒与你 2020-12-23 16:29

Can someone show me a working example of how to generate a SHA hash of a string that I have, say myPassword := \"beautiful\" , using Go 1 ?

The docs pa

相关标签:
8条回答
  • 2020-12-23 16:55

    The package documentation at http://golang.org/pkg/crypto/sha1/ does have an example that demonstrates this. It's stated as an example of the New function, but it's the only example on the page and it has a link right near the top of the page so it is worth looking at. The complete example is,

    Code:

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

    Output:

    59 7f 6a 54 00 10 f9 4c 15 d7 18 06 a9 9a 2c 87 10 e7 47 bd

    0 讨论(0)
  • 2020-12-23 16:58
    h := sha1.New()
    h.Write(content)
    sha := h.Sum(nil)  // "sha" is uint8 type, encoded in base16
    
    shaStr := hex.EncodeToString(sha)  // String representation
    
    fmt.Printf("%x\n", sha)
    fmt.Println(shaStr)
    
    
    0 讨论(0)
提交回复
热议问题