MCPcopy Create free account
hub / github.com/aboutcode-org/scancode-toolkit / _hash_mod

Function _hash_mod

src/commoncode/hash.py:34–68  ·  view source on GitHub ↗

Return a hasher class that returns hashes with a ``bitsize`` bit length. The interface of this class is similar to the hash module API.

(bitsize, hmodule)

Source from the content-addressed store, hash-verified

32
33
34def _hash_mod(bitsize, hmodule):
35 """
36 Return a hasher class that returns hashes with a ``bitsize`` bit length. The interface of this
37 class is similar to the hash module API.
38 """
39
40 class hasher(Hashable):
41 """A hasher class that behaves like a hashlib module."""
42
43 def __init__(self, msg=None, **kwargs):
44 """
45 Return a hasher, populated with an initial ``msg`` bytes string.
46 Close on the bitsize and hmodule
47 """
48 # length of binary digest for this hash
49 self.digest_size = bitsize // 8
50
51 # binh = binary hasher module
52 self.binh = hmodule()
53
54 # msg_len = length in bytes of the message hashed
55 self.msg_len = 0
56
57 if msg:
58 self.update(msg)
59
60 def update(self, msg=None):
61 """
62 Update this hash with a ``msg`` bytes string.
63 """
64 if msg:
65 self.binh.update(msg)
66 self.msg_len += len(msg)
67
68 return hasher
69
70
71class Hashable:

Callers 1

hash.pyFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected