Created
July 19, 2019 13:55
-
-
Save reusee/64b1be9ae92256d112831ed050978f24 to your computer and use it in GitHub Desktop.
This file contains 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 ( | |
"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