Forking what is left of ZeroNet and hopefully adding an AT Proto Frontend/Proxy
at main 56 lines 1.5 kB view raw
1import hashlib 2import os 3import base64 4 5 6def sha512sum(file, blocksize=65536, format="hexdigest"): 7 if type(file) is str: # Filename specified 8 file = open(file, "rb") 9 hash = hashlib.sha512() 10 for block in iter(lambda: file.read(blocksize), b""): 11 hash.update(block) 12 13 # Truncate to 256bits is good enough 14 if format == "hexdigest": 15 return hash.hexdigest()[0:64] 16 else: 17 return hash.digest()[0:32] 18 19 20def sha256sum(file, blocksize=65536): 21 if type(file) is str: # Filename specified 22 file = open(file, "rb") 23 hash = hashlib.sha256() 24 for block in iter(lambda: file.read(blocksize), b""): 25 hash.update(block) 26 return hash.hexdigest() 27 28 29def random(length=64, encoding="hex"): 30 if encoding == "base64": # Characters: A-Za-z0-9 31 hash = hashlib.sha512(os.urandom(256)).digest() 32 return base64.b64encode(hash).decode("ascii").replace("+", "").replace("/", "").replace("=", "")[0:length] 33 else: # Characters: a-f0-9 (faster) 34 return hashlib.sha512(os.urandom(256)).hexdigest()[0:length] 35 36 37# Sha512 truncated to 256bits 38class Sha512t: 39 def __init__(self, data): 40 if data: 41 self.sha512 = hashlib.sha512(data) 42 else: 43 self.sha512 = hashlib.sha512() 44 45 def hexdigest(self): 46 return self.sha512.hexdigest()[0:64] 47 48 def digest(self): 49 return self.sha512.digest()[0:32] 50 51 def update(self, data): 52 return self.sha512.update(data) 53 54 55def sha512t(data=None): 56 return Sha512t(data)