Form sha1.hexdigest to sha1.digest

Discussion in 'Python' started by LMZ, Apr 6, 2008.

  1. LMZ

    LMZ Guest

    How can convert string from sha1.hexdigest() to string that is the
    same, like from sha1.digest()

    thanks for your help!

    Alexandr.
     
    LMZ, Apr 6, 2008
    #1
    1. Advertising

  2. > How can convert string from sha1.hexdigest() to string that is the
    > same, like from sha1.digest()


    Use binascii.unhexlify.

    HTH,
    Martin
     
    Martin v. Löwis, Apr 6, 2008
    #2
    1. Advertising

  3. LMZ

    Guest

    Martin v. Löwis napisa³(a):
    > > How can convert string from sha1.hexdigest() to string that is the
    > > same, like from sha1.digest()

    >
    > Use binascii.unhexlify.
    >
    > HTH,
    > Martin


    Or hexdigest_string.decode('hex')
     
    , Apr 6, 2008
    #3
  4. > Or hexdigest_string.decode('hex')

    I would advise against this, as it's incompatible with Python 3.

    Regards,
    Martin
     
    Martin v. Löwis, Apr 6, 2008
    #4
  5. LMZ

    Guest

    Martin v. Löwis napisa³(a):
    > > Or hexdigest_string.decode('hex')

    >
    > I would advise against this, as it's incompatible with Python 3.


    I didn't know that, you actually made me look it up in the Python 3
    FAQ. And yes, the difference is that decode will return bytes type
    instead of a string. This may or may not be a problem (bytes type is
    supposed to be immutable, so it can be used in many places where a
    string is used now, ex. as a dict key).
     
    , Apr 6, 2008
    #5
  6. >>> Or hexdigest_string.decode('hex')
    >> I would advise against this, as it's incompatible with Python 3.

    >
    > I didn't know that, you actually made me look it up in the Python 3
    > FAQ. And yes, the difference is that decode will return bytes type
    > instead of a string.


    No. The decode method on string objects is removed, you can only
    *encode* strings, but not decode them.

    > This may or may not be a problem


    The problem is this:

    py> hashlib.sha1(b"Hallo").hexdigest().decode("hex")
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    AttributeError: 'str' object has no attribute 'decode'

    Regards,
    Martin
     
    Martin v. Löwis, Apr 6, 2008
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. gert
    Replies:
    11
    Views:
    1,938
    Paul Rubin
    Jan 17, 2009
  2. Adam Tauno Williams
    Replies:
    2
    Views:
    869
    Stefan Behnel
    Dec 30, 2010
  3. Warren Seltzer

    make test-all fails on digest/sha1

    Warren Seltzer, Jan 28, 2008, in forum: Ruby
    Replies:
    1
    Views:
    183
    Warren Seltzer
    Jan 28, 2008
  4. Srikanth Jeeva
    Replies:
    2
    Views:
    730
    Srikanth Jeeva
    Feb 24, 2009
  5. Replies:
    2
    Views:
    333
    Julian Cromarty
    Jun 26, 2013
Loading...

Share This Page