与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

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[:]