Results 1 to 14 of 14

Thread: Sha1 digest

  1. #1
    Join Date
    May 2008
    Location
    Tripoli Libya
    Posts
    70
    Thanks
    10
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Sha1 digest

    hay
    is there any way to get sha1 digest similar to sha1.digest() of python :

    Qt Code:
    1. QByteArray total ("abc");
    2. QByteArray hashed = QCryptographicHash::hash(total, QCryptographicHash::Sha1);
    To copy to clipboard, switch view to plain text mode 

    dont give the same value of python sha1.digest()

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Sha1 digest

    It does give the same value but in different encoding.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    May 2008
    Location
    Tripoli Libya
    Posts
    70
    Thanks
    10
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Sha1 digest

    i know python sha1.hexdigest() give the same value of hashed.toHex()
    but what i asking for how to get the same value of python sha1.digest()


    Added after 6 minutes:


    this is the original python method what i'm trying to port it to Qt
    Qt Code:
    1. def seguid(seq):
    2. """Returns the SEGUID (string) for a sequence (string or Seq object).
    3.  
    4. Given a nucleotide or amino-acid secuence (or any string),
    5. returns the SEGUID string (A SEquence Globally Unique IDentifier).
    6. seq type = str.
    7. For more information about SEGUID, see:
    8. [url]http://bioinformatics.anl.gov/seguid/[/url]
    9. DOI: 10.1002/pmic.200600032 """
    10. try:
    11. #Python 2.5 sha1 is in hashlib
    12. import hashlib
    13. m = hashlib.sha1()
    14. except:
    15. #For older versions
    16. import sha
    17. m = sha.new()
    18. import base64
    19. try:
    20. #Assume its a Seq object
    21. seq = seq.tostring()
    22. except AttributeError:
    23. #Assume its a string
    24. pass
    25. m.update(_as_bytes(seq.upper()))
    26.  
    27. try:
    28. #For Python 3+
    29. return base64.encodebytes(m.digest()).decode().replace("\n","").rstrip("=")
    30. except AttributeError:
    31. pass
    32. try:
    33. #For Python 2.5+
    34.  
    35. return base64.b64encode(m.digest()).rstrip("=")
    36. except:
    37. #For older versions
    38. import os
    39. #Note: Using os.linesep doesn't work on Windows,
    40. #where os.linesep= "\r\n" but the encoded string
    41. #contains "\n" but not "\r\n"
    42. return base64.encodestring(m.digest()).replace("\n","").rstrip("=")
    To copy to clipboard, switch view to plain text mode 
    Last edited by alrawab; 8th February 2013 at 22:46.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Sha1 digest

    Qt Code:
    1. QByteArray result = QByteArray::fromHex(QCryptographicHash::hash("abc", QCryptographicHash::Sha1)).toBase64();
    2. while(result.endsWith('=')) result.chop(1);
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Sha1 digest

    If a hex dump of the 20 bytes of the hash is the same in Python and Qt the the hashes have the same value: i.e. the 20 byes in the result of sha.digest() are the same as the 20 bytes in the result of QCryptographicHash::result().

    What is your actual problem?

    Edit: Now I see the actual problem

  6. The following user says thank you to ChrisW67 for this useful post:

    alrawab (8th February 2013)

  7. #6
    Join Date
    May 2008
    Location
    Tripoli Libya
    Posts
    70
    Thanks
    10
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Sha1 digest

    Quote Originally Posted by wysota View Post
    Qt Code:
    1. QByteArray result = QByteArray::fromHex(QCryptographicHash::hash("abc", QCryptographicHash::Sha1)).toBase64();
    2. while(result.endsWith('=')) result.chop(1);
    To copy to clipboard, switch view to plain text mode 
    thanks wysota this line return bg the original code returns PAG9uybzWLqyfyZ5JKosmgP8/bg

  8. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Sha1 digest

    Qt Code:
    1. QByteArray result = QCryptographicHash::hash(QByteArray("abc").toUpper(), QCryptographicHash::Sha1).toBase64();
    2. while(result.endsWith('=')) result.chop(1);
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. The following user says thank you to wysota for this useful post:

    alrawab (8th February 2013)

  10. #8
    Join Date
    May 2008
    Location
    Tripoli Libya
    Posts
    70
    Thanks
    10
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Sha1 digest

    works fine thanks a lot

  11. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Sha1 digest

    Quote Originally Posted by alrawab View Post
    works fine thanks a lot
    Now please don't stop at copying my code into yours, clapping your hands how fine you managed to solve your problem. Analyze what the code does and why it didn't work the first time you tried to do it yourself.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  12. #10
    Join Date
    May 2008
    Location
    Tripoli Libya
    Posts
    70
    Thanks
    10
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Sha1 digest

    Analyze what the code does .. I know what is does thank you
    you right you need things done well do it yourself

  13. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Sha1 digest

    Quote Originally Posted by alrawab View Post
    Analyze what the code does .. I know what is does thank you
    Great. So why did QCryptographicHash::hash() and sha1.digest() return different results?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  14. #12
    Join Date
    May 2008
    Location
    Tripoli Libya
    Posts
    70
    Thanks
    10
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Sha1 digest

    encoding issue check the hex value for Ascii Tables:
    http://www.ascii.ca/cp864.htm
    http://en.wikipedia.org/wiki/ASCII
    str(m.digest()).encode("hex") is_equal to hashed.toHex()
    http://stackoverflow.com/questions/1...version-c-c-qt
    Qt Code:
    1. Windows-1256
    To copy to clipboard, switch view to plain text mode 
    Last edited by alrawab; 9th February 2013 at 22:41.

  15. #13
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Sha1 digest

    The proper answer is that they didn't return different results.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  16. #14
    Join Date
    May 2008
    Location
    Tripoli Libya
    Posts
    70
    Thanks
    10
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Sha1 digest

    first what i miss is to convert the QByteArray to upper case (It happens...... From time to time)
    the 2nd my win 7 was set to use windows-1256 (RTL semitic languages)
    on RH6 gives the same resault
    Screenshot.jpg

Similar Threads

  1. QImage::fill can't digest what I feed it
    By darenw in forum Newbie
    Replies: 6
    Last Post: 2nd August 2012, 19:21
  2. How to get sha1 hash
    By lyuts in forum Qt Programming
    Replies: 3
    Last Post: 15th February 2012, 15:35

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.