diff options
author | George Hazan <george.hazan@gmail.com> | 2013-08-15 20:00:29 +0000 |
---|---|---|
committer | George Hazan <george.hazan@gmail.com> | 2013-08-15 20:00:29 +0000 |
commit | 623722f7cc4c20d2b7d8df03035801acacda6018 (patch) | |
tree | d4c3686f935c12c9e9274cc3706bef12d9a20d86 /src/mir_core | |
parent | 8588345b7b8ff89bf90a216dbed2302fcf3ccdbc (diff) |
mir_sha1_byte_t => BYTE;
mir_sha1_long_t => ULONG;
mir_hmac_sha1 went to core
git-svn-id: http://svn.miranda-ng.org/main/trunk@5707 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c
Diffstat (limited to 'src/mir_core')
-rw-r--r-- | src/mir_core/mir_core.def | 1 | ||||
-rw-r--r-- | src/mir_core/sha1.cpp | 44 |
2 files changed, 42 insertions, 3 deletions
diff --git a/src/mir_core/mir_core.def b/src/mir_core/mir_core.def index 037afb2aaf..3ec58865c6 100644 --- a/src/mir_core/mir_core.def +++ b/src/mir_core/mir_core.def @@ -229,3 +229,4 @@ mir_writeLogVA @226 mir_writeLogVW @227
bin2hex @228
bin2hexW @229
+mir_hmac_sha1 @230
diff --git a/src/mir_core/sha1.cpp b/src/mir_core/sha1.cpp index 9e1b376d92..5e82a3ce5b 100644 --- a/src/mir_core/sha1.cpp +++ b/src/mir_core/sha1.cpp @@ -94,7 +94,7 @@ MIR_CORE_DLL(void) mir_sha1_init(mir_sha1_ctx *ctx) ctx->W[i] = 0;
}
-MIR_CORE_DLL(void) mir_sha1_append(mir_sha1_ctx *ctx, mir_sha1_byte_t *dataIn, int len)
+MIR_CORE_DLL(void) mir_sha1_append(mir_sha1_ctx *ctx, const BYTE *dataIn, int len)
{
/* Read the data into W and process blocks as they get full
*/
@@ -110,7 +110,7 @@ MIR_CORE_DLL(void) mir_sha1_append(mir_sha1_ctx *ctx, mir_sha1_byte_t *dataIn, i }
}
-MIR_CORE_DLL(void) mir_sha1_finish(mir_sha1_ctx *ctx, mir_sha1_byte_t hashout[20])
+MIR_CORE_DLL(void) mir_sha1_finish(mir_sha1_ctx *ctx, BYTE hashout[20])
{
unsigned char pad0x80 = 0x80;
unsigned char pad0x00 = 0x00;
@@ -145,7 +145,7 @@ MIR_CORE_DLL(void) mir_sha1_finish(mir_sha1_ctx *ctx, mir_sha1_byte_t hashout[20 mir_sha1_init(ctx);
}
-MIR_CORE_DLL(void) mir_sha1_hash(mir_sha1_byte_t *dataIn, int len, mir_sha1_byte_t hashout[20])
+MIR_CORE_DLL(void) mir_sha1_hash(BYTE *dataIn, int len, BYTE hashout[20])
{
mir_sha1_ctx ctx;
@@ -153,3 +153,41 @@ MIR_CORE_DLL(void) mir_sha1_hash(mir_sha1_byte_t *dataIn, int len, mir_sha1_byte mir_sha1_append(&ctx, dataIn, len);
mir_sha1_finish(&ctx, hashout);
}
+
+/////////////////////////////////////////////////////////////////////////////////////////
+
+MIR_CORE_DLL(void) mir_hmac_sha1(BYTE hashout[MIR_SHA1_HASH_SIZE], const BYTE *key, size_t keylen, const BYTE *text, size_t textlen)
+{
+ const unsigned SHA_BLOCKSIZE = 64;
+
+ BYTE mdkey[MIR_SHA1_HASH_SIZE], k_ipad[SHA_BLOCKSIZE], k_opad[SHA_BLOCKSIZE];
+ mir_sha1_ctx ctx;
+
+ if (keylen > SHA_BLOCKSIZE) {
+ mir_sha1_init(&ctx);
+ mir_sha1_append(&ctx, key, (int)keylen);
+ mir_sha1_finish(&ctx, mdkey);
+ keylen = 20;
+ key = mdkey;
+ }
+
+ memcpy(k_ipad, key, keylen);
+ memcpy(k_opad, key, keylen);
+ memset(k_ipad+keylen, 0x36, SHA_BLOCKSIZE - keylen);
+ memset(k_opad+keylen, 0x5c, SHA_BLOCKSIZE - keylen);
+
+ for (unsigned i = 0; i < keylen; i++) {
+ k_ipad[i] ^= 0x36;
+ k_opad[i] ^= 0x5c;
+ }
+
+ mir_sha1_init(&ctx);
+ mir_sha1_append(&ctx, k_ipad, SHA_BLOCKSIZE);
+ mir_sha1_append(&ctx, text, (int)textlen);
+ mir_sha1_finish(&ctx, hashout);
+
+ mir_sha1_init(&ctx);
+ mir_sha1_append(&ctx, k_opad, SHA_BLOCKSIZE);
+ mir_sha1_append(&ctx, hashout, MIR_SHA1_HASH_SIZE);
+ mir_sha1_finish(&ctx, hashout);
+}
|