codecamp

密码学 SHA1哈希密码

简介

SHA-1(英语:Secure Hash Algorithm 1,中文名:安全散列算法1)是一种密码散列函数,美国国家安全局设计,并由美国国家标准技术研究所(NIST)发布为联邦数据处理标准(FIPS)。SHA-1可以生成一个被称为消息摘要的160位(20字节)散列值,散列值通常的呈现形式为40个十六进制数。

试验

在以下链接中进行实验 加密工具

步骤

以下是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
上述关于f表达式列于FIPS PUB 180-1中,以下替代表达式也许也能在主要循环里计算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)
按照算法实现的 SHA-1 功能,可以方便的生成字符串文本的 hash 值。
密码学 MD5算法
密码学 公匙密码思想
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录

第六章 公钥密码学和离散对数

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }