Snooper taoism中的hmac功能

来源:互联网 发布:mac迅雷破解插件 编辑:程序博客网 时间:2024/06/06 15:02

hmac(可能)有多种,是一种对原有hash进行增强的hash算法,因为有了一个key的参与,key的长度可变,所以将被碰撞到的概率可能(本质还是个hash)降低了。

 

fips中有一个sha1_hmac的流程与标准数据值,在流程上主要是  对齐、异或、连接、hash 这4个操作的反复重复。

 

简单说一下,密钥key不足64字节右边补0到64字节,超过64字节先hash然后再补0,如果正好64字节就不用处理了,处理的结果叫k0

 

ipad 是 64个 0x36,opad是 64个 0x5c

 

第一步 k0 xor ipad 结果为 tmp1

第二步 tmp1 后面连上明文

第三步 对第二步进行hash

第四步 k0 xor opad 连上第三步的结果

第五步 对第四步进行hash

 

下面是fips中的标准数据

A.1 SHA-1 with 64-Byte Key

Text: "Sample #1"

 


Key:

00010203 04050607 08090a0b 0c0d0e0f
10111213 14151617 18191a1b 1c1d1e1f
20212223 24252627 28292a2b 2c2d2e2f
30313233 34353637 38393a3b 3c3d3e3f

K0:

00010203 04050607 08090a0b 0c0d0e0f
10111213 14151617 18191a1b 1c1d1e1f
20212223 24252627 28292a2b 2c2d2e2f
30313233 34353637 38393a3b 3c3d3e3f

K0 xor ipad:
36373435 32333031 3e3f3c3d 3a3b3839
26272425 22232021 2e2f2c2d 2a2b2829
16171415 12131011 1e1f1c1d 1a1b1819
06070405 02030001 0e0f0c0d 0a0b0809

(Key xor ipad)||text:
36373435 32333031 3e3f3c3d 3a3b3839
26272425 22232021 2e2f2c2d 2a2b2829
16171415 12131011 1e1f1c1d 1a1b1819
06070405 02030001 0e0f0c0d 0a0b0809
53616d70 6c652023 31

Hash((Key xor ipad)||text):
bcc2c68c abbbf1c3 f5b05d8e 7e73a4d2
7b7e1b20


K0 xor opad:
5c5d5e5f 58595a5b 54555657 50515253
4c4d4e4f 48494a4b 44454647 40414243
7c7d7e7f 78797a7b 74757677 70717273
6c6d6e6f 68696a6b 64656667 60616263

(K0 xor opad) || Hash((Key xor ipad)||text):
5c5d5e5f 58595a5b 54555657 50515253
4c4d4e4f 48494a4b 44454647 40414243
7c7d7e7f 78797a7b 74757677 70717273
6c6d6e6f 68696a6b 64656667 60616263
bcc2c68c abbbf1c3 f5b05d8e 7e73a4d2
7b7e1b20

HMAC(Key, Text) = Hash((K0 xor opad) || Hash((Key xor ipad)||text)):
4f4ca3d5 d68ba7cc 0a1208c9 c61e9c5d
a0403c0a


20-byte HMAC(Key, Text):
4f4ca3d5 d68ba7cc 0a1208c9 c61e9c5d
a0403c0a

 

 

Snooper taoism中使用现有函数组合简单实现了一个sha1_hmac的计算功能

 

 


//
sha1_hash ( xor_whole ( dup ( int ( 64 ) , 5c ) , pack00_len ( 00010203 04050607 08090a0b 0c0d0e0f10111213 14151617 18191a1b 1c1d1e1f20212223 24252627 28292a2b 2c2d2e2f30313233 34353637 38393a3b 3c3d3e3f , int ( 64 ) ) ) sha1_hash ( xor_whole ( dup ( int ( 64 ) , 36 ) , pack00_len ( 00010203 04050607 08090a0b 0c0d0e0f10111213 14151617 18191a1b 1c1d1e1f20212223 24252627 28292a2b 2c2d2e2f30313233 34353637 38393a3b 3c3d3e3f , int ( 64 ) ) ) ansi_string ( "Sample #1" ) ) ) 
//--------
int
//--------
  input = 64  hex = 00 00 00 40 
//

//----------
int
//----------
  input = 64  hex = 00 00 00 40 
//

//----------------
int
//----------------
  input = 64  hex = 00 00 00 40 
//

//------------------
int
//------------------
  input = 64  hex = 00 00 00 40 
