Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
func distanceBetweenChunks(keysize int, data string) float64 {
// we are using hex string here as input : one ascii char is two hex digits long
out := 0.0
totalNumberOfChunks := 0.0
for i := 0; i < len(data)/(5*keysize); i++ {
offset := i * 4 * keysize
firstChunk := data[offset : offset+keysize*2]
secondChunk := data[offset+keysize*2 : offset+4*keysize]
res := float64(hamming(firstChunk, secondChunk))
res /= float64(keysize)
out += res
totalNumberOfChunks = float64(i)
}
return out / totalNumberOfChunks
}
func optimalHammingDistance(data string) int {
sizeOfMin := 0
scoreOfMin := 1000.0
for i := 2; i < 41; i++ {
if scoreOfMin > distanceBetweenChunks(i, data) {
sizeOfMin = i
scoreOfMin = distanceBetweenChunks(i, data)
}
}
return sizeOfMin
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.