Skip to content
This repository was archived by the owner on Apr 19, 2024. It is now read-only.

Commit 21fbf09

Browse files
committed
remove length member from Multihash
A multihash with a mismatching digest length doesn't make sense (see multiformats/multihash#21). This also leaves explicit truncation as the only way to force the shortening of a digest.
1 parent 4e6339d commit 21fbf09

File tree

1 file changed

+25
-25
lines changed

1 file changed

+25
-25
lines changed

multihash.py

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -165,41 +165,41 @@ def get_decoder(cls, encoding):
165165
Codecs.reset()
166166

167167

168-
class Multihash(namedtuple('Multihash', 'func length digest')):
169-
"""A named tuple representing multihash function, length and digest.
168+
class Multihash(namedtuple('Multihash', 'func digest')):
169+
"""A named tuple representing a multihash function and digest.
170170
171171
The hash function is a `Func` member:
172172
173-
>>> mh = Multihash(Func.sha1, 20, b'BINARY_DIGEST')
174-
>>> mh == (Func.sha1, 20, b'BINARY_DIGEST')
173+
>>> mh = Multihash(Func.sha1, b'BINARY_DIGEST')
174+
>>> mh == (Func.sha1, b'BINARY_DIGEST')
175175
True
176-
>>> mh == (mh.func, mh.length, mh.digest)
176+
>>> mh == (mh.func, mh.digest)
177177
True
178178
179179
Although it can also be its integer value (the function code) or its
180180
string name (the function name, with either underscore or hyphen):
181181
182-
>>> mhfc = Multihash(Func.sha1.value, mh.length, mh.digest)
182+
>>> mhfc = Multihash(Func.sha1.value, mh.digest)
183183
>>> mhfc == mh
184184
True
185-
>>> mhfn = Multihash('sha2-256', 32, b'...')
185+
>>> mhfn = Multihash('sha2-256', b'...')
186186
>>> mhfn.func is Func.sha2_256
187187
True
188188
189189
Application-specific codes (0x00-0x0f) are also accepted. Other codes
190190
raise a `ValueError`:
191191
192-
>>> mhfc = Multihash(0x01, 4, b'...')
192+
>>> mhfc = Multihash(0x01, b'...')
193193
>>> mhfc.func
194194
1
195-
>>> mhfc = Multihash(1234, 4, b'...')
195+
>>> mhfc = Multihash(1234, b'...')
196196
Traceback (most recent call last):
197197
...
198198
ValueError: ('invalid hash function code', 1234)
199199
"""
200200
__slots__ = ()
201201

202-
def __new__(cls, func, length, digest):
202+
def __new__(cls, func, digest):
203203
try:
204204
f = Func(func) # function or function code
205205
except ValueError as ve:
@@ -209,8 +209,7 @@ def __new__(cls, func, length, digest):
209209
f = _func_from_name[func] # function name
210210
else:
211211
raise ValueError("invalid hash function code", func)
212-
return super(cls, Multihash).__new__(
213-
cls, f, int(length), bytes(digest))
212+
return super(cls, Multihash).__new__(cls, f, bytes(digest))
214213

215214
@classmethod
216215
def from_hash(self, hash):
@@ -220,7 +219,7 @@ def from_hash(self, hash):
220219
>>> hash = hashlib.sha1(b'foo')
221220
>>> digest = hash.digest()
222221
>>> mh = Multihash.from_hash(hash)
223-
>>> mh == (Func.sha1, len(digest), digest)
222+
>>> mh == (Func.sha1, digest)
224223
True
225224
226225
If there is no matching multihash hash function for the given `hash`,
@@ -237,14 +236,14 @@ def from_hash(self, hash):
237236
except KeyError:
238237
raise ValueError("no matching multihash function", hash.name)
239238
digest = hash.digest()
240-
return Multihash(func, len(digest), digest)
239+
return Multihash(func, digest)
241240

242241
def encode(self, encoding=None):
243242
r"""Encode into a multihash-encoded digest.
244243
245244
If `encoding` is `None`, a binary digest is produced:
246245
247-
>>> mh = Multihash(0x01, 4, b'TEST')
246+
>>> mh = Multihash(0x01, b'TEST')
248247
>>> mh.encode()
249248
b'\x01\x04TEST'
250249
@@ -258,7 +257,7 @@ def encode(self, encoding=None):
258257
fc = self.func.value
259258
except AttributeError: # application-specific function code
260259
fc = self.func
261-
mhash = bytes([fc, self.length]) + self.digest
260+
mhash = bytes([fc, len(self.digest)]) + self.digest
262261
if encoding:
263262
mhash = Codecs.get_encoder(encoding)(mhash)
264263
return mhash
@@ -278,7 +277,7 @@ def verify(self, data):
278277
Application-specific hash functions are currently not supported and a
279278
`ValueError` is raised:
280279
281-
>>> mh = Multihash(0x01, 4, b'TEST')
280+
>>> mh = Multihash(0x01, b'TEST')
282281
>>> mh.verify(data)
283282
Traceback (most recent call last):
284283
...
@@ -292,27 +291,27 @@ def verify(self, data):
292291
raise ValueError("no available hash function for hash", self.func)
293292
hash.update(data)
294293
digest = bytes(hash.digest())
295-
return digest[:self.length] == self.digest
294+
return digest[:len(self.digest)] == self.digest
296295

297296
def truncate(self, length):
298297
"""Return a new `Multihash` with a shorter digest `length`.
299298
300299
If the given `length` is greater than the original, a `ValueError`
301300
is raised.
302301
303-
>>> mh1 = Multihash(0x01, 6, b'FOOBAR')
302+
>>> mh1 = Multihash(0x01, b'FOOBAR')
304303
>>> mh2 = mh1.truncate(3)
305-
>>> mh2 == (0x01, 3, b'FOO')
304+
>>> mh2 == (0x01, b'FOO')
306305
True
307306
>>> mh3 = mh1.truncate(10)
308307
Traceback (most recent call last):
309308
...
310309
ValueError: cannot enlarge the original digest by 4 bytes
311310
"""
312-
if length > self.length:
311+
if length > len(self.digest):
313312
raise ValueError("cannot enlarge the original digest by %d bytes"
314-
% (length - self.length))
315-
return self.__class__(self.func, length, self.digest[:length])
313+
% (length - len(self.digest)))
314+
return self.__class__(self.func, self.digest[:length])
316315

317316

318317
def decode(mhash, encoding=None):
@@ -322,7 +321,7 @@ def decode(mhash, encoding=None):
322321
323322
>>> mhash = b'\x11\x0a\x0b\xee\xc7\xb5\xea?\x0f\xdb\xc9]'
324323
>>> mh = decode(mhash)
325-
>>> mh == (Func.sha1, 10, mhash[2:])
324+
>>> mh == (Func.sha1, mhash[2:])
326325
True
327326
328327
If an `encoding` is specified, it is used to decode the digest
@@ -336,7 +335,8 @@ def decode(mhash, encoding=None):
336335
"""
337336
if encoding:
338337
mhash = Codecs.get_decoder(encoding)(mhash)
339-
return Multihash(int(mhash[0]), int(mhash[1]), mhash[2:])
338+
# TODO: check lenghts
339+
return Multihash(int(mhash[0]), mhash[2:])
340340

341341

342342
def _test():

0 commit comments

Comments
 (0)