Skip to content

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

@gopherbot

Description

@gopherbot

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)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions