doudeng2184 2018-08-14 19:06
浏览 189
已采纳

与Bash sha256sum的Sha256Sum差[关闭]

My go code is producing different sha256sum values than bash commandline. I've read through various questions and answers and they all point to what I've already done, as this community asks me to do before posting

Here's my sha256sum code on go

sha256Key:=verifyEmail+":"+md5password+":"+dateStr
hasherSha256 := sha1.New()
hasherSha256.Write([]byte(sha256Key))
sha256Val:=hex.EncodeToString(hasherSha256.Sum(nil))

And here's my bash script code:

key=$( echo -n "$verifyEmail:$md5PWD:$pwTime" | sha256sum)
echo $key

Ive already validated that the keys are the same. One note, my dateStr variable inside go comes from date formatting:

now := time.Now().Unix()
rem := now % 3600
date := now-rem         
dateStr:=strconv.FormatInt(date,10)

Usually I get downvotes so I tried making this question as clear and concise as possible.

Please let me know if im missing anything.

Thanks

  • 写回答

1条回答 默认 最新

  • dongwen5870 2018-08-14 19:10
    关注

    You say you want to calculate SHA-256 checksum, yet you do:

    hasherSha256 := sha1.New()
    

    That will be an SHA-1 hasher, not SHA-256. Instead do:

    hasherSha256 := sha256.New()
    

    Also note that to calculate a "one-time" digest of some data (ready in a byte slice), you may use the sha256.Sum256() function like this:

    digest := sha256.Sum256([]byte(sha256Key))
    

    Note that here digest will be an array (not a slice, in Go they are quite different), an array of type [32]byte. To obtain a slice "of it" (of type []byte), slice it like this:

    digestSlice := digest[:]
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?