繁体   English   中英

更新MD5哈希?

[英]Updating an MD5 Hash?

我目前有一个非常基本的MD5哈希算法。 (我只能叫atm为MD5(const char *)),但是它将限于小文件。 即32位系统最多只能给我4GB或更少的文件空间。 另外,为什么在世界上,我想将接近1GB的任何内容加载到内存中。 ;)这使我想到了这个问题...

如何散列大文件? 我注意到将文件的一部分加载到内存中时,OpenSSL(将来会在将来使用)使用MD5哈希更新功能。 那么,“更新” MD5哈希时究竟发生了什么? 互联网上是否有伪代码可以在任何地方执行此操作或提供示例?

PS我是加密货币世界的新手。 因此,请问我对任何人的回答有任何后续问题,请原谅我。 喜欢在尝试简单方法之前先尝试困难的方法。 最好的学习方式! ;)

我的MD5标头

    #ifndef MD5_H
    #define MD5_H

    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    #include <math.h>
    #include <ctype.h>

    #define MAX_MD5_HASH_LENGTH 32


    typedef union uwb {
        unsigned w;
        unsigned char b[4];
    } WBunion;

    typedef unsigned Digest[4];

    unsigned f0( unsigned abcd[] );

    unsigned f1( unsigned abcd[] );

    unsigned f2( unsigned abcd[] );

    unsigned f3( unsigned abcd[] );

    typedef unsigned (*DgstFctn)(unsigned a[]);

    unsigned *calcKs( unsigned *k);
    unsigned rol( unsigned v, short amt );
    unsigned *md5( const char *msg, int mlen);

    char* convertRawMd5HashToString(unsigned* rawMd5);
    int isValidMd5(const char* md5String);

    #endif

源文件

#include "md5.h"

unsigned *calcKs( unsigned *k)
{
    double s, pwr;
    int i;

    pwr = pow( 2, MAX_MD5_HASH_LENGTH);
    for (i=0; i<64; i++) {
        s = fabs(sin(1+i));
        k[i] = (unsigned)( s * pwr );
    }
    return k;
}

// ROtate v Left by amt bits
unsigned rol( unsigned v, short amt )
{
    unsigned  msk1 = (1<<amt) -1;
    return ((v>>(MAX_MD5_HASH_LENGTH-amt)) & msk1) | ((v<<amt) & ~msk1);
}

unsigned *md5( const char *message, int messageLength) 
{
    static const Digest h0 = { 0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476 };
    static const DgstFctn ff[] = { &f0, &f1, &f2, &f3 };
    static const short M[] = { 1, 5, 3, 7 };
    static const short O[] = { 0, 1, 5, 0 };
    static const short rot0[] = { 7,12,17,22};
    static const short rot1[] = { 5, 9,14,20};
    static const short rot2[] = { 4,11,16,23};
    static const short rot3[] = { 6,10,15,21};
    static const short *rots[] = {rot0, rot1, rot2, rot3 };
    static unsigned kspace[64];
    static unsigned *k;

    static Digest h;
    Digest abcd;
    DgstFctn fctn;
    short m, o, g;
    unsigned f;
    short *rotn;
    union {
        unsigned w[16];
        char     b[64];
    }mm;
    int os = 0;
    int grp, grps, q, p;
    unsigned char *msg2;

    if (k==NULL) k= calcKs(kspace);

    for (q=0; q<4; q++) h[q] = h0[q];   // initialize
    {
        grps  = 1 + (messageLength+8)/64;
        msg2 = malloc( 64*grps);
        memcpy( msg2, message, messageLength);
        msg2[messageLength] = (unsigned char)0x80;  
        q = messageLength + 1;
        while (q < 64*grps){ msg2[q] = 0; q++ ; }
        {
            WBunion u;
            u.w = 8*messageLength;
            q -= 8;
            memcpy(msg2+q, &u.w, 4 );
        }
    }

    for (grp=0; grp<grps; grp++)
    {
        memcpy( mm.b, msg2+os, 64);
        for(q=0;q<4;q++) abcd[q] = h[q];
        for (p = 0; p<4; p++) {
            fctn = ff[p];
            rotn = rots[p];
            m = M[p]; o= O[p];
            for (q=0; q<16; q++) {
                g = (m*q + o) % 16;
                f = abcd[1] + rol( abcd[0]+ fctn(abcd) + k[q+16*p] + mm.w[g], rotn[q%4]);

                abcd[0] = abcd[3];
                abcd[3] = abcd[2];
                abcd[2] = abcd[1];
                abcd[1] = f;
            }
        }
        for (p=0; p<4; p++)
            h[p] += abcd[p];
        os += 64;
    }

    if( msg2 )
        free( msg2 );

    return h;
}

char* convertRawMd5HashToString(unsigned* rawMd5)
{
    static char* outputBuffer[MAX_MD5_HASH_LENGTH];
    memset(outputBuffer, 0, MAX_MD5_HASH_LENGTH);

    int j, k;
    WBunion u;
    for (j=0;j<4; j++){
        u.w = rawMd5[j];
        for (k=0;k<4;k++) sprintf(outputBuffer, "%s%02x", outputBuffer, u.b[k]);
    }

    return outputBuffer;
}

unsigned f0( unsigned abcd[] ){
    return ( abcd[1] & abcd[2]) | (~abcd[1] & abcd[3]); }

unsigned f1( unsigned abcd[] ){
    return ( abcd[3] & abcd[1]) | (~abcd[3] & abcd[2]);}

unsigned f2( unsigned abcd[] ){
    return  abcd[1] ^ abcd[2] ^ abcd[3];}

unsigned f3( unsigned abcd[] ){
    return abcd[2] ^ (abcd[1] |~ abcd[3]);}

int isValidMd5(const char* md5String)
{
    if(strlen(md5String) != MAX_MD5_HASH_LENGTH)
        return 0;

    for (int i = 0; i < MAX_MD5_HASH_LENGTH; ++i) {
        char c = tolower(md5String[i]);
        if((c >= 'a' && c <= 'f') || isdigit(c)) {
            continue;
        } else {
            return 0;
        }
    }

    return 1;
}

我找不到原始作者,但是如果有人知道谁是本摘要的大部分作者,请告诉我。 谢谢。 :)

填充发生在第一个for循环中,您需要将其推迟到数据结束。 然后,您可以在第二个for循环中运行尽可能多的数据,直到结束,然后添加填充。 这也将允许更改代码,以便在不需要制作数据时进行复制。 将其拆分为init,更新和完成函数。 那应该不难编写代码。

当然,一个更好的主意是使用一个已将功能分为初始化,更新和完成的版本。 有趣的是,Apple Common Crypto是开源的,用“ C”编写,请看一下。 MD代码该代码位于CommonDigestPriv.h

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM