Generating the SHA hash of a string using golang

后端 未结 8 1634
清酒与你
清酒与你 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:50

    Here's some good examples:

    • http://golang.org/src/pkg/crypto/hmac/hmac_test.go
    • http://www.jokecamp.com/blog/examples-of-creating-base64-hashes-using-hmac-sha256-in-different-languages/#go

    The second example targets sha256, to do sha1 hexadecimal you'd do:

    // Calculate the hexadecimal HMAC SHA1 of requestDate using sKey                
    key := []byte(c.SKey)                                                           
    h := hmac.New(sha1.New, key)                                                    
    h.Write([]byte(requestDate))                                                    
    hmacString := hex.EncodeToString(h.Sum(nil))
    

    (from https://github.com/soniah/dnsmadeeasy)

提交回复
热议问题