2015-01-12 02:03:45 +01:00
|
|
|
import hashlib
|
2015-09-13 23:17:13 +02:00
|
|
|
import os
|
|
|
|
import base64
|
2015-01-12 02:03:45 +01:00
|
|
|
|
2015-07-12 20:36:46 +02:00
|
|
|
|
2017-10-03 15:20:50 +02:00
|
|
|
def sha512sum(file, blocksize=65536, format="hexdigest"):
|
2019-03-15 21:06:59 +01:00
|
|
|
if type(file) is str: # Filename specified
|
2015-07-12 20:36:46 +02:00
|
|
|
file = open(file, "rb")
|
|
|
|
hash = hashlib.sha512()
|
2019-03-15 21:06:59 +01:00
|
|
|
for block in iter(lambda: file.read(blocksize), b""):
|
2015-07-12 20:36:46 +02:00
|
|
|
hash.update(block)
|
2017-10-03 15:20:50 +02:00
|
|
|
|
|
|
|
# Truncate to 256bits is good enough
|
|
|
|
if format == "hexdigest":
|
|
|
|
return hash.hexdigest()[0:64]
|
|
|
|
else:
|
|
|
|
return hash.digest()[0:32]
|
|
|
|
|
2015-01-13 10:56:47 +01:00
|
|
|
|
Version 0.3.5, Rev830, Full Tor mode support with hidden services, Onion stats in Sidebar, GeoDB download fix using Tor, Gray out disabled sites in Stats page, Tor hidden service status in stat page, Benchmark sha256, Skyts tracker out expodie in, 2 new tracker using ZeroNet protocol, Keep SSL cert option between restarts, SSL Certificate pinning support for connections, Site lock support for connections, Certificate pinned connections using implicit SSL, Flood protection whitelist support, Foreign keys support for DB layer, Not support for SQL query helper, 0 length file get bugfix, Pex onion address support, Faster port testing, Faster uPnP port opening, Need connections more often on owned sites, Delay ZeroHello startup message if port check or Tor manager not ready yet, Use lockfiles to avoid double start, Save original socket on proxy monkey patching to get ability to connect localhost directly, Handle atomic write errors, Broken gevent https workaround helper, Rsa crypt functions, Plugin to Bootstrap using ZeroNet protocol
2016-01-05 00:20:52 +01:00
|
|
|
def sha256sum(file, blocksize=65536):
|
2019-03-16 02:38:38 +01:00
|
|
|
if type(file) is str: # Filename specified
|
Version 0.3.5, Rev830, Full Tor mode support with hidden services, Onion stats in Sidebar, GeoDB download fix using Tor, Gray out disabled sites in Stats page, Tor hidden service status in stat page, Benchmark sha256, Skyts tracker out expodie in, 2 new tracker using ZeroNet protocol, Keep SSL cert option between restarts, SSL Certificate pinning support for connections, Site lock support for connections, Certificate pinned connections using implicit SSL, Flood protection whitelist support, Foreign keys support for DB layer, Not support for SQL query helper, 0 length file get bugfix, Pex onion address support, Faster port testing, Faster uPnP port opening, Need connections more often on owned sites, Delay ZeroHello startup message if port check or Tor manager not ready yet, Use lockfiles to avoid double start, Save original socket on proxy monkey patching to get ability to connect localhost directly, Handle atomic write errors, Broken gevent https workaround helper, Rsa crypt functions, Plugin to Bootstrap using ZeroNet protocol
2016-01-05 00:20:52 +01:00
|
|
|
file = open(file, "rb")
|
|
|
|
hash = hashlib.sha256()
|
2019-03-15 21:06:59 +01:00
|
|
|
for block in iter(lambda: file.read(blocksize), b""):
|
Version 0.3.5, Rev830, Full Tor mode support with hidden services, Onion stats in Sidebar, GeoDB download fix using Tor, Gray out disabled sites in Stats page, Tor hidden service status in stat page, Benchmark sha256, Skyts tracker out expodie in, 2 new tracker using ZeroNet protocol, Keep SSL cert option between restarts, SSL Certificate pinning support for connections, Site lock support for connections, Certificate pinned connections using implicit SSL, Flood protection whitelist support, Foreign keys support for DB layer, Not support for SQL query helper, 0 length file get bugfix, Pex onion address support, Faster port testing, Faster uPnP port opening, Need connections more often on owned sites, Delay ZeroHello startup message if port check or Tor manager not ready yet, Use lockfiles to avoid double start, Save original socket on proxy monkey patching to get ability to connect localhost directly, Handle atomic write errors, Broken gevent https workaround helper, Rsa crypt functions, Plugin to Bootstrap using ZeroNet protocol
2016-01-05 00:20:52 +01:00
|
|
|
hash.update(block)
|
|
|
|
return hash.hexdigest()
|
|
|
|
|
|
|
|
|
2015-09-13 23:17:13 +02:00
|
|
|
def random(length=64, encoding="hex"):
|
|
|
|
if encoding == "base64": # Characters: A-Za-z0-9
|
|
|
|
hash = hashlib.sha512(os.urandom(256)).digest()
|
2019-03-15 21:06:59 +01:00
|
|
|
return base64.b64encode(hash).decode("ascii").replace("+", "").replace("/", "").replace("=", "")[0:length]
|
2015-09-13 23:17:13 +02:00
|
|
|
else: # Characters: a-f0-9 (faster)
|
|
|
|
return hashlib.sha512(os.urandom(256)).hexdigest()[0:length]
|
|
|
|
|
|
|
|
|
2017-10-03 15:22:05 +02:00
|
|
|
# Sha512 truncated to 256bits
|
|
|
|
class Sha512t:
|
|
|
|
def __init__(self, data):
|
|
|
|
if data:
|
|
|
|
self.sha512 = hashlib.sha512(data)
|
|
|
|
else:
|
|
|
|
self.sha512 = hashlib.sha512()
|
2015-09-13 23:17:13 +02:00
|
|
|
|
2017-10-03 15:22:05 +02:00
|
|
|
def hexdigest(self):
|
|
|
|
return self.sha512.hexdigest()[0:64]
|
|
|
|
|
|
|
|
def digest(self):
|
|
|
|
return self.sha512.digest()[0:32]
|
|
|
|
|
|
|
|
def update(self, data):
|
|
|
|
return self.sha512.update(data)
|
|
|
|
|
|
|
|
|
|
|
|
def sha512t(data=None):
|
|
|
|
return Sha512t(data)
|