Skip to content

Instantly share code, notes, and snippets.

@melkael
Created February 1, 2019 14:43
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 melkael/3aca4d858d9b3b972ba1d647c281db20 to your computer and use it in GitHub Desktop.
Save melkael/3aca4d858d9b3b972ba1d647c281db20 to your computer and use it in GitHub Desktop.
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