⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 md.cpp

📁 RSA C++源代码DEMO,附加DESMD5等众多算法
💻 CPP
📖 第 1 页 / 共 2 页
字号:
    /* Update number of bits
     */
    if ((context->count[0] += ((UINT4)inputLen << 3))
      < ((UINT4)inputLen << 3)) {
        ++(context->count[1]);
    }
    context->count[1] += ((UINT4)inputLen >> 29);

    partLen = 64 - index;
    /* Transform as many times as possible.
     */
    if (inputLen >= partLen) {
        T_memcpy (&context->buffer[index], input, partLen);
        Md4_Transform (context->state, context->buffer);
        for (i = partLen; i + 63 < inputLen; i += 64) {
            Md4_Transform (context->state, input+i);
        }
        index = 0;
    }
    else {
        i = 0;
    }

    /* Buffer remaining input
     */
    T_memcpy (&context->buffer[index], input+i, inputLen-i);
}

/* MD4 finalization. Ends an MD4 message-digest operation, writing the
    the message digest and zeroizing the context.
 */
void MD4_Final (BYTE digest[16], MD4CTX *context)
{
    BYTE                bits[8];
    UINT                index, padLen;

    /* Save number of bits
     */
    EncodeLH (bits, context->count, 8);
    /* Pad out to 56 mod 64.
     */
    index = (UINT)((context->count[0] >> 3) & 0x3f);
    padLen = (index < 56) ? (56 - index) : (120 - index);
    MD4_Update (context, MD4_PADDING, padLen);

    /* Append length (before padding)
     */
    MD4_Update (context, bits, 8);
    /* Store state in digest
     */
    EncodeLH (digest, context->state, 16);

    /* Zeroize sensitive information.
     */
    T_memset (context, 0, sizeof (*context));
}


/* MD5 ---------------------------------------------------------------
 */
/* Constants for Md5_Transform routine.
 */
#define MD5_S11     7
#define MD5_S12     12
#define MD5_S13     17
#define MD5_S14     22
#define MD5_S21     5
#define MD5_S22     9
#define MD5_S23     14
#define MD5_S24     20
#define MD5_S31     4
#define MD5_S32     11
#define MD5_S33     16
#define MD5_S34     23
#define MD5_S41     6
#define MD5_S42     10
#define MD5_S43     15
#define MD5_S44     21

#define MD5_PADDING MD4_PADDING

/* MD5_F, MD5_G, MD5_H and MD5_I are basic MD5 functions.
 */
#define MD5_F(x, y, z)      (((x)&(y))|((~x)&(z)))
#define MD5_G(x, y, z)      (((x)&(z))|((y)&(~z)))
#define MD5_H(x, y, z)      ((x)^(y)^(z))
#define MD5_I(x, y, z)      ((y)^((x)|(~z)))
/* MD5_FF, MD5_GG, MD5_HH, and MD5_II transformations for rounds 1, 2, 3,
    and 4. Rotation is separate from addition to prevent recomputation.
 */
#define MD5_FF(a, b, c, d, x, s, ac) { \
    (a) += MD5_F ((b), (c), (d))+(x)+(UINT4)(ac); \
    (a) = ROTL32 ((a), (s)); \
    (a) += (b); \
}
#define MD5_GG(a, b, c, d, x, s, ac) { \
    (a) += MD5_G ((b), (c), (d))+(x)+(UINT4)(ac); \
    (a) = ROTL32 ((a), (s)); \
    (a) += (b); \
}
#define MD5_HH(a, b, c, d, x, s, ac) { \
    (a) += MD5_H ((b), (c), (d))+(x)+(UINT4)(ac); \
    (a) = ROTL32 ((a), (s)); \
    (a) += (b); \
}
#define MD5_II(a, b, c, d, x, s, ac) { \
    (a) += MD5_I ((b), (c), (d))+(x)+(UINT4)(ac); \
    (a) = ROTL32 ((a), (s)); \
    (a) += (b); \
}


