summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2015-03-15 09:29:49 -0400
committerJesse Luehrs <doy@tozt.net>2015-03-15 09:29:49 -0400
commit57b66ba59faf95d98d829874be114bea095e7e31 (patch)
tree033f81512029d753cd367c0cb776f1c44eeb0326
parent927121ecfa5cd251228fc9d64142f2224245a6dc (diff)
downloadmatasano-57b66ba59faf95d98d829874be114bea095e7e31.tar.gz
matasano-57b66ba59faf95d98d829874be114bea095e7e31.zip
hamming distance
-rw-r--r--src/lib.rs24
1 files changed, 24 insertions, 0 deletions
diff --git a/src/lib.rs b/src/lib.rs
index 5c8639c..40abaad 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -75,6 +75,25 @@ pub fn find_single_byte_xor_encrypted_string (inputs: &[Vec<u8>]) -> Vec<u8> {
return best_decrypted;
}
+fn hamming (bytes1: &[u8], bytes2: &[u8]) -> u64 {
+ count_bits(&fixed_xor(bytes1, bytes2)[..])
+}
+
+fn count_bits (bytes: &[u8]) -> u64 {
+ bytes.iter().map(|&c| { count_bits_byte(c) }).fold(0, |acc, n| acc + n)
+}
+
+fn count_bits_byte (byte: u8) -> u64 {
+ (((byte & (0x01 << 0)) >> 0)
+ + ((byte & (0x01 << 1)) >> 1)
+ + ((byte & (0x01 << 2)) >> 2)
+ + ((byte & (0x01 << 3)) >> 3)
+ + ((byte & (0x01 << 4)) >> 4)
+ + ((byte & (0x01 << 5)) >> 5)
+ + ((byte & (0x01 << 6)) >> 6)
+ + ((byte & (0x01 << 7)) >> 7)) as u64
+}
+
fn crack_single_byte_xor_with_confidence (input: &[u8]) -> (u8, f64) {
let mut min_diff = 100.0;
let mut best_key = 0;
@@ -112,3 +131,8 @@ fn crack_single_byte_xor_with_confidence (input: &[u8]) -> (u8, f64) {
return (best_key, min_diff);
}
+
+#[test]
+fn test_hamming () {
+ assert_eq!(hamming(b"this is a test", b"wokka wokka!!!"), 37);
+}