diff options
author | rsiddharth <s@ricketyspace.net> | 2020-09-05 10:40:42 -0400 |
---|---|---|
committer | rsiddharth <s@ricketyspace.net> | 2020-09-05 10:41:08 -0400 |
commit | b28784b5890d5fdc7cd9d4b9bd05d497e19ca6cb (patch) | |
tree | e842f6d9b11348bef891159d9bd48a0de34176e5 /lib/hamming.go | |
parent | 44f0fd653e2fa0fd2e0a4d067a335bba2cd21525 (diff) |
lib: add KeySizeWithMinDistance
* lib/hamming.go (KeySizeWithMinDistance): New function.
Diffstat (limited to 'lib/hamming.go')
-rw-r--r-- | lib/hamming.go | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/lib/hamming.go b/lib/hamming.go index 1e53d54..05a7546 100644 --- a/lib/hamming.go +++ b/lib/hamming.go @@ -32,6 +32,29 @@ func setBits(b byte) int { return int(c) } + +// Returns key size with minimum normalized hamming distance +// 'keyMin' is the minimum key size +// 'keyMax' is the maximum key size +func KeySizeWithMinDistance(keyMin, keyMax int) int { + var mk int = 0 // Key size with min distance. + var md float64 = 100.0 // Distance for key size 'mk'. + for k := keyMin; k <= keyMax; k++ { + p := genKey(k) + q := genKey(k) + + // Compute distance. + d := HammingDistance(p, q) + + nd := float64(d) / float64(k) + if nd < md { + mk = k + md = nd + } + } + return mk +} + // Generates a key of size 'size' bytes. func genKey(size int) []byte { bs := make([]byte, size, size) |