//

//------------------
ansi_string convert utf16-little-endian format input data to ansi format
//------------------
  input = 53 00 61 00 6D 00 70 00 6C 00 65 00 20 00 23 00 31 00 
//------------------
  output = 53 61 6D 70 6C 65 20 23 31 
//

//------
dup repeat the specified number of times the input data
//------
  result = 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 
//

//------
pack00_len implementation of alignment when input data length is not multiple times of the length
//------
  input = 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F 20 21 22 23 24 25 26 27 28 29 2A 2B 2C 2D 2E 2F 30 31 32 33 34 35 36 37 38 39 3A 3B 3C 3D 3E 3F 
//------
  output = 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F 20 21 22 23 24 25 26 27 28 29 2A 2B 2C 2D 2E 2F 30 31 32 33 34 35 36 37 38 39 3A 3B 3C 3D 3E 3F 
//

//----------
dup repeat the specified number of times the input data
//----------
  result = 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 
//

//----------
pack00_len implementation of alignment when input data length is not multiple times of the length
//----------
  input = 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F 20 21 22 23 24 25 26 27 28 29 2A 2B 2C 2D 2E 2F 30 31 32 33 34 35 36 37 38 39 3A 3B 3C 3D 3E 3F 
//----------
  output = 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F 20 21 22 23 24 25 26 27 28 29 2A 2B 2C 2D 2E 2F 30 31 32 33 34 35 36 37 38 39 3A 3B 3C 3D 3E 3F 
//

//----
xor_whole XOR data 1 to data 2, the returned data length is the length of the data 2
//----
  data 1 = 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 
//----
  data 2 = 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F 20 21 22 23 24 25 26 27 28 29 2A 2B 2C 2D 2E 2F 30 31 32 33 34 35 36 37 38 39 3A 3B 3C 3D 3E 3F 
//----
  result = 5C 5D 5E 5F 58 59 5A 5B 54 55 56 57 50 51 52 53 4C 4D 4E 4F 48 49 4A 4B 44 45 46 47 40 41 42 43 7C 7D 7E 7F 78 79 7A 7B 74 75 76 77 70 71 72 73 6C 6D 6E 6F 68 69 6A 6B 64 65 66 67 60 61 62 63 
//

//------
xor_whole XOR data 1 to data 2, the returned data length is the length of the data 2
//------
  data 1 = 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 
//------
  data 2 = 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F 20 21 22 23 24 25 26 27 28 29 2A 2B 2C 2D 2E 2F 30 31 32 33 34 35 36 37 38 39 3A 3B 3C 3D 3E 3F 
//------
  result = 36 37 34 35 32 33 30 31 3E 3F 3C 3D 3A 3B 38 39 26 27 24 25 22 23 20 21 2E 2F 2C 2D 2A 2B 28 29 16 17 14 15 12 13 10 11 1E 1F 1C 1D 1A 1B 18 19 06 07 04 05 02 03 00 01 0E 0F 0C 0D 0A 0B 08 09 
//

//----
sha1_hash
//----
  input = 
//--------
36 37 34 35 32 33 30 31 3E 3F 3C 3D 3A 3B 38 39 26 27 24 25 22 23 20 21 2E 2F 2C 2D 2A 2B 28 29 
//--------
16 17 14 15 12 13 10 11 1E 1F 1C 1D 1A 1B 18 19 06 07 04 05 02 03 00 01 0E 0F 0C 0D 0A 0B 08 09 
//--------
53 61 6D 70 6C 65 20 23 31 
//----
  output = BC C2 C6 8C AB BB F1 C3 F5 B0 5D 8E 7E 73 A4 D2 7B 7E 1B 20 
//

//--
sha1_hash
//--
  input = 
//------
5C 5D 5E 5F 58 59 5A 5B 54 55 56 57 50 51 52 53 4C 4D 4E 4F 48 49 4A 4B 44 45 46 47 40 41 42 43 
//------
7C 7D 7E 7F 78 79 7A 7B 74 75 76 77 70 71 72 73 6C 6D 6E 6F 68 69 6A 6B 64 65 66 67 60 61 62 63 
//------
BC C2 C6 8C AB BB F1 C3 F5 B0 5D 8E 7E 73 A4 D2 7B 7E 1B 20 
//--
  output = 4F 4C A3 D5 D6 8B A7 CC 0A 12 08 C9 C6 1E 9C 5D A0 40 3C 0A 
//

原创粉丝点击