~lovesyao/+junk/transgs

« back to all changes in this revision

Viewing changes to nazo/hmac_md5.d

  • Committer: Nazo
  • Date: 2008-10-18 08:26:14 UTC
  • Revision ID: lovesyao@hotmail.com-20081018082614-22qgtg2gsotz5r2i
initial checkin

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 * hmacMd5を取得するAPIです。
 
3
 * History:
 
4
 *          0.0.0.2 高速化
 
5
 *          0.0.0.1 バージョン付け開始
 
6
 * Authors: Nazo
 
7
 * Version: 0.0.0.2
 
8
 * License: Public Domain
 
9
 */
 
10
module nazo.hmac_md5;
 
11
 
 
12
import nazo.mem, std.md5, std.base64, std.string, std.stdio;
 
13
 
 
14
static ulong[8] k_ipad=toMultiArr_ubyte!(typeof(k_ipad[0]),0x36);
 
15
static typeof(k_ipad[0])[8] k_opad=toMultiArr_ubyte!(typeof(k_ipad[0]),0x5c);
 
16
static int before_tmplen=0;
 
17
 
 
18
///hmacMd5(キーを使ったハッシュ)の取得
 
19
///マルチスレッドで呼ばないでください><
 
20
string hmacMd5(string data,string key){
 
21
  ubyte[16] re;
 
22
 
 
23
  if(key.length%8){
 
24
    key = key ~ \0\0\0\0\0\0\0\0[0..8-key.length%8];
 
25
  }
 
26
  assert(key.length%8 == 0);
 
27
 
 
28
  if(key.length>64){//未テスト
 
29
    std.md5.sum(re,cast(void[])key);
 
30
    key=cast(string)re;
 
31
  }
 
32
 
 
33
  typeof(k_ipad[0])[] tmp=cast(typeof(k_ipad[0])[])key;
 
34
  int i;
 
35
  for(;i<tmp.length;i++){
 
36
    k_ipad[i]=tmp[i]^toMultiArr_ubyte!(typeof(k_ipad[0]),0x36);
 
37
    k_opad[i]=tmp[i]^toMultiArr_ubyte!(typeof(k_opad[0]),0x5c);
 
38
  }
 
39
  for(;i<before_tmplen;i++){
 
40
    k_ipad[i]=toMultiArr_ubyte!(typeof(k_ipad[0]),0x36);
 
41
    k_opad[i]=toMultiArr_ubyte!(typeof(k_opad[0]),0x5c);
 
42
  }
 
43
 
 
44
  MD5_CTX context;
 
45
  context.start();
 
46
  context.update(k_ipad);
 
47
  context.update(data);
 
48
  context.finish(re);
 
49
  context.start();
 
50
  context.update(k_opad);
 
51
  context.update(re);
 
52
  context.finish(re);
 
53
  before_tmplen=tmp.length;
 
54
  return std.base64.encode(cast(string)re);
 
55
}