blob: 1634733fea0cb429ae30192920d66625c501b02b (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
/*
* kbiffcrypt.cpp
* Copyright (C) 2005 Michael Hendricks <[email protected]>
*
* This file contains the implementation of the functions in
* the KBiffCrypt namespace.
*
* $Id$
*/
#include "kbiffcrypt.h"
#include <kmdcodec.h>
#include <ntqcstring.h>
TQString KBiffCrypt::hmac_md5(const TQString& text, const TQString& k)
{
// If the original key is too long, the new key will be a hash of
// the original key. Then the new key might have NULL bytes in it,
// so we can't use TQCString
TQByteArray key = (TQByteArray)k.utf8();
KMD5 context; // for calculating MD5 sums
KMD5::Digest digest; // intermediate storage for MD5 sums
// inner and outer padding (key XORd with ipad and opad, respectively)
TQByteArray k_ipad(64);
TQByteArray k_opad(64);
// if key is longer than 64 bytes reset it to key=MD5(key)
if (key.size() > 64) {
// cast to a TQCString because we don't want to hash the
// trailing NULL byte
KMD5 tctx((TQCString)key);
key.duplicate((char*)tctx.rawDigest(), 16);
}
/* the HMAC-MD5 transform looks like this:
*
* MD5(K XOR opad, MD5(K XOR ipad, text))
*
* where K is an n byte key
* ipad is the byte 0x36 repeated 64 times
* opad is the byte 0x5c repeated 64 times
* text is the data being protected
*/
// XOR key with ipad and opad values, copying
// the pad values after the key's end
for (unsigned int i=0; i<64; i++) {
if( i < key.size() ) {
k_ipad[i] = key[i] ^ 0x36;
k_opad[i] = key[i] ^ 0x5c;
} else {
k_ipad[i] = 0x36;
k_opad[i] = 0x5c;
}
}
// perform inner MD5
context.reset(); // init context for 1st pass
context.update(k_ipad); // start with inner pad
context.update(text.utf8()); // then text of datagram
context.rawDigest(digest); // finish up 1st pass
// perform outer MD5
context.reset(); // init context for 2nd pass
context.update(k_opad); // start with outer pad
context.update(digest, 16); // then results of 1st hash
return context.hexDigest(); // finish up 2nd pass and return
}
|