Skip to content

Instantly share code, notes, and snippets.

@reusee
Created July 19, 2019 13:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save reusee/64b1be9ae92256d112831ed050978f24 to your computer and use it in GitHub Desktop.
Save reusee/64b1be9ae92256d112831ed050978f24 to your computer and use it in GitHub Desktop.
package main
import (
"crypto/md5"
"encoding/binary"
"runtime"
"sync"
)
func main() {
shards := runtime.NumCPU()
lowerBound := uint64(10000000000)
upperBound := uint64(19999999999)
perShard := (upperBound - lowerBound) / uint64(shards)
var sets []map[[16]byte]struct{}
wg := new(sync.WaitGroup)
for i := 0; i < shards+1; i++ {
wg.Add(1)
begin := lowerBound + perShard*uint64(i)
end := begin + perShard
set := make(map[[16]byte]struct{})
sets = append(sets, set)
go func() {
defer wg.Done()
bs := make([]byte, 16)
for num := begin; num < end; num++ {
binary.BigEndian.PutUint64(bs, num)
sum := md5.Sum(bs)
if _, ok := set[sum]; ok {
panic("collision found")
}
set[sum] = struct{}{}
}
}()
}
wg.Wait()
hashes := make(map[[16]byte]struct{})
n := 0
for _, set := range sets {
n += len(set)
for hash := range set {
hashes[hash] = struct{}{}
}
}
if len(hashes) != n {
panic("collision found")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment