在 Python 中将十六进制转换为整数 - 相当于 C# 中的 ToInt64

Converting a hex to integer in Python - equivalent for ToInt64 in C#

我正在尝试将 sha256 哈希转换为 integer.This 在 C# 中工作,但我无法在 Python 中工作。

C# Version :
string inputString = "TestString";
            SHA256 sha256Hash = SHA256.Create();
            byte[] bytes = sha256Hash.ComputeHash(Encoding.UTF8.GetBytes(inputString));
            //Read the Bytes and print
            var sb = new StringBuilder(bytes.Length * 2);
            sb = new StringBuilder(bytes.Length * 2);
            foreach (byte b in bytes)
            {
                sb.Append(b.ToString("x2"));
            }
            Console.WriteLine("Bytes: " + sb);            
            Console.WriteLine("ConvertedInt: " + BitConverter.ToInt64(bytes));
Output :
Bytes: 6dd79f2770a0bb38073b814a5ff000647b37be5abbde71ec9176c6ce0cb32a27
ConvertedInt: 4088037490330425197

上面按预期打印字节和整数值。尝试在 Python 中执行相同操作但无法转换为相同的整数。

import hashlib
hashobj = hashlib.sha256("TestString".encode('utf-8')).hexdigest()
print(hashobj)

#Tried Option 1:
print(f"Option 1 prints:\r")
print(int(hashobj,32))

#Tried Option 2:
print(f"Option 1 prints:\r")
print(int(hashobj,16))

Output:
6dd79f2770a0bb38073b814a5ff000647b37be5abbde71ec9176c6ce0cb32a27
Option 1 prints:
428476861264242379186014021585258195649378281633207959348811042267114033125399631735390859241543
Option 1 prints:
49683071055471546380350462257487304408748464885741562355821390565893091830311

如何在 python 中将 hashobj 转换为 64 位 int?只是想在 python 中获得相同的整数值,感谢您的帮助。

编辑 1:

根据@Sweeper 和@Recursing 的回答,能够继续进行,但在对结果整数进行签名时遇到问题。
情况 1:十六进制到整数是 +ve,当我尝试对 int(8bytes) 和一个数字取模时,比如 300,给出预期结果 108.
案例 2 和案例 3:十六进制到整数是 -ve,然后我必须读取可变字节数,案例 2 为 7 个字节,案例 3 为 9 个字节,然后取模 300 给出预期结果 108.

问题:整数为-ve时,如何确定读取的字节数,得到相同的结果?谢谢。

#Case 1
hash_bytes = hashlib.sha256("098C10227K3R".encode('utf-8')).digest()
print("Case 1 : Is Positive : {}".format(int.from_bytes(hash_bytes[:8], byteorder="little", signed=True) > 0))
print("Case 1 : IntegerValue : {}".format(int.from_bytes(hash_bytes[:8], byteorder="little", signed=True)))
print("Case 1 : 8 Bytes:")
print(int.from_bytes(hash_bytes[:8], byteorder="little", signed=True) % 300)

#Case 2
hash_bytes = hashlib.sha256("159YK282MS3T".encode('utf-8')).digest()
print("Case 2 : Is Positive : {}".format(int.from_bytes(hash_bytes[:8], byteorder="little", signed=True) > 0))
print("Case 2 : IntegerValue : {}".format(int.from_bytes(hash_bytes[:8], byteorder="little", signed=True)))
print("Case 2 : 8 Bytes:")
print(int.from_bytes(hash_bytes[:8], byteorder="little", signed=True) % 300)
print("Case 2 : 7 Bytes:")
print(int.from_bytes(hash_bytes[:7], byteorder="little", signed=True) % 300)

#Case 3
print("Case 3:")
hash_bytes = hashlib.sha256("17FK427W501L".encode('utf-8')).digest()
print("Case 3 : Is Positive : {}".format(int.from_bytes(hash_bytes[:8], byteorder="little", signed=True) > 0))
print("Case 3 : IntegerValue : {}".format(int.from_bytes(hash_bytes[:8], byteorder="little", signed=True)))
print("Case 3 - 8 Bytes:")
print(int.from_bytes(hash_bytes[:8], byteorder="little", signed=True) % 300)
print("Case 3 - 9 Bytes:")
print(int.from_bytes(hash_bytes[:9], byteorder="little", signed=True) % 300)

Output:
Case 1 : Is Positive : True
Case 1 : IntegerValue : 9212408962392255108
Case 1 : 8 Bytes:
108
Case 2 : Is Positive : False
Case 2 : IntegerValue : -5822649536180381508
Case 2 : 8 Bytes:
192
Case 2 : 7 Bytes:
Case 3 : IntegerValue 7 bytes: 14015580891781308
108
Case 3:
Case 3 : Is Positive : False
Case 3 : IntegerValue : -2669588811718081008
Case 3 - 8 Bytes:
192
Case 3 - 9 Bytes:
Case 3 : IntegerValue 9 bytes: -445391446580747319792
108

正如@Sweeper 所说,C# 代码仅保留哈希的前 8 个字节,并将其转换为整数

在 python 你可以做:

import hashlib
hash_bytes = hashlib.sha256("TestString".encode('utf-8')).digest()
first_bytes = hash_bytes[:8]
print(int.from_bytes(first_bytes, byteorder="little", signed=True))

另一种方法是使用标准库中的 struct 模块:https://docs.python.org/3/library/struct.html

print(struct.unpack("q", first_bytes))