0

I'm trying to convert a string into SHA1 checksum, which is what is used in Anki's csum field.

sfld: 'Bonjour' - Card front content without html (first part of flds, filtered).

csum: 4077833205 - A string SHA1 checksum of sfld, limited to 8 digits. PHP: (int)(hexdec(getFirstNchars(sha1($sfld), 8)))

So what I want to do here is convert the string Bonjour and get 4077833205.

However, although I tried following this post, I could not get the correct value.

int(hashlib.sha1(b"Bonjour").hexdigest(), 16) % (10**8) # 48831164

abs(hash(s)) % (10 ** 8) # 70576291

Also the digit is also not matched, and the csum value is 10 digit, although the doc says it is 8 digits. So I think I misunderstand it.

I checked my Anki database and found that the value of the column csum is 7 to 10 digits, though 10 digit is the most common.

My question is, what is the equivalent of PHP's (int)(hexdec(getFirstNchars(sha1($sfld), 8))) in Python and why is the answer above not matched to the correct value?

Community
  • 1
  • 1
Blaszard
  • 30,954
  • 51
  • 153
  • 233

1 Answers1

1

The mod function you're using in python to chop your length is in the wrong place. It's happening too late.

The php code does the sha1, chops the returned hex string to 8 chars and then converts that value to a decimal. Your python code is doing things in the wrong order. It does the sha1, converts the entire result to a decimal, and then chops that to 8 chars (with the mod).

The python equivalent of your php code is:

int(hashlib.sha1(b"Bonjour").hexdigest()[:8],16)
clockwatcher
  • 3,193
  • 13
  • 13