Python hashlib.blake2b() Examples
The following are 30
code examples of hashlib.blake2b().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
hashlib
, or try the search function
.
Example #1
Source File: types.py From py-scale-codec with GNU General Public License v3.0 | 6 votes |
def create_from_account_list(cls, accounts, threshold): from scalecodec.utils.ss58 import ss58_decode account_ids = [] for account in accounts: if account[0:2] != '0x': account = '0x{}'.format(ss58_decode(account)) account_ids.append(account) account_list_cls = cls.get_decoder_class('Vec<AccountId>') account_list_data = account_list_cls.encode(sorted(account_ids)) threshold_data = cls.get_decoder_class("u16").encode(threshold) multi_account_id = "0x{}".format(blake2b( b"modlpy/utilisuba" + bytes(account_list_data.data) + bytes(threshold_data.data), digest_size=32 ).digest().hex()) multi_account_obj = cls() multi_account_obj.encode(multi_account_id) return multi_account_obj
Example #2
Source File: _uploader.py From dephell with MIT License | 6 votes |
def _get_hashes(path: Path) -> Dict[str, str]: sha256_manager = hashlib.sha256() md5_manager = hashlib.md5() if hasattr(hashlib, 'blake2b'): blake2_manager = hashlib.blake2b(digest_size=32) else: blake2_manager = None with path.open('rb') as stream: while True: data = stream.read(65536) if not data: break sha256_manager.update(data) md5_manager.update(data) if blake2_manager: blake2_manager.update(data) return dict( md5_digest=md5_manager.hexdigest(), sha256_digest=sha256_manager.hexdigest(), blake2_256_digest=blake2_manager.hexdigest(), )
Example #3
Source File: test_hash.py From gigantum-client with MIT License | 6 votes |
def test_hash_same_as_nonchunked(self, event_loop, mock_dataset_with_manifest): ds, manifest, working_dir = mock_dataset_with_manifest sh = SmartHash(ds.root_dir, manifest.cache_mgr.cache_root, manifest.dataset_revision) cache_dir = manifest.cache_mgr.cache_root revision = manifest.dataset_revision filename = "test1.txt" helper_append_file(cache_dir, revision, filename, "asdfdsfgkdfshuhwedfgft345wfd" * 100000) assert sh.fast_hash_data == {} assert sh.is_cached(filename) is False assert os.path.exists(os.path.join(cache_dir, revision, ".smarthash")) is False hash_result = await sh.hash([filename]) hash_result = hash_result[0] h = blake2b() with open(sh.get_abs_path(filename), 'rb') as fh: h.update(fh.read()) assert hash_result == h.hexdigest()
Example #4
Source File: code_object.py From reactivepy with BSD 3-Clause "New" or "Revised" License | 6 votes |
def __init__(self, code: str, key: bytes, ns_manager: BuiltInManager): self.symbol_table: symtable = symtable(code, '<string>', 'exec') self.code: str = code self.input_vars: List[SymbolWrapper] = CodeObject._find_input_variables(self.symbol_table, ns_manager ) self.output_vars: FrozenSet[SymbolWrapper] = CodeObject._find_output_variables(self.symbol_table ) h = blake2b(digest_size=10, key=key) if len(self.output_vars) > 0: display_id_prefix = "+".join(map(str, self.output_vars)) h.update(display_id_prefix.encode('utf-8')) self.display_id = f"{display_id_prefix}-{h.hexdigest()}" else: h.update(self.code.encode('utf-8')) self.display_id = f"{h.hexdigest()}"
Example #5
Source File: ss58.py From py-scale-codec with GNU General Public License v3.0 | 6 votes |
def ss58_encode(address, address_type=42): checksum_prefix = b'SS58PRE' if type(address) is bytes or type(address) is bytearray: address_bytes = address else: address_bytes = bytes.fromhex(address) if len(address_bytes) == 32: # Checksum size is 2 bytes for public key checksum_length = 2 elif len(address_bytes) in [1, 2, 4, 8]: # Checksum size is 1 byte for account index checksum_length = 1 else: raise ValueError("Invalid length for address") address_format = bytes([address_type]) + address_bytes checksum = blake2b(checksum_prefix + address_format).digest() return base58.b58encode(address_format + checksum[:checksum_length]).decode()
Example #6
Source File: ss58.py From polkascan-pre-harvester with GNU General Public License v3.0 | 6 votes |
def ss58_encode(address, address_type=42): checksum_prefix = b'SS58PRE' if type(address) is bytes or type(address) is bytearray: address_bytes = address else: address_bytes = bytes.fromhex(address) if len(address_bytes) == 32: # Checksum size is 2 bytes for public key checksum_length = 2 elif len(address_bytes) in [1, 2, 4, 8]: # Checksum size is 1 byte for account index checksum_length = 1 else: raise ValueError("Invalid length for address") address_format = bytes([address_type]) + address_bytes checksum = blake2b(checksum_prefix + address_format).digest() return base58.b58encode(address_format + checksum[:checksum_length]).decode()
Example #7
Source File: crypto.py From tinydecred with ISC License | 6 votes |
def __init__(self, pw=None): """ Args: pw (byte-like): A password that deterministically generates the key. """ super().__init__() if not pw: self.key = None self.keyParams = None return # If a password was provided, create a new set of key parameters and an # authentication code. salt = rando.newKey() _, hashName, iterations = defaultKDFParams() b = ByteArray( hashlib.pbkdf2_hmac(hashName, bytes(pw), salt.bytes(), iterations) ) self.key = b[:32] self.keyParams = KDFParams(salt) authKey = b[32:].bytes() authMsg = self.keyParams.baseParams().bytes() self.keyParams.auth = ByteArray( hashlib.blake2b(authMsg, digest_size=32, key=authKey).digest() )
Example #8
Source File: compat.py From web3-gear with MIT License | 6 votes |
def sign(self, key): '''Sign this transaction with a private key. A potentially already existing signature would be overridden. ''' h = blake2b(digest_size=32) h.update(rlp.encode(self, ThorTransaction.exclude(["Signature"]))) rawhash = h.digest() if key in (0, "", b"\x00" * 32, "0" * 64): raise Exception("Zero privkey cannot sign") if len(key) == 64: key = to_bytes(hexstr=key) # we need a binary key pk = keys.PrivateKey(key) self.Signature = pk.sign_msg_hash(rawhash).to_bytes() # # estimate eth gas #
Example #9
Source File: routine.py From TopicNet with MIT License | 6 votes |
def blake2bchecksum(file_path): """ Calculates hash of the file Parameters ---------- file_path : str path to the file """ with open(file_path, 'rb') as fh: m = hashlib.blake2b() while True: data = fh.read(8192) if not data: break m.update(data) return m.hexdigest()
Example #10
Source File: progress.py From kopf with MIT License | 6 votes |
def make_key(self, key: Union[str, handlers.HandlerId], max_length: int = 63) -> str: # K8s has a limitation on the allowed charsets in annotation/label keys. # https://kubernetes.io/docs/concepts/overview/working-with-objects/annotations/#syntax-and-character-set safe_key = key.replace('/', '.') # K8s has a limitation of 63 chars per annotation/label key. # Force it to 63 chars by replacing the tail with a consistent hash (with full alphabet). # Force it to end with alnums instead of altchars or trailing chars (K8s requirement). prefix = f'{self.prefix}/' if self.prefix else '' if len(safe_key) <= max_length - len(prefix): suffix = '' else: digest = hashlib.blake2b(safe_key.encode('utf-8'), digest_size=4).digest() alnums = base64.b64encode(digest, altchars=b'-.').decode('ascii') suffix = f'-{alnums}'.rstrip('=-.') full_key = f'{prefix}{safe_key[:max_length - len(prefix) - len(suffix)]}{suffix}' return full_key
Example #11
Source File: ss58.py From polkascan-pre-harvester with GNU General Public License v3.0 | 5 votes |
def ss58_decode(address, valid_address_type=42): checksum_prefix = b'SS58PRE' ss58_format = base58.b58decode(address) if ss58_format[0] != valid_address_type: raise ValueError("Invalid Address type") # Public keys has a two byte checksum, account index 1 byte if len(ss58_format) in [3, 4, 6, 10]: checksum_length = 1 elif len(ss58_format) in [5, 7, 11, 35]: checksum_length = 2 elif len(ss58_format) in [8, 12]: checksum_length = 3 elif len(ss58_format) in [9, 13]: checksum_length = 4 elif len(ss58_format) in [14]: checksum_length = 5 elif len(ss58_format) in [15]: checksum_length = 6 elif len(ss58_format) in [16]: checksum_length = 7 elif len(ss58_format) in [17]: checksum_length = 8 else: raise ValueError("Invalid address length") checksum = blake2b(checksum_prefix + ss58_format[0:-checksum_length]).digest() if checksum[0:checksum_length] != ss58_format[-checksum_length:]: raise ValueError("Invalid checksum") return ss58_format[1:len(ss58_format)-checksum_length].hex()
Example #12
Source File: ss58.py From py-substrate-interface with GNU General Public License v3.0 | 5 votes |
def ss58_encode(address, address_type=42): """ Encodes an account ID to an Substrate address according to provided address_type Parameters ---------- address address_type Returns ------- """ checksum_prefix = b'SS58PRE' if type(address) is bytes or type(address) is bytearray: address_bytes = address else: address_bytes = bytes.fromhex(address.replace('0x', '')) if len(address_bytes) == 32: # Checksum size is 2 bytes for public key checksum_length = 2 elif len(address_bytes) in [1, 2, 4, 8]: # Checksum size is 1 byte for account index checksum_length = 1 else: raise ValueError("Invalid length for address") address_format = bytes([address_type]) + address_bytes checksum = blake2b(checksum_prefix + address_format).digest() return base58.b58encode(address_format + checksum[:checksum_length]).decode()
Example #13
Source File: server.py From pheweb with GNU Affero General Public License v3.0 | 5 votes |
def get_hash(cls, plaintext): import hashlib return hashlib.blake2b(plaintext.encode('utf8'), digest_size=cls._hash_length, key=app.config['SECRET_KEY'].encode('utf8')).hexdigest()
Example #14
Source File: validators.py From graham_discord_bot with MIT License | 5 votes |
def validate_checksum_xrb(address: str) -> bool: """Given an xrb/nano/ban address validate the checksum""" if (address[:5] == 'nano_' and len(address) == 65) or (address[:4] in ['ban_', 'xrb_'] and len(address) == 64): # Populate 32-char account index account_map = "13456789abcdefghijkmnopqrstuwxyz" account_lookup = {} for i in range(0, 32): account_lookup[account_map[i]] = BitArray(uint=i, length=5) # Extract key from address (everything after prefix) acrop_key = address[4:-8] if address[:5] != 'nano_' else address[5:-8] # Extract checksum from address acrop_check = address[-8:] # Convert base-32 (5-bit) values to byte string by appending each 5-bit value to the bitstring, essentially bitshifting << 5 and then adding the 5-bit value. number_l = BitArray() for x in range(0, len(acrop_key)): number_l.append(account_lookup[acrop_key[x]]) number_l = number_l[4:] # reduce from 260 to 256 bit (upper 4 bits are never used as account is a uint256) check_l = BitArray() for x in range(0, len(acrop_check)): check_l.append(account_lookup[acrop_check[x]]) check_l.byteswap() # reverse byte order to match hashing format # verify checksum h = blake2b(digest_size=5) h.update(number_l.bytes) return h.hexdigest() == check_l.hex return False
Example #15
Source File: dataset_view.py From QCFractal with BSD 3-Clause "New" or "Revised" License | 5 votes |
def hash(self) -> str: """ Returns the Blake2b hash of the view """ b2b = hashlib.blake2b() with open(self._path, "rb") as f: for chunk in iter(lambda: f.read(8192), b""): b2b.update(chunk) return b2b.hexdigest()
Example #16
Source File: rate_limiter.py From starbelly with MIT License | 5 votes |
def get_domain_token(domain): ''' Get a token for a domain. :param str domain: The domain to generate a token for. :returns: The token corresponding to the domain. :rtype: bytes ''' hash_ = hashlib.blake2b(domain.encode('ascii'), digest_size=16) token = hash_.digest() return token
Example #17
Source File: bitpacking.py From randovania with GNU General Public License v3.0 | 5 votes |
def single_byte_hash(data: bytes) -> int: return hashlib.blake2b(data, digest_size=1).digest()[0]
Example #18
Source File: layout_description.py From randovania with GNU General Public License v3.0 | 5 votes |
def _shareable_hash_bytes(self) -> bytes: bytes_representation = json.dumps(self._serialized_patches).encode() return hashlib.blake2b(bytes_representation, digest_size=5).digest()
Example #19
Source File: services.py From nano-dpow with MIT License | 5 votes |
def hash_key(x: str): m = hashlib.blake2b() m.update(x.encode("utf-8")) return m.digest()
Example #20
Source File: ss58.py From py-scale-codec with GNU General Public License v3.0 | 5 votes |
def ss58_decode(address, valid_address_type=None): checksum_prefix = b'SS58PRE' ss58_format = base58.b58decode(address) if valid_address_type and ss58_format[0] != valid_address_type: raise ValueError("Invalid Address type") # Determine checksum length according to length of address string if len(ss58_format) in [3, 4, 6, 10]: checksum_length = 1 elif len(ss58_format) in [5, 7, 11, 35]: checksum_length = 2 elif len(ss58_format) in [8, 12]: checksum_length = 3 elif len(ss58_format) in [9, 13]: checksum_length = 4 elif len(ss58_format) in [14]: checksum_length = 5 elif len(ss58_format) in [15]: checksum_length = 6 elif len(ss58_format) in [16]: checksum_length = 7 elif len(ss58_format) in [17]: checksum_length = 8 else: raise ValueError("Invalid address length") checksum = blake2b(checksum_prefix + ss58_format[0:-checksum_length]).digest() if checksum[0:checksum_length] != ss58_format[-checksum_length:]: raise ValueError("Invalid checksum") return ss58_format[1:len(ss58_format)-checksum_length].hex()
Example #21
Source File: crypto.py From tinydecred with ISC License | 5 votes |
def rekey(pw, kp): """ Regenerate a key using its origin key parameters, as returned by `params`. Args: pw (bytes-like): The key password. kp (KDFParams): The key parameters from the original generation of the key being regenerated. Returns: SecretKey: The regenerated key. """ sk = SecretKey() func = kp.kdfFunc if func != "pbkdf2_hmac": raise DecredError("unknown key derivation function") b = ByteArray( hashlib.pbkdf2_hmac(kp.hashName, bytes(pw), bytes(kp.salt), kp.iterations) ) # Get the authentication message and key create the MAC tag to compare # with the included key parameters. authKey = b[32:].bytes() authMsg = kp.baseParams().bytes() auth = hashlib.blake2b(authMsg, digest_size=32, key=authKey).digest() if auth != kp.auth: raise DecredError("rekey auth check failed") sk.key = b[:32] sk.keyParams = kp return sk
Example #22
Source File: block.py From py-scale-codec with GNU General Public License v3.0 | 5 votes |
def generate_hash(self): if self.contains_transaction: return blake2b(self.data.data, digest_size=32).digest().hex()
Example #23
Source File: hasher.py From py-substrate-interface with GNU General Public License v3.0 | 5 votes |
def blake2_128_concat(data): """ Helper function to calculate a 16 bytes Blake2b hash for provided data, concatenated with data, used as key for Substrate storage items Parameters ---------- data Returns ------- """ return "{}{}".format(blake2b(data, digest_size=16).digest().hex(), data.hex())
Example #24
Source File: metadata.py From vessel-classification with Apache License 2.0 | 5 votes |
def stable_hash(x): x = six.ensure_binary(x) digest = hashlib.blake2b(six.ensure_binary(x)).hexdigest()[-8:] return int(digest, 16)
Example #25
Source File: resources.py From forseti-security with Apache License 2.0 | 5 votes |
def size_t_hash(key): """Hash the key using size_t. Args: key (str): The key to hash. Returns: str: The hashed key. """ hash_digest = hashlib.blake2b(key.encode()).hexdigest() # pylint: disable=no-member return '%u' % ctypes.c_size_t(int(hash_digest, 16)).value
Example #26
Source File: dpow_server.py From nano-dpow with MIT License | 5 votes |
def hash_key(x: str): m = hashlib.blake2b() m.update(x.encode("utf-8")) return m.digest()
Example #27
Source File: media.py From showroom with MIT License | 5 votes |
def blake2bsum(filename): b2bhash = hashlib.blake2b(digest_size=32) try: return hashsum(b2bhash, filename) except FileNotFoundError: return ""
Example #28
Source File: blake.py From beacon_chain with MIT License | 5 votes |
def blake(x): return blake2b(x).digest()[:32]
Example #29
Source File: hash_ssz.py From beacon_chain with MIT License | 5 votes |
def hash(x): return blake2b(x).digest()[:32]
Example #30
Source File: time_test.py From beacon_chain with MIT License | 5 votes |
def hash(x): return blake2b(x).digest()[:32]