donglao4370 2012-12-21 13:10
浏览 117

在Python中模拟PHP的sha1原始输出

I am using Django to make an e-shop. I am trying to add online payment with http://liqpay.com/. They have examples in PHP: https://liqpay.com/?do=pages&p=cnb12.

In the example they use PHP's shal($data, 1) flag 1 (then the sha1 digest is instead returned in raw binary format with a length of 20).

But the Python function hashlib.sha1(), does not have this flag.

Is there some analog in Python?

  • 写回答

2条回答 默认 最新

  • doudai8783 2012-12-21 13:22
    关注

    Python hashlib objects can provide you with either the raw bytes or a hex digest, depending on the method used. Calling .digest() gives you raw bytes:

    >>> import hashlib
    >>> data = 'foobar'
    >>> hashlib.sha1(data).digest()
    '\x88C\xd7\xf9$\x16!\x1d\xe9\xeb\xb9c\xffL\xe2\x81%\x93(x'
    >>> len(hashlib.sha1(data).digest())
    20
    

    If you want to get the hex digest, use the .hexdigest() method instead:

    >>> hashlib.sha1(data).hexdigest()
    '8843d7f92416211de9ebb963ff4ce28125932878'
    
    评论

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站