Created
February 1, 2019 14:43
-
-
Save melkael/3aca4d858d9b3b972ba1d647c281db20 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
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