瀏覽代碼

add (hmac-)sha1/sha512

Thomas Waldmann 10 年之前
父節點
當前提交
88b62282ad
共有 2 個文件被更改,包括 65 次插入1 次删除
  1. 4 0
      attic/archiver.py
  2. 61 1
      attic/key.py

+ 4 - 0
attic/archiver.py

@@ -534,8 +534,12 @@ Type "Yes I am sure" if you understand this and want to continue.\n""")
         - 00      sha256 (simple hash, no MAC, faster on 32bit CPU)
         - 01      sha512-256 (simple hash, no MAC, faster on 64bit CPU)
         - 02      ghash (simple hash, no MAC, fastest on CPUs with AES-GCM support)
+        - 03      sha1 (simple hash, no MAC, fastest on CPUs without AES-GCM support)
+        - 04      sha512 (simple hash, no MAC, faster on 64bit CPU)
         - 10      hmac-sha256 (MAC, faster on 32bit CPU)
         - 11      hmac-sha512-256 (MAC, faster on 64bit CPU)
+        - 13      hmac-sha1 (MAC, fastest on CPUs without AES-GCM support)
+        - 14      hmac-sha512 (MAC, faster on 64bit CPU)
         - 20      gmac (MAC, fastest on CPUs with AES-GCM support)
         """ % (COMPR_DEFAULT, PLAIN_DEFAULT, CIPHER_DEFAULT, HASH_DEFAULT, MAC_DEFAULT))
         subparser = subparsers.add_parser('init', parents=[common_parser],

+ 61 - 1
attic/key.py

@@ -5,7 +5,7 @@ import msgpack
 import textwrap
 from collections import namedtuple
 import hmac
-from hashlib import sha256, sha512
+from hashlib import sha1, sha256, sha512
 import zlib
 
 try:
@@ -123,6 +123,46 @@ class GHASH:
         return hash
 
 
+class SHA1(object):  # note: can't subclass sha1
+    TYPE = 3
+    digest_size = 20
+
+    def __init__(self, key, data=b''):
+        # signature is like for a MAC, we ignore the key as this is a simple hash
+        if key is not None:
+            raise Exception("use a HMAC if you have a key")
+        self.h = sha1(data)
+
+    def update(self, data):
+        self.h.update(data)
+
+    def digest(self):
+        return self.h.digest()
+
+    def hexdigest(self):
+        return self.h.hexdigest()
+
+
+class SHA512(object):  # note: can't subclass sha512
+    TYPE = 4
+    digest_size = 64
+
+    def __init__(self, key, data=b''):
+        # signature is like for a MAC, we ignore the key as this is a simple hash
+        if key is not None:
+            raise Exception("use a HMAC if you have a key")
+        self.h = sha512(data)
+
+    def update(self, data):
+        self.h.update(data)
+
+    def digest(self):
+        return self.h.digest()
+
+    def hexdigest(self):
+        return self.h.hexdigest()
+
+
 class HMAC_SHA256(HMAC):
     TYPE = 10
     digest_size = 32
@@ -143,6 +183,26 @@ class HMAC_SHA512_256(HMAC):
         super().__init__(key, data, sha512_256)
 
 
+class HMAC_SHA1(HMAC):
+    TYPE = 13
+    digest_size = 20
+
+    def __init__(self, key, data):
+        if key is None:
+            raise Exception("do not use HMAC if you don't have a key")
+        super().__init__(key, data, sha1)
+
+
+class HMAC_SHA512(HMAC):
+    TYPE = 14
+    digest_size = 64
+
+    def __init__(self, key, data):
+        if key is None:
+            raise Exception("do not use HMAC if you don't have a key")
+        super().__init__(key, data, sha512)
+
+
 class GMAC(GHASH):
     TYPE = 20
     digest_size = 16