dongqin1819 2017-07-27 10:18
浏览 227
已采纳

sha1的go与python和openssl的不同

I am trying to build a base64 encoded sha1 hash in go but the result i am getting is very different to the results of other programming languages

package main

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

func main() {
    c := sha1.New()
  input := []byte("hello")
  myBytes := c.Sum(input)
  fmt.Println(base64.StdEncoding.EncodeToString(base64.StdPadding))
}

This Go Code prints out aGVsbG/aOaPuXmtLDTJVv++VYBiQr9gHCQ==

My Python Code Looks like this

import hashlib
import base64


print(base64.b64encode(hashlib.sha1('hello').digest()))

And outputs qvTGHdzF6KLavt4PO0gs2a6pQ00=

My bash command for comparison looks like this

echo -n hello| openssl dgst -sha1 -binary |base64

And outputs this qvTGHdzF6KLavt4PO0gs2a6pQ00=

Which lets me assume that the python code is doing everything correct. But why does go prints another result. Where is my mistake?

Thnx in advance

  • 写回答

2条回答 默认 最新

  • douqiao1997 2017-07-27 10:24
    关注

    There is an example of how to use it properly. You should do:

    c := sha1.New()
    io.WriteString(c, "hello")
    myBytes := c.Sum(nil)
    fmt.Println(base64.StdEncoding.EncodeToString(myBytes))
    

    https://play.golang.org/p/sELsWTcrdd

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 在若依框架下实现人脸识别
  • ¥15 网络科学导论,网络控制
  • ¥100 安卓tv程序连接SQLSERVER2008问题
  • ¥15 利用Sentinel-2和Landsat8做一个水库的长时序NDVI的对比,为什么Snetinel-2计算的结果最小值特别小,而Lansat8就很平均
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同