md5 and learning go.

1,147 views
Skip to first unread message

toxicnaan

unread,
Nov 11, 2010, 12:06:24 PM11/11/10
to golang-nuts
Hi,

hope this question isn't too dumb. I'm new to go and i come from a C/
Java Background, I'm playing with some example and trying to calculate
some md5 sums just for fun.

I've got the following code to calculate the hash of the string "hello
go rocks"

hash := md5.New()
hash.Write("hello go rocks")
md5string := hash.Sum()
fmt.Print(md5string)

however, i get

cannot use "hello" (type string) as type []uint8 in function argument

i presume when hash.Write I have to cast this to an type uint8 ?

I've looked at the md5_test.go, but i don't understand it as of yet.

hope you can help.

Cheers,
Lee









Evan Shaw

unread,
Nov 11, 2010, 12:11:10 PM11/11/10
to toxicnaan, golang-nuts
On Thu, Nov 11, 2010 at 11:06 AM, toxicnaan <toxi...@gmail.com> wrote:
> Hi,
>
> hope this question isn't too dumb. I'm new to go and i come from a C/
> Java Background, I'm playing with some example and trying to calculate
> some md5 sums just for fun.
>
> I've got the following code to calculate the hash of the string "hello
> go rocks"
>
> hash := md5.New()
> hash.Write("hello go rocks")
> md5string := hash.Sum()
> fmt.Print(md5string)
>
> however, i get
>
> cannot use "hello" (type string) as type []uint8 in function argument
>
> i presume when hash.Write I have to cast this to an type uint8 ?

Yes. The Write method takes a slice of bytes ([]byte), but you're
trying to give it a string. You can convert your string to a slice of
bytes by changing your second line to:

hash.Write([]byte("hello go rocks"))

Converting to []uint8 should work equally well, as byte and uint8 are
aliases for the same type.

- Evan

jimt

unread,
Nov 11, 2010, 12:14:03 PM11/11/10
to golang-nuts
It should also be noted that hash.Sum() returns the 16-byte raw binary
hash and will not print to a console very well. If you want the 32-
byte hexadecimal representation, use this:

hash := md5.New()
hash.Write([]byte("hello go rocks"))
md5string := fmt.Sprintf("%x", hash.Sum())
fmt.Print(md5string)

Ostsol

unread,
Nov 11, 2010, 12:15:05 PM11/11/10
to golang-nuts
Looking at the package documentation, md5.New() returns a hash.Hash,
which is also an io.Writer. io.Writer describes the function Write,
which takes a byte slice. Since Go natively implements strings, as
opposed to C which assigns string literals to a char array, you do
have to manually cast the string literal to a byte slice:

hash.Write([]byte("hello go rocks")

The compiler reports 'uint8' because 'byte' is simply a built-in alias
of it.

-Daniel

roger peppe

unread,
Nov 11, 2010, 12:27:42 PM11/11/10
to Ostsol, golang-nuts
On 11 November 2010 17:15, Ostsol <ost...@gmail.com> wrote:
> Looking at the package documentation, md5.New() returns a hash.Hash,
> which is also an io.Writer.  io.Writer describes the function Write,
> which takes a byte slice.  Since Go natively implements strings, as
> opposed to C which assigns string literals to a char array, you do
> have to manually cast the string literal to a byte slice:

if you want to calculate the hash of a file, the interface makes it dead easy:
e.g.

h := md5.New()
io.Copy(h, os.Stdin)
fmt.Printf("%x\n", h.Sum())

toxicnaan

unread,
Nov 11, 2010, 12:37:06 PM11/11/10
to golang-nuts
you'll be please to know that

hash := md5.New()
hash.Write([]byte("hello go rocks"))
md5string := hash.Sum()
fmt.Print(md5string)

prints

[113 127 248 131 169 97 172 197 162 81 87 86 190 31 175 118]

so go is clever enough to convert this byte array string a string:-).

717ff883a961acc5a2515756be1faf76

what is [113 127 248 131 169 97 172 197 162 81 87 86 190 31 175 118] ,
i presume that's the byte array converted to ascii
decimals :-) ...nice 113=71

so, fmt.Print is clever, i presume you can throw any type at it , and
it will understand it.

Indeed, go does indeed rock. I shall code some more!

Cheers,
Lee
Reply all
Reply to author
Forward
0 new messages