Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sha1.Sum() gives different output when called more than one time #216

Closed
gopherbot opened this issue Nov 16, 2009 · 4 comments
Closed

sha1.Sum() gives different output when called more than one time #216

gopherbot opened this issue Nov 16, 2009 · 4 comments

Comments

@gopherbot
Copy link
Contributor

by patla073:

Hello, I've been using the sha1-package and I have encountered a strange 
behaviour when calling sha1.Sum() on the same object more than one time.

I do not have much experience with sha1, but this behaviour is inconsistent 
with the hashlib from Python. I have put together two small programs to 
reproduce the output from go and the corresponding output from python.

Best Regards.
Patrik


Go program:



package main

import "fmt"
import "strings"
import "crypto/sha1"

func main() {

    teststring := "issue9";
    thehash := sha1.New();
    thehash.Write(strings.Bytes(teststring));
    result := thehash.Sum();
    fmt.Printf("%d\n\n", result);
    result = thehash.Sum();
    fmt.Printf("%d\n\n", result);

}




Corresponding Python program:

from hashlib import sha1
from struct import unpack

teststring = "issue9"
thehash = sha1(teststring)
result = thehash.digest()
print unpack('%sB' % len(result), result)
print
result = thehash.digest()
print unpack('%sB' % len(result), result)



Go output:

%d([]uint8=[130 101 105 116 191 0 110 128 79 240 20 225 81 250 173 57 214 4 
177 25])

%d([]uint8=[6 217 191 209 66 212 123 60 34 155 125 43 70 214 99 211 197 240 
134 1])



Python output:

(130, 101, 105, 116, 191, 0, 110, 128, 79, 240, 20, 225, 81, 250, 173, 57, 
214, 4, 177, 25)

(130, 101, 105, 116, 191, 0, 110, 128, 79, 240, 20, 225, 81, 250, 173, 57, 
214, 4, 177, 25)
@agl
Copy link
Contributor

agl commented Nov 16, 2009

Comment 1:

Sum() is finishing the hash. It's not currently allowed to call it twice.
However, this should either be well documented as such or changed to allow partial 
digests.

Labels changed: added package-change.

Owner changed to a...@golang.org.

Status changed to Thinking.

@rsc
Copy link
Contributor

rsc commented Dec 2, 2009

Comment 2:

Labels changed: added packagechange, removed package-change.

@rsc
Copy link
Contributor

rsc commented Jan 19, 2010

Comment 3:

http://golang.org/cl/186210

Status changed to Started.

@rsc
Copy link
Contributor

rsc commented Jan 19, 2010

Comment 4:

This issue was closed by revision b9f26c3.

Status changed to Fixed.

Merged into issue #-.

@golang golang locked and limited conversation to collaborators Jun 24, 2016
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants