2012-07-25 85 views
0

在這個問題:驗證bcrypt散列?

Can someone explain how BCrypt verifies a hash?

伊恩·博伊德寫道,在他的回答中結束:

有了這些知識,你現在可以驗證密碼correctbatteryhorsestapler對保存的哈希: $ 2A $ 12 $ mACnM5lzNigHMaf7O1py1OLCBgGL4tYUF0N/4rS9CwDsI7ytwL4D6

我用下面的Perl程序試圖驗證這個哈希:

use Crypt::Eksblowfish::Bcrypt qw(bcrypt); 
my $password = "correctbatteryhorsestapler"; 
my $hash = '$2a$12$mACnM5lzNigHMaf7O1py1OLCBgGL4tYUF0N/4rS9CwDsI7ytwL4D6'; 

print "Verifying password $password<br>with hash $hash<BR><BR>"; 

my $new_hash = bcrypt($password,$hash); 

print "<pre>Original hash: " . $hash . "<br>" . "New hash:  " . $new_hash . "</pre><br>"; 

if ($hash ne $new_hash) { 
    print "No match."; 
} 

我的程序的輸出如下:

Verifying password correctbatteryhorsestapler 
with hash $2a$12$mACnM5lzNigHMaf7O1py1OLCBgGL4tYUF0N/4rS9CwDsI7ytwL4D6 

Original hash: $2a$12$mACnM5lzNigHMaf7O1py1OLCBgGL4tYUF0N/4rS9CwDsI7ytwL4D6 
New hash:  $2a$12$mACnM5lzNigHMaf7O1py1O3vlf6.BA8k8x3IoJ.Tq3IB/2e7g61Km 


No match. 

不知,基於上述信息,任何人都可以告訴我,我做錯了,我的Perl腳本(爲什麼它不」 t正確驗證密碼)?如果我用我的系統生成散列,我得到一個匹配:

Verifying password correctbatteryhorsestapler 
with hash $2a$12$mACnM5lzNigHMaf7O1py1O3vlf6.BA8k8x3IoJ.Tq3IB/2e7g61Km 

Original hash: $2a$12$mACnM5lzNigHMaf7O1py1O3vlf6.BA8k8x3IoJ.Tq3IB/2e7g61Km 
New hash:  $2a$12$mACnM5lzNigHMaf7O1py1O3vlf6.BA8k8x3IoJ.Tq3IB/2e7g61Km 

我得到兩個不同的系統相同的結果(一個CentOS的運行eksblowfish 0.009和其他的Win7 64位運行eksblowfish 0.007,兩者運行Perl 5.8.8)。

我在做的學習,所以我希望理解爲什麼這不適合我。

+0

這個散列應該是鹽嗎?如:bcrypt($ password,$ hash)' – Wug 2012-07-25 16:52:58

+0

我假設它是一種鹽,它只會以鹽的形式呈現(沒有$ 2a $ 12 $標識符+工作因子),但也許這是關鍵我的誤解?提出一個散列,bcrypt應該能夠提取鹽(這是它如何驗證,對吧?)。 – haus 2012-07-25 17:02:32

+0

鹽是最後$後的前22個字符。嘗試對$ 2a $ 12 $ mACnM5lzNigHMaf7O1py1O'進行加密。 – Wug 2012-07-25 17:13:58

回答

2

答案是這是我的錯。我在correctbatteryhorsestapler的問題中給了你錯誤的散列。

我選了一個真正的散列;但不想泄露密碼,我將其更改爲參考XKCD

我應該採取額外的時間來產生實際的哈希correctbatteryhorsestapler

$2a$12$mACnM5lzNigHMaf7O1py1O3vlf6.BA8k8x3IoJ.Tq3IB/2e7g61Km 

我的錯的混亂。在原來的問題中,我的意思是現在你已經準備好進入世界,它並不意味着它是一個真正的測試向量。


你可能想看看一些實際known test vectors

