Created
November 18, 2018 16:14
-
-
Save ik5/e737baa6dbeb146b77e75bf4d1365c85 to your computer and use it in GitHub Desktop.
Some non cryptographic hashing algorithms I tested
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"hash" | |
"hash/adler32" | |
"hash/crc32" | |
"hash/fnv" | |
"github.com/dgryski/dgohash" | |
) | |
func doHash(b []byte, h hash.Hash) { | |
h.Reset() | |
h.Write(b) | |
result := h.Sum(nil) | |
if result[0]%2 == 0 { | |
fmt.Println(fmt.Sprintf("%x", result)) | |
} else { | |
fmt.Println(fmt.Sprintf("%X", result)) | |
} | |
} | |
func java(b []byte) { | |
doHash(b, dgohash.NewJava32()) | |
} | |
func dbj32(b []byte) { | |
doHash(b, dgohash.NewDjb32()) | |
} | |
func dbj32a(b []byte) { | |
doHash(b, dgohash.NewDjb32a()) | |
} | |
func elf32(b []byte) { | |
doHash(b, dgohash.NewElf32()) | |
} | |
func sdbm32(b []byte) { | |
doHash(b, dgohash.NewSDBM32()) | |
} | |
func sqlite3(b []byte) { | |
doHash(b, dgohash.NewSQLite32()) | |
} | |
func jenkins(b []byte) { | |
doHash(b, dgohash.NewJenkins32()) | |
} | |
func murmur(b []byte) { | |
doHash(b, dgohash.NewMurmur3_x86_32()) | |
} | |
func superFast(b []byte) { | |
doHash(b, dgohash.NewSuperFastHash()) | |
} | |
func marvin(b []byte) { | |
doHash(b, dgohash.NewMarvin32(0)) | |
} | |
func fnvHash(b []byte) { | |
doHash(b, fnv.New32()) | |
} | |
func fnvAHash(b []byte) { | |
doHash(b, fnv.New32a()) | |
} | |
func adler32Hash(b []byte) { | |
doHash(b, adler32.New()) | |
} | |
func crc32Hash(b []byte) { | |
result := crc32.ChecksumIEEE(b) | |
if result%2 == 0 { | |
fmt.Println(fmt.Sprintf("%x", result)) | |
} else { | |
fmt.Println(fmt.Sprintf("%X", result)) | |
} | |
} | |
func main() { | |
by := []byte("Hello World !") | |
hashes := map[string]func(b []byte){ | |
"java": java, | |
"dbj32": dbj32, | |
"dbj32a": dbj32a, | |
"sdbm32": sdbm32, | |
"elf32": elf32, | |
"sqlite3": sqlite3, | |
"jenkins": jenkins, | |
"murmur": murmur, | |
"superfast": superFast, | |
"marvin": marvin, | |
"adler32": adler32Hash, | |
"crc32": crc32Hash, | |
"fnv": fnvHash, | |
"fnvA": fnvAHash, | |
} | |
for name, f := range hashes { | |
fmt.Printf("%12.12s: ", name) | |
f(by) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment