【问题标题】:from php to django for pack('H32',$challenge) and md5("\0" . $word . $challenge)从 php 到 django for pack('H32',$challenge) 和 md5("\0" . $word . $challenge)
【发布时间】:2011-04-07 04:43:49
【问题描述】:

我在 PHP 中一个接一个地调用了以下函数:

$chal = pack('H32',$challenge);
$word = md5("\0" . $word . $chal);

我想在 Django python 中做同样的事情。 请问您有什么建议吗?

【问题讨论】:

    标签: php python django md5 pack


    【解决方案1】:

    这种方法产生与原始 php 相同的结果(至少在 x86_64 上)。

    import hashlib
    import struct
    
    cha1 = struct.pack('16B', *[int(c, 16) for c in (challenge[i:i+2]
        for i in xrange(0, len(challenge), 2))])
    
    word = hashlib.md5('\0%s%s' % (word, cha1)).hexdigest()
    

    Python 的 struct.pack() 方法没有 4 位大小(单个十六进制字符)的格式说明符,这似乎大致是 php 的 pack() 正在做的事情。因此,您必须将原始挑战字符串分成 2 个字符的块,并将每个块解释为十六进制八位字节。

    【讨论】:

    • 非常感谢!我想知道为什么 pack 会给我错误的结果,现在我明白了
    【解决方案2】:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-06-07
      • 1970-01-01
      • 1970-01-01
      • 2019-02-13
      • 2017-10-04
      • 2017-11-06
      • 1970-01-01
      • 2016-04-08
      相关资源
      最近更新 更多