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

Commit 58ff311

Browse files
committed
app-specific hash support for Multihash.{from_hash,verify}()
1 parent 7e6164f commit 58ff311

File tree

1 file changed

+8
-19
lines changed

1 file changed

+8
-19
lines changed

multihash.py

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -276,20 +276,18 @@ def from_hash(self, hash):
276276
"""Create a `Multihash` from a hashlib-compatible `hash` object.
277277
278278
>>> import hashlib
279-
>>> hash = hashlib.sha1(b'foo')
279+
>>> data = b'foo'
280+
>>> hash = hashlib.sha1(data)
280281
>>> digest = hash.digest()
281282
>>> mh = Multihash.from_hash(hash)
282283
>>> mh == (Func.sha1, digest)
283284
True
284285
285-
If there is no matching multihash hash function for the given `hash`,
286-
a `KeyError` is raised.
286+
Application-specific hash functions are also supported (see
287+
`FuncHash`).
287288
288-
>>> hash = hashlib.sha224(b'foo')
289-
>>> mh = Multihash.from_hash(hash)
290-
Traceback (most recent call last):
291-
...
292-
ValueError: ('no matching multihash function', 'sha224')
289+
If there is no matching multihash hash function for the given `hash`,
290+
a `ValueError` is raised.
293291
"""
294292
try:
295293
func = FuncHash.func_from_hash(hash)
@@ -334,18 +332,9 @@ def verify(self, data):
334332
>>> mh.verify(b'foobar')
335333
False
336334
337-
Application-specific hash functions are currently not supported and a
338-
`ValueError` is raised:
339-
340-
>>> mh = Multihash(0x01, b'TEST')
341-
>>> mh.verify(data)
342-
Traceback (most recent call last):
343-
...
344-
ValueError: ('cannot verify with app-specific hash function', 1)
335+
Application-specific hash functions are also supported (see
336+
`FuncHash`).
345337
"""
346-
if self.func not in Func:
347-
raise ValueError("cannot verify with app-specific hash function",
348-
self.func)
349338
hash = FuncHash.hash_from_func(self.func)
350339
if not hash:
351340
raise ValueError("no available hash function for hash", self.func)

0 commit comments

Comments
 (0)