PDA

View Full Version : Sha1 digest



alrawab
8th February 2013, 22:24
hay
is there any way to get sha1 digest similar to sha1.digest() of python :


QByteArray total ("abc");
QByteArray hashed = QCryptographicHash::hash(total, QCryptographicHash::Sha1);

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

wysota
8th February 2013, 22:30
It does give the same value but in different encoding.

alrawab
8th February 2013, 22:46
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


def seguid(seq):
"""Returns the SEGUID (string) for a sequence (string or Seq object).

Given a nucleotide or amino-acid secuence (or any string),
returns the SEGUID string (A SEquence Globally Unique IDentifier).
seq type = str.
For more information about SEGUID, see:
http://bioinformatics.anl.gov/seguid/
DOI: 10.1002/pmic.200600032 """
try:
#Python 2.5 sha1 is in hashlib
import hashlib
m = hashlib.sha1()
except:
#For older versions
import sha
m = sha.new()
import base64
try:
#Assume its a Seq object
seq = seq.tostring()
except AttributeError:
#Assume its a string
pass
m.update(_as_bytes(seq.upper()))

try:
#For Python 3+
return base64.encodebytes(m.digest()).decode().replace("\n","").rstrip("=")
except AttributeError:
pass
try:
#For Python 2.5+

return base64.b64encode(m.digest()).rstrip("=")
except:
#For older versions
import os
#Note: Using os.linesep doesn't work on Windows,
#where os.linesep= "\r\n" but the encoded string
#contains "\n" but not "\r\n"
return base64.encodestring(m.digest()).replace("\n","").rstrip("=")

wysota
8th February 2013, 22:53
QByteArray result = QByteArray::fromHex(QCryptographicHash::hash("abc", QCryptographicHash::Sha1)).toBase64();
while(result.endsWith('=')) result.chop(1);

ChrisW67
8th February 2013, 22:56
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

alrawab
8th February 2013, 23:01
QByteArray result = QByteArray::fromHex(QCryptographicHash::hash("abc", QCryptographicHash::Sha1)).toBase64();
while(result.endsWith('=')) result.chop(1);

thanks wysota this line return bg the original code returns PAG9uybzWLqyfyZ5JKosmgP8/bg

wysota
8th February 2013, 23:21
QByteArray result = QCryptographicHash::hash(QByteArray("abc").toUpper(), QCryptographicHash::Sha1).toBase64();
while(result.endsWith('=')) result.chop(1);

alrawab
8th February 2013, 23:25
:) works fine thanks a lot

wysota
8th February 2013, 23:27
:) 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.

alrawab
8th February 2013, 23:40
Analyze what the code does .. I know what is does thank you
you right you need things done well do it yourself

wysota
9th February 2013, 00:51
Analyze what the code does .. I know what is does thank you

Great. So why did QCryptographicHash::hash() and sha1.digest() return different results?

alrawab
9th February 2013, 22:28
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/1258718/hex-to-string-conversion-c-c-qt

Windows-1256

wysota
10th February 2013, 10:27
The proper answer is that they didn't return different results.

alrawab
11th February 2013, 06:18
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
8705