From 8d26a572ddb08f48a460b9db1cd195113bf16a6f Mon Sep 17 00:00:00 2001
From: shortcutme <tamas@zeronet.io>
Date: Tue, 3 Oct 2017 15:22:05 +0200
Subject: [PATCH] Hashlib-like truncated sha512/256 object

---
 src/Crypt/CryptHash.py | 31 +++++++++++++++++--------------
 1 file changed, 17 insertions(+), 14 deletions(-)

diff --git a/src/Crypt/CryptHash.py b/src/Crypt/CryptHash.py
index 39459bcc..118053b6 100644
--- a/src/Crypt/CryptHash.py
+++ b/src/Crypt/CryptHash.py
@@ -44,20 +44,23 @@ def random(length=64, encoding="hex"):
         return hashlib.sha512(os.urandom(256)).hexdigest()[0:length]
 
 
+# Sha512 truncated to 256bits
+class Sha512t:
+    def __init__(self, data):
+        if data:
+            self.sha512 = hashlib.sha512(data)
+        else:
+            self.sha512 = hashlib.sha512()
 
-if __name__ == "__main__":
-    import cStringIO as StringIO
-    a = StringIO.StringIO()
-    a.write("hello!")
-    a.seek(0)
-    print hashlib.sha1("hello!").hexdigest()
-    print sha1sum(a)
+    def hexdigest(self):
+        return self.sha512.hexdigest()[0:64]
 
-    import time
-    s = time.time()
-    print sha1sum(open("F:\\Temp\\bigfile")),
-    print time.time() - s
+    def digest(self):
+        return self.sha512.digest()[0:32]
 
-    s = time.time()
-    print sha512sum(open("F:\\Temp\\bigfile")),
-    print time.time() - s
+    def update(self, data):
+        return self.sha512.update(data)
+
+
+def sha512t(data=None):
+    return Sha512t(data)