分享:用四十种语言分别写一个MD5算法 之1 C语言MD5算法

来源:互联网 发布:mt4编程 编辑:程序博客网 时间:2024/06/06 20:07
分享:用四十种语言分别写一个MD5算法 之1 C语言MD5算法

Library: OpenSSL

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/md5.h>
 
const char *string = "hello csdner";
 
int main()
{
  int i;
  unsigned char result[MD5_DIGEST_LENGTH];
 
  MD5(string, strlen(string), result);
 
  // output
  for(i = 0; i < MD5_DIGEST_LENGTH; i++)
    printf("%02x", result[i]);
  printf("\n");
 
  return EXIT_SUCCESS;
}

Implementation of md5

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
 
typedef union uwb {
    unsigned w;
    unsigned char b[4];
} WBunion;
 
typedef unsigned Digest[4];
 
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]);}
 
typedef unsigned (*DgstFctn)(unsigned a[]);
 
unsigned *calcKs( unsigned *k)
{
    double s, pwr;
    int i;
 
    pwr = pow( 2, 32);
    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>>(32-amt)) & msk1) | ((v<<amt) & ~msk1);
}
 
unsigned *md5( const char *msg, int mlen)
{
    static Digest h0 = { 0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476 };
//    static Digest h0 = { 0x01234567, 0x89ABCDEF, 0xFEDCBA98, 0x76543210 };
    static DgstFctn ff[] = { &f0, &f1, &f2, &f3 };
    static short M[] = { 1, 5, 3, 7 };
    static short O[] = { 0, 1, 5, 0 };
    static short rot0[] = { 7,12,17,22};
    static short rot1[] = { 5, 9,14,20};
    static short rot2[] = { 4,11,16,23};
    static short rot3[] = { 6,10,15,21};
    static 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 + (mlen+8)/64;
        msg2 = malloc( 64*grps);
        memcpy( msg2, msg, mlen);
        msg2[mlen] = (unsigned char)0x80;  
        q = mlen + 1;
        while (q < 64*grps){ msg2[q] = 0; q++ ; }
        {
//            unsigned char t;
            WBunion u;
            u.w = 8*mlen;
//            t = u.b[0]; u.b[0] = u.b[3]; u.b[3] = t;
//            t = u.b[1]; u.b[1] = u.b[2]; u.b[2] = t;
            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;
    }
    return h;
}    
 
int main( int argc, char *argv[] )
{
    int j,k;
    const char *msg = "hello csdner";
    unsigned *d = md5(msg, strlen(msg));
    WBunion u;
 
    printf("= 0x");
    for (j=0;j<4; j++){
        u.w = d[j];
        for (k=0;k<4;k++) printf("%02x",u.b[k]);
    }
    printf("\n");
 
    return 0;
}

版权所有:百万商业圈
未经许可不得转载,有任何疑问请与我本人联系 QQ 99923309 Mail:bwsyq@bwsyq.com 更多详情>>

开源:完全自主研发搜索引擎1.0源代码及说明,单机400万网页,任意50词以内的检索不超过 20毫秒

开源:基于百万商业圈.NET开发框架开发的并行带分词的采集器

百万商业圈 .NET 开发框架2.0及开发框架API说明书(BWFW)(含并行计算及中英文分词功能)

分享一点代码(小型C web开发框架),用C语言实现的一个WEB 文件上传(含全部源代码)一

天心天字辈ERP全部PDK源代码到了我手上的后果 - 超越天心之WEB天云

大家看看我的BS的甘特图排程做的如何? 无刷新Ajax甘特图 展示生产排程结果 演示

软件工程概述 - 企业架构 - IT企业做大做强之根本 - 之我见

实践检验得出的真理:asp.net 项目在 linux mono上编译需要修改的只有 3个地方

给大家漏一手本人亲自精心撰写的通用ajax框架,完全兼容 IE FireFox各个版本!(附完整源码及完整范例)

开发了一个中文分词服务器(C语言开发+词库+源代码),最大特色可以让javascript来调用!

用纯C语言写了一个HtmlParse(网页分析器)外带采集功能,大小只有200K(免费+开源+操作示意图)

分享:用九种语言分别写一个web server 之 C语言web server

分享:用九种语言分别写一个web server 之 Delphi语言web server

分享:用九种语言分别写一个web server 之 Fantom语言web server

分享:用九种语言分别写一个web server 之 Perl语言web server

分享:用九种语言分别写一个web server 之 Go语言web server

分享:用九种语言分别写一个web server 之 PicoLisp语言web server

分享:用九种语言分别写一个web server 之 PureBasic语言web server

分享:用九种语言分别写一个web server 之 Python语言web server

分享:用九种语言分别写一个web server 之 Tcl语言web server
原创粉丝点击