@@ -20,14 +20,21 @@ def __init__(self):
2020 self .reset ()
2121
2222 def absorb (self , trits , offset = 0 , length = None ):
23+ # Pad input if necessary, so that it can be divided evenly into
24+ # hashes.
25+ pad = ((len (trits ) % TRIT_HASH_LENGTH ) or TRIT_HASH_LENGTH )
26+ trits += [0 ] * (TRIT_HASH_LENGTH - pad )
27+
2328 if length is None :
2429 length = len (trits )
2530
26- if length % 243 :
31+ if length < 1 :
2732 raise with_context (
28- exc = ValueError ('Illegal length (s/b divisible by 243) .' ),
33+ exc = ValueError ('Invalid length passed to ``absorb`` .' ),
2934
3035 context = {
36+ 'trits' : trits ,
37+ 'offset' : offset ,
3138 'length' : length ,
3239 },
3340 )
@@ -49,12 +56,24 @@ def absorb(self, trits, offset=0, length=None):
4956
5057 offset += TRIT_HASH_LENGTH
5158
52- def squeeze (self , trits , offset = 0 , length = TRIT_HASH_LENGTH ):
53- if length % 243 :
59+ def squeeze (self , trits , offset = 0 , length = None ):
60+ # Pad input if necessary, so that it can be divided evenly into
61+ # hashes.
62+ pad = ((len (trits ) % TRIT_HASH_LENGTH ) or TRIT_HASH_LENGTH )
63+ trits += [0 ] * (TRIT_HASH_LENGTH - pad )
64+
65+ if length is None :
66+ # By default, we will try to squeeze one hash.
67+ # Note that this is different than ``absorb``.
68+ length = len (trits ) or TRIT_HASH_LENGTH
69+
70+ if length < 1 :
5471 raise with_context (
55- exc = ValueError ('Illegal length (s/b divisible by 243) .' ),
72+ exc = ValueError ('Invalid length passed to ``squeeze`` .' ),
5673
5774 context = {
75+ 'trits' : trits ,
76+ 'offset' : offset ,
5877 'length' : length ,
5978 },
6079 )
0 commit comments