Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "fmt"
- "hash/crc32"
- "strings"
- "io/ioutil"
- )
- // Hash - generates hash(crc32) for string
- func Hash(s string) uint32 {
- crc32q := crc32.MakeTable(0xD5828281)
- return crc32.Checksum([]byte(s), crc32q)
- }
- func main() {
- // map to store generated data, used to detect collisions
- m := make(map[uint32]string)
- for {
- // use kernel UUIDv4 generator
- data, err := ioutil.ReadFile("/proc/sys/kernel/random/uuid")
- if err != nil {
- continue
- }
- // convert output to string UUIDv4
- uuid := strings.TrimSpace(string(data))
- // make hash of string
- hash := Hash(uuid)
- // check if hash in map, collision
- if val, ok := m[hash]; ok {
- if uuid != val {
- fmt.Printf("Hash [%d] collision detected: [%s] = [%s] \n", hash, uuid, val)
- }
- } else {
- // add hash to map
- m[hash] = uuid
- }
- }
- }
Add Comment
Please, Sign In to add comment