('',         '$2a$06$DCq7YPn5Rq63x1Lad4cll.', '$2a$06$DCq7YPn5Rq63x1Lad4cll.TV4S6ytwfsfvkgY8jIucDrjc8deX1s.'), 
('',         '$2a$08$HqWuK6/Ng6sg9gQzbLrgb.', '$2a$08$HqWuK6/Ng6sg9gQzbLrgb.Tl.ZHfXLhvt/SgVyWhQqgqcZ7ZuUtye'), 
('',         '$2a$10$k1wbIrmNyFAPwPVPSVa/ze', '$2a$10$k1wbIrmNyFAPwPVPSVa/zecw2BCEnBwVS2GbrmgzxFUOqW9dk4TCW'), 
('',         '$2a$12$k42ZFHFWqBp3vWli.nIn8u', '$2a$12$k42ZFHFWqBp3vWli.nIn8uYyIkbvYRvodzbfbK18SSsY.CsIQPlxO'), 
('a',         '$2a$06$m0CrhHm10qJ3lXRY.5zDGO', '$2a$06$m0CrhHm10qJ3lXRY.5zDGO3rS2KdeeWLuGmsfGlMfOxih58VYVfxe'), 
('a',         '$2a$08$cfcvVd2aQ8CMvoMpP2EBfe', '$2a$08$cfcvVd2aQ8CMvoMpP2EBfeodLEkkFJ9umNEfPD18.hUF62qqlC/V.'), 
('a',         '$2a$10$k87L/MF28Q673VKh8/cPi.', '$2a$10$k87L/MF28Q673VKh8/cPi.SUl7MU/rWuSiIDDFayrKk/1tBsSQu4u'), 
('a',         '$2a$12$8NJH3LsPrANStV6XtBakCe', '$2a$12$8NJH3LsPrANStV6XtBakCez0cKHXVxmvxIlcz785vxAIZrihHZpeS'), 
('abc',        '$2a$06$If6bvum7DFjUnE9p2uDeDu', '$2a$06$If6bvum7DFjUnE9p2uDeDu0YHzrHM6tf.iqN8.yx.jNN1ILEf7h0i'), 
('abc',        '$2a$08$Ro0CUfOqk6cXEKf3dyaM7O', '$2a$08$Ro0CUfOqk6cXEKf3dyaM7OhSCvnwM9s4wIX9JeLapehKK5YdLxKcm'), 
('abc',        '$2a$10$WvvTPHKwdBJ3uk0Z37EMR.', '$2a$10$WvvTPHKwdBJ3uk0Z37EMR.hLA2W6N9AEBhEgrAOljy2Ae5MtaSIUi'), 
('abc',        '$2a$12$EXRkfkdmXn2gzds2SSitu.', '$2a$12$EXRkfkdmXn2gzds2SSitu.MW9.gAVqa9eLS1//RYtYCmB1eLHg.9q'), 
('abcdefghijklmnopqrstuvwxyz',   '$2a$06$.rCVZVOThsIa97pEDOxvGu', '$2a$06$.rCVZVOThsIa97pEDOxvGuRRgzG64bvtJ0938xuqzv18d3ZpQhstC'), 
('abcdefghijklmnopqrstuvwxyz',   '$2a$08$aTsUwsyowQuzRrDqFflhge', '$2a$08$aTsUwsyowQuzRrDqFflhgekJ8d9/7Z3GV3UcgvzQW3J5zMyrTvlz.'), 
('abcdefghijklmnopqrstuvwxyz',   '$2a$10$fVH8e28OQRj9tqiDXs1e1u', '$2a$10$fVH8e28OQRj9tqiDXs1e1uxpsjN0c7II7YPKXua2NAKYvM6iQk7dq'), 
('abcdefghijklmnopqrstuvwxyz',   '$2a$12$D4G5f18o7aMMfwasBL7Gpu', '$2a$12$D4G5f18o7aMMfwasBL7GpuQWuP3pkrZrOAnqP.bmezbMng.QwJ/pG'), 
('[email protected]#$%^&*()  [email protected]#$%^&*()PNBFRD', '$2a$06$fPIsBO8qRqkjj273rfaOI.', '$2a$06$fPIsBO8qRqkjj273rfaOI.HtSV9jLDpTbZn782DC6/t7qT67P6FfO'), 
('[email protected]#$%^&*()  [email protected]#$%^&*()PNBFRD', '$2a$08$Eq2r4G/76Wv39MzSX262hu', '$2a$08$Eq2r4G/76Wv39MzSX262huzPz612MZiYHVUJe/OcOql2jo4.9UxTW'), 
('[email protected]#$%^&*()  [email protected]#$%^&*()PNBFRD', '$2a$10$LgfYWkbzEvQ4JakH7rOvHe', '$2a$10$LgfYWkbzEvQ4JakH7rOvHe0y8pHKF9OaFgwUZ2q7W2FFZmZzJYlfS'), 
('[email protected]#$%^&*()  [email protected]#$%^&*()PNBFRD', '$2a$12$WApznUOJfkEGSmYRfnkrPO', '$2a$12$WApznUOJfkEGSmYRfnkrPOr466oFDCaj4b6HY3EXGvfxm43seyhgC') 

也有測試,確保它不會對Unicode字符失敗。有在地方的實施給予UTF-8時墜毀的錯誤:

  • 密碼:ππππππππ希臘小字母pi(U + 03C0),八個重複

對於其中實際哈希值爲:

$2a$10$.TtQJ4Jr6isd4Hp.mVfZeuh6Gws4rOQ/vdBczhDx.19NFK0Y84Dle 
+0

謝謝伊恩,我感謝後續。 – haus 2012-10-09 16:46:41