fixing cryptolog tests
authorWilliam Budington <bill@eff.org>
Mon, 28 Oct 2013 23:24:15 +0000 (19:24 -0400)
committerWilliam Budington <bill@eff.org>
Mon, 28 Oct 2013 23:24:15 +0000 (19:24 -0400)
cryptolog.py
cryptolog_test.py

index 8c10c76..a603b10 100755 (executable)
@@ -32,8 +32,9 @@ def salt():
     salt_day = now
   return salt_data
 
-def hash_entity(entity, hashed_size):
-    return b64encode(HMAC(salt(), entity).digest())[:hashed_size]
+def hash_entity(entity, hashed_size, salt=None):
+    salt = salt or salt()
+    return b64encode(HMAC(salt, entity).digest())[:hashed_size]
 
 class LogParseError(Exception):
   pass
@@ -54,6 +55,7 @@ class CryptoFilter(object):
       field_list: what to encrypt that matches named groups
         above, e.g. ["IP", "UA"]
     """
+    self._salt = None
     if regex:
       self.SetRegex(regex)
     if field_list:
@@ -114,9 +116,12 @@ class CryptoFilter(object):
     split_log = filter(lambda x: type(x) != NoneType, split_log)
     return '%s\n' % (' '.join(split_log))
 
+  def SetSaltfile(self, salt_file):
+    self._salt = open(salt_file, 'r').read()
+
   def EncryptField(self, field, hashed_size):
     """Encrypt relevant field (e.g. IP) using salted hash."""
-    return hash_entity(field, 6)
+    return hash_entity(field, 6, self._salt)
 
 
 if __name__ == "__main__":
index 52a4b64..a9896ea 100755 (executable)
@@ -7,7 +7,7 @@ import re
 
 TMP_TEST_OUTPUT_FILE = 'testdata/testoutput.txt'
 TEST_SALT_FILE = 'testdata/testsaltfile'
-SIMPLE_REGEX = re.compile(r'(?P<IP>\d\d?\d?\.\d\d?\d?\.\d\d?\d?\.\d\d?\d?)( )(?P<OTHER>.*)')
+SIMPLE_REGEX = re.compile(r'(?P<IP>\d\d?\d?\.\d\d?\d?\.\d\d?\d?\.\d\d?\d?) (?P<OTHER>.*)')
 TEST_LOG_ENTRY = """127.0.0.1 - - [06/Apr/2011:00:00:00 -0700] "GET /br/br.gif HTTP/1.1" 301 249 - -"""
 
 class TestCryptoFilter(unittest.TestCase):
@@ -24,7 +24,7 @@ class TestCryptoFilter(unittest.TestCase):
         self.cryptofilter = None
         
     def testCryptifyOnlyIp(self):
-        self.cryptofilter.SetFields(['IP'])
+        self.cryptofilter.SetFields(['IP'], [])
         self.cryptofilter.SetRegex(SIMPLE_REGEX)
         res = self.cryptofilter.EncryptSingleLogEntry(TEST_LOG_ENTRY)
         self.assertEqual(res[:6], "EF9k26")