V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
panlilu
V2EX  ›  Python

python中 hashlib库里的 md5.digest() 和 md5.hexdigest()的值的转换问题

  •  
  •   panlilu ·
    panlilu · 2011-07-05 23:05:10 +08:00 · 32724 次点击
    这是一个创建于 4677 天前的主题,其中的信息可能已经有所发展或是发生改变。
    在python中执行这样的代码:

    md5 = hashlib.md5('adsf')
    md5.digest() //返回: '\x05\xc1*(s48l\x94\x13\x1a\xb8\xaa\x00\xd0\x8a'
    md5.hexdigest() //返回: '05c12a287334386c94131ab8aa00d08a'

    现在我的需求是已知 md5.hexdigest() 的结果求md5.hexdigest()的结果
    例如实现这样一个函数:
    convertMd5( '05c12a287334386c94131ab8aa00d08a')
    //能返回 '\x05\xc1*(s48l\x94\x13\x1a\xb8\xaa\x00\xd0\x8a'

    没有什么思路,望各位指点。
    15 条回复    1970-01-01 08:00:00 +08:00
    panlilu
        1
    panlilu  
    OP
       2011-07-05 23:16:03 +08:00
    勘误 已知 md5.hexdigest() 的结果求md5.digest() 的结果。。
    Hyperion
        2
    Hyperion  
       2011-07-05 23:23:51 +08:00
    import binascii
    t = binascii.a2b_hex('05c12a287334386c94131ab8aa00d08a')
    print t
    panlilu
        3
    panlilu  
    OP
       2011-07-05 23:25:49 +08:00
    @Hyperion 万分感谢!!!
    gastlygem
        4
    gastlygem  
       2011-07-05 23:26:33 +08:00
    提示:

    for c in md5.digest():
    print hex(ord(c))
    imonyse
        5
    imonyse  
       2011-07-05 23:27:00 +08:00
    import binascii
    binascii.unhexlify("05c12a287334386c94131ab8aa00d08a")

    http://docs.python.org/library/binascii.html
    gastlygem
        6
    gastlygem  
       2011-07-05 23:27:51 +08:00
    偶慢了好多啊。另外Hyperion的答案好多了……
    panlilu
        7
    panlilu  
    OP
       2011-07-05 23:28:07 +08:00
    @gastlygem 楼上已经帮忙解答啦。。我其实原来就是不知道这个'\x'开头的是什么东西。。谢谢提示。
    Hyperion
        8
    Hyperion  
       2011-07-05 23:29:48 +08:00
    = =+ python新手一只…… 除了几个我经常用标准库其余都没记住……

    话说很期待楼主的tuojin内测啊……
    panlilu
        9
    panlilu  
    OP
       2011-07-05 23:30:28 +08:00
    谢谢楼上各位大牛相助。这贴可以结啦~

    v2ex上果然多python牛人!
    reus
        10
    reus  
       2011-07-06 01:43:55 +08:00
    什么都不用import,直接用hex编码来decode
    '05c12a287334386c94131ab8aa00d08a'.decode('hex')
    est
        11
    est  
       2011-07-06 02:04:53 +08:00
    @reus 正解!

    但是我比较讨厌hex编码有一点:必须要求字符为偶数长度。
    Hyperion
        12
    Hyperion  
       2011-07-06 13:40:27 +08:00
    @reus 原来encode就可以了... 只知道可以用来编码转换, 原来hex也可以转...

    没有文档活不下去星人表示压力很大... _codecs的介绍很少啊...
    reus
        13
    reus  
       2011-07-06 14:15:16 +08:00
    http://docs.python.org/library/codecs.html 这里都有的,还有base64,bz2,zip,quote,rot13之类,很方便
    Hyperion
        14
    Hyperion  
       2011-07-06 15:37:37 +08:00
    @reus = = 我一直查的是Module Docs... 囧
    reus
        15
    reus  
       2011-07-06 19:42:36 +08:00
    @est 非偶数可以先rjust/ljust一下
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3360 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 12:13 · PVG 20:13 · LAX 05:13 · JFK 08:13
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.