密码学 SHA1哈希密码

2020-07-31 13:40 更新

简介

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

试验

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

步骤

以下是SHA-1算法的伪代码:

  1. Note: All variables are unsigned 32 bits and wrap modulo 232when calculating
  2. İniyorlar variables:
  3. h0:= 0x67452301
  4. h1:= 0xEFCDAB89
  5. h2:= 0x98BADCFE
  6. h3:= 0x10325476
  7. h4:= 0xC3D2E1F0
  8. Pre-processing:
  9. append the bit '1' to the message
  10. append k bits '0', where k is the minimum number >= 0 such that the resulting message
  11. length (in bits) is congruent to 448(mod 512)
  12. append length of message (before pre-processing), in bits, as 64-bit big-endian integer
  13. Process the message in successive 512-bit chunks:
  14. break message into 512-bit chunks
  15. for each chunk
  16. break chunk into sixteen 32-bit big-endian words w[i], 0 i 15
  17. Extend the sixteen 32-bit words into eighty 32-bit words:
  18. for i from 16 to 79
  19. w[i]:= (w[i-3] xor w[i-8] xor w[i-14] xor w[i-16]) leftrotate 1
  20. Initialize hash value for this chunk:
  21. a:= h0
  22. b:= h1
  23. c:= h2
  24. d:= h3
  25. e:= h4
  26. Main loop:
  27. for i from 0 to 79
  28. if 0 i 19 then
  29. f:= (b and c) or ((not b) and d)
  30. k:= 0x5A827999
  31. else if 20 i 39
  32. f:= b xor c xor d
  33. k:= 0x6ED9EBA1
  34. else if 40 i 59
  35. f:= (b and c) or (b and d) or(c and d)
  36. k:= 0x8F1BBCDC
  37. else if 60 i 79
  38. f:= b xor c xor d
  39. k:= 0xCA62C1D6
  40. temp:= (a leftrotate 5) + f + e + k + w[i]
  41. e:= d
  42. d:= c
  43. c:= b leftrotate 30
  44. b:= a
  45. a:= temp
  46. Add this chunk's hash to result so far:
  47. h0:= h0 + a
  48. h1:= h1 + b
  49. h2:= h2 + c
  50. h3:= h3 + d
  51. h4:= h4 + e
  52. Produce the final hash value (big-endian):
  53. digest = hash = h0 append h1 append h2 append h3 append h4
  54. 上述关于f表达式列于FIPS PUB 180-1中,以下替代表达式也许也能在主要循环里计算f:
  55. (0 ≤ i ≤ 19): f:= d xor (b and (c xor d)) (alternative)
  56. (40 ≤ i ≤ 59): f:= (b and c) or (d and (b or c))
  57. (alternative 1)(40 ≤ i ≤ 59): f:= (b and c) or (d and (b xor c))
  58. (alternative 2)(40 ≤ i ≤ 59): f:= (b and c) + (d and (b xor c)) (alternative 3)
  59. 按照算法实现的 SHA-1 功能,可以方便的生成字符串文本的 hash 值。
以上内容是否对您有帮助:
在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号