密碼學(xué) SHA1哈希密碼

2020-07-31 13:40 更新

簡介

SHA-1(英語:Secure Hash Algorithm 1,中文名:安全散列算法1)是一種密碼散列函數(shù),美國國家安全局設(shè)計,并由美國國家標(biāo)準(zhǔn)技術(shù)研究所(NIST)發(fā)布為聯(lián)邦數(shù)據(jù)處理標(biāo)準(zhǔn)(FIPS)。SHA-1可以生成一個被稱為消息摘要的160位(20字節(jié))散列值,散列值通常的呈現(xiàn)形式為40個十六進(jìn)制數(shù)。

試驗

在以下鏈接中進(jìn)行實驗 加密工具

步驟

以下是SHA-1算法的偽代碼:

Note: All variables are unsigned 32 bits and wrap modulo 232when calculating
?niyorlar variables:
h0:= 0x67452301
h1:= 0xEFCDAB89
h2:= 0x98BADCFE
h3:= 0x10325476
h4:= 0xC3D2E1F0
Pre-processing:
append the bit '1' to the message
append k bits '0', where k is the minimum number >= 0 such that the resulting message
length (in bits) is congruent to 448(mod 512)
append length of message (before pre-processing), in bits, as 64-bit big-endian integer
Process the message in successive 512-bit chunks:
break message into 512-bit chunks
for each chunk
break chunk into sixteen 32-bit big-endian words w[i], 0 ≤ i ≤ 15
Extend the sixteen 32-bit words into eighty 32-bit words:
for i from 16 to 79
w[i]:= (w[i-3] xor w[i-8] xor w[i-14] xor w[i-16]) leftrotate 1
Initialize hash value for this chunk:
a:= h0
b:= h1
c:= h2
d:= h3
e:= h4
Main loop:
for i from 0 to 79
if 0 ≤ i ≤ 19 then
f:= (b and c) or ((not b) and d)
k:= 0x5A827999
else if 20 ≤ i ≤ 39
f:= b xor c xor d
k:= 0x6ED9EBA1
else if 40 ≤ i ≤ 59
f:= (b and c) or (b and d) or(c and d)
k:= 0x8F1BBCDC
else if 60 ≤ i ≤ 79
f:= b xor c xor d
k:= 0xCA62C1D6
temp:= (a leftrotate 5) + f + e + k + w[i]
e:= d
d:= c
c:= b leftrotate 30
b:= a
a:= temp
Add this chunk's hash to result so far:
h0:= h0 + a
h1:= h1 + b
h2:= h2 + c
h3:= h3 + d
h4:= h4 + e
Produce the final hash value (big-endian):
digest = hash = h0 append h1 append h2 append h3 append h4
上述關(guān)于f表達(dá)式列于FIPS PUB 180-1中,以下替代表達(dá)式也許也能在主要循環(huán)里計算f:
(0 ≤ i ≤ 19): f:= d xor (b and (c xor d)) (alternative)
(40 ≤ i ≤ 59): f:= (b and c) or (d and (b or c))
(alternative 1)(40 ≤ i ≤ 59): f:= (b and c) or (d and (b xor c))
(alternative 2)(40 ≤ i ≤ 59): f:= (b and c) + (d and (b xor c)) (alternative 3)
按照算法實現(xiàn)的 SHA-1 功能,可以方便的生成字符串文本的 hash 值。
以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號