static void Md5_Transform PROTO_LIST ((UINT4 [4], const BYTE [64]));
/* MD5 basic transformation. Transforms state based on block.
 */
static void Md5_Transform (UINT4 state[4], const BYTE block[64])
{
    UINT4               a, b, c, d, x[16];

    a = state[0], b = state[1], c = state[2], d = state[3];
    DecodeLH (x, block, 64);

    /* Round 1 */
    MD5_FF (a, b, c, d, x[ 0], MD5_S11, 0xd76aa478);    /* 1 */
    MD5_FF (d, a, b, c, x[ 1], MD5_S12, 0xe8c7b756);    /* 2 */
    MD5_FF (c, d, a, b, x[ 2], MD5_S13, 0x242070db);    /* 3 */
    MD5_FF (b, c, d, a, x[ 3], MD5_S14, 0xc1bdceee);    /* 4 */
    MD5_FF (a, b, c, d, x[ 4], MD5_S11, 0xf57c0faf);    /* 5 */
    MD5_FF (d, a, b, c, x[ 5], MD5_S12, 0x4787c62a);    /* 6 */
    MD5_FF (c, d, a, b, x[ 6], MD5_S13, 0xa8304613);    /* 7 */
    MD5_FF (b, c, d, a, x[ 7], MD5_S14, 0xfd469501);    /* 8 */
    MD5_FF (a, b, c, d, x[ 8], MD5_S11, 0x698098d8);    /* 9 */
    MD5_FF (d, a, b, c, x[ 9], MD5_S12, 0x8b44f7af);    /* 10 */
    MD5_FF (c, d, a, b, x[10], MD5_S13, 0xffff5bb1);    /* 11 */
    MD5_FF (b, c, d, a, x[11], MD5_S14, 0x895cd7be);    /* 12 */
    MD5_FF (a, b, c, d, x[12], MD5_S11, 0x6b901122);    /* 13 */
    MD5_FF (d, a, b, c, x[13], MD5_S12, 0xfd987193);    /* 14 */
    MD5_FF (c, d, a, b, x[14], MD5_S13, 0xa679438e);    /* 15 */
    MD5_FF (b, c, d, a, x[15], MD5_S14, 0x49b40821);    /* 16 */

    /* Round 2 */
    MD5_GG (a, b, c, d, x[ 1], MD5_S21, 0xf61e2562);    /* 17 */
    MD5_GG (d, a, b, c, x[ 6], MD5_S22, 0xc040b340);    /* 18 */
    MD5_GG (c, d, a, b, x[11], MD5_S23, 0x265e5a51);    /* 19 */
    MD5_GG (b, c, d, a, x[ 0], MD5_S24, 0xe9b6c7aa);    /* 20 */
    MD5_GG (a, b, c, d, x[ 5], MD5_S21, 0xd62f105d);    /* 21 */
    MD5_GG (d, a, b, c, x[10], MD5_S22,  0x2441453);    /* 22 */
    MD5_GG (c, d, a, b, x[15], MD5_S23, 0xd8a1e681);    /* 23 */
    MD5_GG (b, c, d, a, x[ 4], MD5_S24, 0xe7d3fbc8);    /* 24 */
    MD5_GG (a, b, c, d, x[ 9], MD5_S21, 0x21e1cde6);    /* 25 */
    MD5_GG (d, a, b, c, x[14], MD5_S22, 0xc33707d6);    /* 26 */
    MD5_GG (c, d, a, b, x[ 3], MD5_S23, 0xf4d50d87);    /* 27 */
    MD5_GG (b, c, d, a, x[ 8], MD5_S24, 0x455a14ed);    /* 28 */
    MD5_GG (a, b, c, d, x[13], MD5_S21, 0xa9e3e905);    /* 29 */
    MD5_GG (d, a, b, c, x[ 2], MD5_S22, 0xfcefa3f8);    /* 30 */
    MD5_GG (c, d, a, b, x[ 7], MD5_S23, 0x676f02d9);    /* 31 */
    MD5_GG (b, c, d, a, x[12], MD5_S24, 0x8d2a4c8a);    /* 32 */

    /* Round 3 */
    MD5_HH (a, b, c, d, x[ 5], MD5_S31, 0xfffa3942);    /* 33 */
    MD5_HH (d, a, b, c, x[ 8], MD5_S32, 0x8771f681);    /* 34 */
    MD5_HH (c, d, a, b, x[11], MD5_S33, 0x6d9d6122);    /* 35 */
    MD5_HH (b, c, d, a, x[14], MD5_S34, 0xfde5380c);    /* 36 */
    MD5_HH (a, b, c, d, x[ 1], MD5_S31, 0xa4beea44);    /* 37 */
    MD5_HH (d, a, b, c, x[ 4], MD5_S32, 0x4bdecfa9);    /* 38 */
    MD5_HH (c, d, a, b, x[ 7], MD5_S33, 0xf6bb4b60);    /* 39 */
    MD5_HH (b, c, d, a, x[10], MD5_S34, 0xbebfbc70);    /* 40 */
    MD5_HH (a, b, c, d, x[13], MD5_S31, 0x289b7ec6);    /* 41 */
    MD5_HH (d, a, b, c, x[ 0], MD5_S32, 0xeaa127fa);    /* 42 */
    MD5_HH (c, d, a, b, x[ 3], MD5_S33, 0xd4ef3085);    /* 43 */
    MD5_HH (b, c, d, a, x[ 6], MD5_S34,  0x4881d05);    /* 44 */
    MD5_HH (a, b, c, d, x[ 9], MD5_S31, 0xd9d4d039);    /* 45 */
    MD5_HH (d, a, b, c, x[12], MD5_S32, 0xe6db99e5);    /* 46 */
    MD5_HH (c, d, a, b, x[15], MD5_S33, 0x1fa27cf8);    /* 47 */
    MD5_HH (b, c, d, a, x[ 2], MD5_S34, 0xc4ac5665);    /* 48 */

    /* Round 4 */
    MD5_II (a, b, c, d, x[ 0], MD5_S41, 0xf4292244);    /* 49 */
    MD5_II (d, a, b, c, x[ 7], MD5_S42, 0x432aff97);    /* 50 */
    MD5_II (c, d, a, b, x[14], MD5_S43, 0xab9423a7);    /* 51 */
    MD5_II (b, c, d, a, x[ 5], MD5_S44, 0xfc93a039);    /* 52 */
    MD5_II (a, b, c, d, x[12], MD5_S41, 0x655b59c3);    /* 53 */
    MD5_II (d, a, b, c, x[ 3], MD5_S42, 0x8f0ccc92);    /* 54 */
    MD5_II (c, d, a, b, x[10], MD5_S43, 0xffeff47d);    /* 55 */
    MD5_II (b, c, d, a, x[ 1], MD5_S44, 0x85845dd1);    /* 56 */
    MD5_II (a, b, c, d, x[ 8], MD5_S41, 0x6fa87e4f);    /* 57 */
    MD5_II (d, a, b, c, x[15], MD5_S42, 0xfe2ce6e0);    /* 58 */
    MD5_II (c, d, a, b, x[ 6], MD5_S43, 0xa3014314);    /* 59 */
    MD5_II (b, c, d, a, x[13], MD5_S44, 0x4e0811a1);    /* 60 */
    MD5_II (a, b, c, d, x[ 4], MD5_S41, 0xf7537e82);    /* 61 */
    MD5_II (d, a, b, c, x[11], MD5_S42, 0xbd3af235);    /* 62 */
    MD5_II (c, d, a, b, x[ 2], MD5_S43, 0x2ad7d2bb);    /* 63 */
    MD5_II (b, c, d, a, x[ 9], MD5_S44, 0xeb86d391);    /* 64 */

    state[0] += a;
    state[1] += b;
    state[2] += c;
    state[3] += d;

    /* Zeroize sensitive information.
     */
    T_memset (x, 0, sizeof (x));
}

