Guest User

Untitled

a guest
Jan 16th, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "fmt"
  5. "hash/crc32"
  6. "strings"
  7. "io/ioutil"
  8. )
  9.  
  10. // Hash - generates hash(crc32) for string
  11. func Hash(s string) uint32 {
  12. crc32q := crc32.MakeTable(0xD5828281)
  13. return crc32.Checksum([]byte(s), crc32q)
  14. }
  15.  
  16. func main() {
  17. // map to store generated data, used to detect collisions
  18. m := make(map[uint32]string)
  19. for {
  20. // use kernel UUIDv4 generator
  21. data, err := ioutil.ReadFile("/proc/sys/kernel/random/uuid")
  22. if err != nil {
  23. continue
  24. }
  25. // convert output to string UUIDv4
  26. uuid := strings.TrimSpace(string(data))
  27. // make hash of string
  28. hash := Hash(uuid)
  29. // check if hash in map, collision
  30. if val, ok := m[hash]; ok {
  31. if uuid != val {
  32. fmt.Printf("Hash [%d] collision detected: [%s] = [%s] \n", hash, uuid, val)
  33. }
  34. } else {
  35. // add hash to map
  36. m[hash] = uuid
  37. }
  38. }
  39. }
Add Comment
Please, Sign In to add comment