<p>使用标准库的方式完全错误。不要假设一个方法/函数的作用,如果你不熟悉,一定要阅读文档。在</p>
<p><a href="https://golang.org/pkg/crypto/sha1/#New" rel="noreferrer">^{<cd1>}</a>返回<a href="https://golang.org/pkg/hash/#Hash" rel="noreferrer">^{<cd2>}</a>。它的<code>Sum()</code>方法是<strong>而不是</strong>来计算哈希值,但是为了得到当前的哈希结果,它不会改变底层的哈希状态。在</p>
<p><code>hash.Hash</code>实现<a href="https://golang.org/pkg/io/#Writer" rel="noreferrer">^{<cd5>}</a>,要计算某些数据的哈希值,必须将该数据写入其中。<code>Hash.Sum()</code>如果已经分配了一个可选的片,那么它将结果(散列)写入其中。如果您想让它分配一个新的,请传递<code>nil</code>。在</p>
<p>另外,<code>base64.StdEncoding.EncodeToString()</code>需要将要转换为base64的字节数据(字节片),因此必须将校验和数据传递给它。在您的代码中,您没有告诉<code>EncodeToString()</code>要编码什么。在</p>
<p>工作示例:</p>
<pre><code>c := sha1.New()
input := []byte("hello")
c.Write(input)
sum := c.Sum(nil)
fmt.Println(base64.StdEncoding.EncodeToString(sum))
</code></pre>
<p>输出如预期(在<a href="https://play.golang.org/p/DJARC5KI4c" rel="noreferrer">Go Playground</a>上尝试):</p>
^{pr2}$
<p>请注意,<a href="https://golang.org/pkg/crypto/sha1/" rel="noreferrer">^{<cd10>}</a>包还有一个方便的<a href="https://golang.org/pkg/crypto/sha1/#Sum" rel="noreferrer">^{<cd11>}</a>函数,该函数只需一步即可完成:</p>
^{3}$
<p>输出相同。在<a href="https://play.golang.org/p/jXJC66m_SS" rel="noreferrer">Go Playground</a>上试试。在</p>