/* MD5 initialization. Begins an MD5 operation, writing a new context.
 */
void MD5_Init (MD5CTX *context)
{
    context->count[0] = context->count[1] = 0;
    /* Load magic initialization constants.
     */
    context->state[0] = 0x67452301;
    context->state[1] = 0xefcdab89;
    context->state[2] = 0x98badcfe;
    context->state[3] = 0x10325476;
}

/* MD5 block update operation. Continues an MD5 message-digest
    operation, processing another message block, and updating the
    context.
 */
void MD5_Update (MD5CTX *context, const BYTE *input, UINT inputLen)
{
    UINT                i, index, partLen;

    /* Compute number of bytes mod 64
     */
    index = (UINT)((context->count[0] >> 3)&0x3F);
    /* Update number of bits
     */
    if ((context->count[0] += ((UINT4)inputLen << 3))
     < ((UINT4)inputLen << 3)) {
        (++context->count[1]);
    }
    context->count[1] += ((UINT4)inputLen >> 29);

    partLen = 64-index;

    /* Transform as many times as possible.
     */
    if (inputLen >= partLen) {
        T_memcpy (&context->buffer[index], input, partLen);
        Md5_Transform (context->state, context->buffer);
        for (i = partLen; i + 63 < inputLen; i += 64) {
            Md5_Transform (context->state, input+i);
        }
        index = 0;
    }
    else {
        i = 0;
    }

    /* Buffer remaining input */
    T_memcpy (&context->buffer[index], input+i, inputLen-i);
}

