您的位置:首页 > 运维架构

snooper taoism中分步编写实现sha1_hmac

2011-01-31 10:52 218 查看
 
// snooper taoism的界面脚本支持各种函数的任意组合
// 本示例是一个利用sha1_hash等常用函数组合实现sha1_hmac过程
// 为简化流程,声明了几个用来存储临时结果的控件
// 比如 ipad, opad, tmp1, tmp2........
 
 
static static_prompt = "HMAC" position = 0% width = 100% height = 1
crlf
 
// 临时变量,上一步的结果,下一步的输入数据
edit tmp1 filename = "tmpxxxxxxxxxxxx" position = 0% width = 0% height = 1
edit tmp2 filename = "tmpxxxxxxxxxxxx" position = 0% width = 0% height = 1
edit ipad filename = "tmpxxxxxxxxxxxx" position = 0% width = 0% height = 1
edit opad filename = "tmpxxxxxxxxxxxx" position = 0% width = 0% height = 1
edit K0 filename = "tmpxxxxxxxxxxxx" position = 0% width = 0% height = 1
crlf
 
// 输入框
static static_HMAC_text = "input" position = 0% width = 100% height = 1
crlf
edit HMAC_text filename = "HMAC_text" position = 0% width = 100% height = 2
crlf
 
// 密钥
static static_HMAC_key = "key" position = 0% width = 100% height = 1
crlf
edit HMAC_key filename = "HMAC_key" position = 0% width = 100% height = 2
crlf
 
// 输出结果
static static_HMAC_output = "output" position = 0% width = 100% height = 1
crlf
edit HMAC_output filename = "HMAC_output" position = 0% width = 100% height = 2
crlf
 
// 按钮
static static_prompt_001 = "  Algorithm" position = 0% width = 100% height = 1
crlf
 
button sha1_hmac = "sha1_hmac" position = 20% width = 50% height = 1
crlf
 
// 下面是fipx中的标准数据
 
//ipad 是 64个 0x36,opad是 64个 0x5c
 
 
//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
 
 
function sha1_hmac
       // 第0步,组织ipad和opad, K0
       // ipad, 64个0x36
       ipad = dup( int( 64 ), 36 )
       // opad,64个5c
       opad = dup( int( 64 ), 5c )
 
       // 第1步,准备K0
       // K0 输入密钥右补0到64字节
       K0 = pack00_len( $hmac_key, int( 64 ) )
 
       // 第2步,K0 异或 ipad, IPAD是
       tmp1 = xor_whole( $K0, $ipad )
 
       // 第3步,异或结果 连结 上 text
       tmp1 = $tmp1         $hmac_text
 
       // 第4步,使用sha1_hash进行计算
       tmp1 = sha1_hash(       $tmp1          )
 
       // 第5步,K0 异或 opad
       tmp2 = xor_whole(    $K0,   $opad     )
 
       // 第6步,将 5、4 步进行 连结 操作
       tmp1 = $tmp2 $tmp1
 
       // 第7步,将 6 的结果进行sha1 hash
       hmac_output = sha1_hash(    $tmp1   )
end function
 
 
 
执行结果
 

第0步

//dup ( int ( 64 ) , 36 )
//----int
//----  input = 64  hex = 00 00 00 40
//
//--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
//
//-----Final 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
//dup ( int ( 64 ) , 5c )
//----int
//----  input = 64  hex = 00 00 00 40
//
//--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
//
//-----Final 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

第1步

//pack00_len ( 00010203 04050607 08090a0b 0c0d0e0f10111213 14151617 18191a1b 1c1d1e1f20212223 24252627 28292a2b 2c2d2e2f30313233 34353637 38393a3b 3c3d3e3f , int ( 64 ) )
//----int
//----  input = 64  hex = 00 00 00 40
//
//--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
//
//-----Final result --- 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

第2步

//xor_whole ( 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 , 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 )
//--xor_whole XOR data 1 to data 2, the returned data length is the length of the data 2
//--  data 1 = 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
//--  data 2 = 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
//--  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
//
//-----Final 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

第3步

//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 ansi_string ( "Sample #1" )
//--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
//
//-----Final 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 53 61 6D 70 6C 65 20 23 31

第4步

//sha1_hash ( 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 )
//--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
//
//-----Final result --- BC C2 C6 8C AB BB F1 C3 F5 B0 5D 8E 7E 73 A4 D2 7B 7E 1B 20

第5步

//xor_whole ( 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 , 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 )
//--xor_whole XOR data 1 to data 2, the returned data length is the length of the data 2
//--  data 1 = 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
//--  data 2 = 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
//--  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
//
//-----Final 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

第6步

//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
//-----Final 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 BC C2 C6 8C AB BB F1 C3 F5 B0 5D 8E 7E 73 A4 D2 7B 7E 1B 20

第7步

//sha1_hash ( 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 )
//--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
//
//-----Final result --- 4F 4C A3 D5 D6 8B A7 CC 0A 12 08 C9 C6 1E 9C 5D A0 40 3C 0A
 
 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c input output ipad alignment hex