-1
鑑於諸如一個非常大的整數是否可以將SHA 256位散列作爲整數存儲在BIGINT列中?
>>> import hashlib
>>> h = hashlib.sha256("foo").hexdigest()
>>> h
'2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae'
>>> i = int(h, 16)
>>> i
19970150736239713706088444570146546354146685096673408908105596072151101138862L
我試圖創造的SQLite版本3.7.13表如:
sqlite> .schema sha_table
CREATE TABLE "sha_table" (
"id" integer NOT NULL PRIMARY KEY,
"sha_hash" UNSIGNED BIG INT NOT NULL
);
sqlite> INSERT INTO `sha_table` (`sha_hash`) VALUES (19970150736239713706088444570146546354146685096673408908105596072151101138862);
sqlite> SELECT * FROM `sha_table`;
1|1.99701507362397e+76
試圖這個數字轉換回期望整數/六角不起作用:
>>> i = int(1.99701507362397e+76)
>>> i
19970150736239699946838208148745496378851447158029907897771645036831291998208L
>>> "{:0>64x}".format(i)
'2c26b46b68ffbe00000000000000000000000000000000000000000000000000'
編輯:從Python sqlite3的客戶端嘗試似乎不工作,要麼:
>>> cursor.execute("SELECT sha_hash FROM sha_table")
>>> i = int(cursor.fetchone()[0])
>>> i
19970150736239716016218650738648251798472370569655933119801582864759645011968L
>>>> "{:0>64x}".format(i)
'2c26b46b68ffbe00000000000000000000000000000000000000000000000000'
謝謝!
你是否對查詢的實際返回值調用了'int()',或者是否複製了科學記數法並將其粘貼到另一行中? – Hyperboreus
@Hyperboreus我剛剛在shell中嘗試過它,使用sqlite3客戶端更新了一個示例,它顯示了相同的結果。謝謝! –
什麼是'type(cursor.fetchone()[0])'? – Hyperboreus