/* MD5 finalization. Ends an MD5 message-digest operation, writing the
    the message digest and zeroizing the context.
 */
void MD5_Final (BYTE digest[16], MD5CTX *context)
{
    BYTE                bits[8];
    UINT                index, padLen;

    /* Save number of bits
     */
    EncodeLH (bits, context->count, 8);

    /* Pad out to 56 mod 64.
     */
    index = (UINT)((context->count[0] >> 3) & 0x3f);
    padLen = (index < 56) ? (56-index) : (120-index);
    MD5_Update (context, MD5_PADDING, padLen);

    /* Append length (before padding)
     */
    MD5_Update (context, bits, 8);

    /* Store state in digest
     */
    EncodeLH (digest, context->state, 16);

    /* Zeroize sensitive information.
     */
    T_memset (context, 0, sizeof (*context));
}


/* SHA-1 -------------------------------------------------------------
 */
/* Constants for Sha1_Transform routine.
 */
#define SHA1_K0     0x5a827999
#define SHA1_K1     0x6ed9eba1
#define SHA1_K2     0x8f1bbcdc
#define SHA1_K3     0xca62c1d6

#define SHA1_PADDING    MD4_PADDING

/* Functions used in SHA-1
 */
#define SHA1_F00(b, c, d)       (((b)&(c))|(~(b)&(d)))
#define SHA1_F20(b, c, d)       ((b)^(c)^(d))
#define SHA1_F40(b, c, d)       (((b)&(c))|((c)&(d))|((d)&(b)))
#define SHA1_F60(b, c, d)       ((b)^(c)^(d))


static void Sha1_Transform PROTO_LIST ((UINT4 [5], const BYTE [64]));
/* SHA-1 basic transformation. Transforms state based on block.
 */
