72

有人可以向我展示一个工作示例,说明如何myPassword := "beautiful"使用 Go 1 生成我拥有的字符串的 SHA 哈希值吗?

文档页面缺少示例,我在 Google 上找不到任何工作代码。

4

8 回答 8

90

一个例子 :

import (
    "crypto/sha1"
    "encoding/base64"
)

func (ms *MapServer) storee(bv []byte) {
    hasher := sha1.New()
    hasher.Write(bv)
    sha := base64.URLEncoding.EncodeToString(hasher.Sum(nil))
        ...
}

在这个例子中,我从一个字节数组中创建了一个 sha。您可以使用获取字节数组

bv := []byte(myPassword) 

当然,如果您不需要,您不需要在 base64 中对其进行编码:您可以使用 Sum 函数返回的原始字节数组。

下面的评论似乎有些混乱。因此,让我们为下一个用户阐明转换为字符串的最佳实践:

  • 您永远不会将 SHA 作为字符串存储在数据库中,而是作为原始字节存储
  • 当您想向用户显示 SHA 时,常用的方式是十六进制
  • 当您需要字符串表示因为它必须适合 URL 或文件名时,通常的解决方案是Base64,它更紧凑
于 2012-05-22T12:22:08.053 回答
72

Go By Example有一个关于 sha1 散列的页面。

package main

import (
    "fmt"
    "crypto/sha1"
    "encoding/hex"
)

func main() {

    s := "sha1 this string"
    h := sha1.New()
    h.Write([]byte(s))
    sha1_hash := hex.EncodeToString(h.Sum(nil))

    fmt.Println(s, sha1_hash)
}

你可以在 play.golang.org 上运行这个例子

于 2015-02-05T17:18:30.363 回答
28

http://golang.org/pkg/crypto/sha1/上的包文档确实有一个示例来说明这一点。它是作为 New 函数的示例,但它是页面上唯一的示例,并且它在页面顶部附近有一个链接,因此值得一看。完整的例子是,

代码:

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

输出:

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

于 2012-05-22T20:11:40.463 回答
19

您实际上可以以更简洁和惯用的方式执行此操作:

// Assuming 'r' is set to some inbound net/http request
form_value := []byte(r.PostFormValue("login_password"))
sha1_hash := fmt.Sprintf("%x", sha1.Sum(form_value))

// Then output optionally, to test
fmt.Println(sha1_hash)

在这个包含 login_password 字段的http.Request POST 的简单示例中,值得注意的是,调用fmt.Sprintf()%x将哈希值转换为十六进制,而无需包含import "encoding/hex"声明。

(我们使用fmt.Sprintf()而不是fmt.Printf()因为我们将字符串输出到变量赋值,而不是io.Writer接口。)

同样值得参考的是sha1.Sum()函数以与sha1.New()定义相同的方式详细实例化:

func New() hash.Hash {
    d := new(digest)
    d.Reset()
    return d
}

func Sum(data []byte) [Size]byte {
    var d digest
    d.Reset()
    d.Write(data)
    return d.checkSum()
}

这适用于 Golang 标准加密集中的 Sha 库变体(至少在发布时),例如Sha512

最后,如果愿意,他们可以遵循 Golang 的 [to]String() 实现,使用类似func (h hash.Hash) String() string {...}封装流程的方法。

这很可能超出了原始问题的预期范围。

于 2015-02-25T20:01:45.367 回答
9
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)

于 2019-04-03T11:33:12.283 回答
7

这里有一些很好的例子:

第二个示例针对 sha256,要执行 sha1 十六进制,您将执行以下操作:

// 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))

(来自https://github.com/soniah/dnsmadeeasy

于 2014-12-04T12:23:20.417 回答
3

这是一个可用于生成 SHA1 哈希的函数:

// SHA1 hashes using sha1 algorithm
func SHA1(text string) string {
    algorithm := sha1.New()
    algorithm.Write([]byte(text))
    return hex.EncodeToString(algorithm.Sum(nil))
}

我在这里汇总了一组这些实用程序哈希函数:https ://github.com/shomali11/util

你会发现FNV32, FNV32a, FNV64, FNV65a, MD5, SHA1,SHA256SHA512

于 2017-11-12T00:29:36.610 回答
1
// Get sha1 from string
func Hashstr(Txt string) string {
    h := sha1.New()
    h.Write([]byte(Txt))
    bs  := h.Sum(nil)
    sh:= string(fmt.Sprintf("%x\n", bs))
    return sh
}
于 2019-03-13T10:10:21.617 回答