summaryrefslogtreecommitdiff
path: root/src/mir_core/sha1.cpp
diff options
context:
space:
mode:
authorGeorge Hazan <george.hazan@gmail.com>2013-08-15 20:00:29 +0000
committerGeorge Hazan <george.hazan@gmail.com>2013-08-15 20:00:29 +0000
commit623722f7cc4c20d2b7d8df03035801acacda6018 (patch)
treed4c3686f935c12c9e9274cc3706bef12d9a20d86 /src/mir_core/sha1.cpp
parent8588345b7b8ff89bf90a216dbed2302fcf3ccdbc (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/sha1.cpp')
-rw-r--r--src/mir_core/sha1.cpp44
1 files changed, 41 insertions, 3 deletions
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);
+}