在Python中使用hashlib检测可用的非标准哈希算法

时间:2012-03-14 15:56:29

标签: python hash python-2.7 checksum ripemd

根据Python文档,hashlib模块(MD5和SHA ***)只支持一些哈希算法。如何检测其他算法是否可用? (比如RIPEMD-160)当然,我可以尝试使用文档中的RIPEMD-160示例来使用它,但我不确定它会如何抱怨。它是否会抛出异常,如果是,是哪个异常?

1 个答案:

答案 0 :(得分:4)

只需在shell中试用:

>>> h = hashlib.new('ripemd161')                                                                                                                     
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/hashlib.py", line 124, in __hash_new
    return __get_builtin_constructor(name)(string)
  File "/usr/lib/python2.7/hashlib.py", line 91, in __get_builtin_constructor
    raise ValueError('unsupported hash type %s' % name)
ValueError: unsupported hash type ripemd161