Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate hash number of a string in Go?

Tags:

hash

go

For example:

hash("HelloWorld") = 1234567 

Is there any built-in function could do this ?

Thanks.

like image 976
WoooHaaaa Avatar asked Nov 27 '12 10:11

WoooHaaaa


People also ask

How do you generate a hash value?

In Windows File Explorer select the files you want the hash values calculated for, click the right mouse button, and select Calculate Hash Value, then select the appropriate hash type from the pop-up sub-menu (e.g. MD5). The values will then be calculated and displayed.

What is hash seed?

Hash Seeds The original Hash strain is a crossbreed of pure Afghani with Northern Lights #1. The hash plant is easy to grow—perfect for beginner and seasoned cultivators. The fast-flowering indica is hardy, thriving in warm and colder climates.


2 Answers

The hash package is helpful for this. Note it's an abstraction over specific hash implementations. Some ready made are found in the package subdirectories.

Example:

package main  import (         "fmt"         "hash/fnv" )  func hash(s string) uint32 {         h := fnv.New32a()         h.Write([]byte(s))         return h.Sum32() }  func main() {         fmt.Println(hash("HelloWorld"))         fmt.Println(hash("HelloWorld.")) } 

(Also here)


Output:

926844193 107706013 
like image 110
zzzz Avatar answered Sep 24 '22 22:09

zzzz


Here is a function you could use to generate a hash number:

// FNV32a hashes using fnv32a algorithm func FNV32a(text string) uint32 {     algorithm := fnv.New32a()     algorithm.Write([]byte(text))     return algorithm.Sum32() } 

I put together a group of those utility hash functions here: https://github.com/shomali11/util

You will find FNV32, FNV32a, FNV64, FNV64a, MD5, SHA1, SHA256 and SHA512

like image 36
Raed Shomali Avatar answered Sep 22 '22 22:09

Raed Shomali