static void Sha1_Transform (UINT4 state[5], const BYTE block[64])
{
    UINT4               a, b, c, d, e, temp, w[80];
    UINT                t;

    DecodeHL (w, block, 64);
    for (t = 16; t < 80; ++t) {
        temp = w[t-3]^w[t-8]^w[t-14]^w[t-16];
        w[t] = ROTL32 (temp, 1);
    }
    a = state[0], b = state[1], c = state[2], d = state[3], e = state[4];
    for (t = 0; t < 20; ++t) {
        temp = ROTL32 (a, 5)+SHA1_F00 (b, c, d)+e+w[t]+SHA1_K0;
        e = d; d = c; c = ROTL32 (b, 30); b = a; a = temp;
    }
    for (t = 20; t < 40; ++t) {
        temp = ROTL32 (a, 5)+SHA1_F20 (b, c, d)+e+w[t]+SHA1_K1;
        e = d; d = c; c = ROTL32 (b, 30); b = a; a = temp;
    }
    for (t = 40; t < 60; ++t) {
        temp = ROTL32 (a, 5)+SHA1_F40 (b, c, d)+e+w[t]+SHA1_K2;
        e = d; d = c; c = ROTL32 (b, 30); b = a; a = temp;
    }
    for (t = 60; t < 80; ++t) {
        temp = ROTL32 (a, 5)+SHA1_F60 (b, c, d)+e+w[t]+SHA1_K3;
        e = d; d = c; c = ROTL32 (b, 30); b = a; a = temp;
    }
    state[0] += a;
    state[1] += b;
    state[2] += c;
    state[3] += d;
    state[4] += e;
    T_memset (w, 0, sizeof (w));
}

/* SHA-1 initialization. Begins an MD5 operation, writing a new context.
 */
void SHA1_Init (SHA1CTX *context)
{
    context->count[0] = context->count[1] = 0;
    /* Load magic initialization constants.
     */
    context->state[0] = 0x67452301;
    context->state[1] = 0xefcdab89;
    context->state[2] = 0x98badcfe;
    context->state[3] = 0x10325476;
    context->state[4] = 0xc3d2e1f0;
}

/* SHA-1 block update operation. Continues an MD5 message-digest
    operation, processing another message block, and updating the
    context.
 */
void SHA1_Update (SHA1CTX *context, const BYTE *input, UINT inputLen)
{
    UINT                i, index, partLen;

    /* Compute number of bytes mod 64
     */
    index = (UINT)((context->count[1] >> 3)&0x3F);
    /* Update number of bits
     */
    if ((context->count[1] += ((UINT4)inputLen << 3))
     < ((UINT4)inputLen << 3)) {
        (++context->count[0]);
    }
    context->count[0] += ((UINT4)inputLen >> 29);

    partLen = 64-index;

    /* Transform as many times as possible.
     */
    if (inputLen >= partLen) {
        T_memcpy (&context->buffer[index], input, partLen);
        Sha1_Transform (context->state, context->buffer);
        for (i = partLen; i + 63 < inputLen; i += 64) {
            Sha1_Transform (context->state, input+i);
        }
        index = 0;
    }
    else {
        i = 0;
    }

    /* Buffer remaining input */
    T_memcpy (&context->buffer[index], input+i, inputLen-i);
}

/* SHA-1 finalization. Ends an MD5 message-digest operation, writing the
    the message digest and zeroizing the context.
 */
void SHA1_Final (BYTE digest[20], SHA1CTX *context)
{
    BYTE                bits[8];
    UINT                index, padLen;

    /* Save number of bits
     */
    EncodeHL (bits, context->count, 8);

    /* Pad out to 56 mod 64.
     */
    index = (UINT)((context->count[1] >> 3) & 0x3f);
    padLen = (index < 56) ? (56-index) : (120-index);
    SHA1_Update (context, SHA1_PADDING, padLen);

    /* Append length (before padding)
     */
    SHA1_Update (context, bits, 8);

    /* Store state in digest
     */
    EncodeHL (digest, context->state, 20);

    /* Zeroize sensitive information.
     */
    T_memset (context, 0, sizeof (*context));
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -