md5sum.c

#include <stdio.h>
#include <stdlib.h>
#include "md5.h"

#pragma warning(disable:4996)

#define BUFFER_SIZE        0x200000

void print_digest(const unsigned char* digest);
void print_digest(const unsigned char* digest) {
    int    i;

    ; i < ; i++ ) {
        printf("%02x", *digest++);
    }
}

int main(int argc, char* argv[]) {
    struct MD5Context    md5;
    unsigned char*        buffer;
    unsigned ];
    size_t                len;
    FILE*                file;
    int                    i;

    // check the arguments.
     ) {
        printf("use this program as:\n md5sum file1 file2 file3...\n");
        );
    }
    // Get the memory as the buffer.
    buffer = (unsigned char*)malloc(BUFFER_SIZE);
    if ( buffer == NULL ) {
        printf("Have not enought memory to do this work!\n");
        );
    }
    // One by one.
    ; i < argc; i++ ) {
        MD5Init(&md5);
        // open the file.
        file = fopen(argv[i], "rb");
        if ( file == NULL ) {
            printf("Can't open file: %s\n", argv[i]);
            continue;
        }
        // read the data from the file.
        , BUFFER_SIZE, file)) >  ) {
            MD5Update(&md5, (const unsigned char*)buffer, (unsigned int)len);
        }
        // Finish and show it.
        MD5Final(digest, &md5);
        fclose(file);
        printf("%s: ", argv[i]);
        print_digest((const unsigned char*)digest);
        printf("\n");
    }
    // Free the memory.
    free((void*)buffer);

    );
}

md5.c

/*
 * This code implements the MD5 message-digest algorithm.
 * The algorithm is due to Ron Rivest.  This code was
 * written by Colin Plumb in 1993, no copyright is claimed.
 * This code is in the public domain; do with it what you wish.
 *
 * Equivalent code is available from RSA Data Security, Inc.
 * This code has been tested against that, and is equivalent,
 * except that you don't need to include two pages of legalese
 * with every copy.
 *
 * To compute the message digest of a chunk of bytes, declare an
 * MD5Context structure, pass it to MD5Init, call MD5Update as
 * needed on buffers full of bytes, and then call MD5Final, which
 * will fill a supplied 16-byte array with the digest.
 */
#include <string.h>        /* for memcpy() */
#include "md5.h"
#ifndef HIGHFIRST
#define byteReverse(buf, len)    /* Nothing */
#else
void byteReverse(unsigned char *buf, unsigned longs);

#ifndef ASM_MD5
/*
 * Note: this code is harmless on little-endian machines.
 */
void byteReverse(unsigned char *buf, unsigned longs)
{
    unsigned long t;
    do {
    t = (unsigned ] <<  | buf[]) <<  |
        ((unsigned) buf[] <<  | buf[]);
    *(unsigned long *) buf = t;
    buf += ;
    } while (--longs);
}
#endif
#endif

/*
 * Start MD5 accumulation.  Set bit count to 0 and buffer to mysterious
 * initialization constants.
 */
void MD5Init(struct MD5Context *ctx)
{
    ctx->buf[] = 0x67452301;
    ctx->buf[] = 0xefcdab89;
    ctx->buf[] = 0x98badcfe;
    ctx->buf[] = 0x10325476;

    ctx->bits[] = ;
    ctx->bits[] = ;
}

/*
 * Update context to reflect the concatenation of another buffer full
 * of bytes.
 */
void MD5Update(struct MD5Context *ctx, unsigned char const *buf, unsigned len)
{
  unsigned long t;

    //Update bitcount 

    t = ctx->bits[];
    ] = t + ((unsigned )) < t)
    ctx->bits[]++;        // Carry from low to high
   ctx->bits[] += len >> ;

    t = (t >> ) & 0x3f;    // Bytes already in shsInfo->data 

    //Handle any leading odd-sized chunks

    if (t) {
unsigned char *p = (unsigned char *) ctx->in + t;

    t =  - t;
    if (len < t) {
        memcpy(p, buf, len);
        return;
    }
    memcpy(p, buf, t);
    byteReverse(ctx->);
    MD5Transform(ctx->buf, (unsigned long *) ctx->in);
    buf += t;
    len -= t;
    }
    // Process data in 64-byte chunks 

    ) {
    memcpy(ctx->);
    byteReverse(ctx->);
    MD5Transform(ctx->buf, (unsigned long *) ctx->in);
    buf += ;
    len -= ;
    }

    // Handle any remaining bytes of data. 

    memcpy(ctx->in, buf, len);
}

/*
 * Final wrapup - pad to 64-byte boundary with the bit pattern
 * 1 0* (64-bit count of bits processed, MSB-first)
 */
], struct MD5Context *ctx)
{
    unsigned count;
    unsigned char *p;

    // Compute number of bytes mod 64
    count = (ctx->bits[] >> ) & 0x3F;

    //Set the first char of padding to 0x80.  This is safe since there is
      // always at least one byte free
    p = ctx->in + count;
    *p++ = 0x80;

    // Bytes of padding needed to make 64 bytes
    count =  -  - count;

    // Pad out to 56 mod 64
    ) {
    // Two lots of padding:  Pad the first block to 64 bytes
    memset(p, , count);
    byteReverse(ctx->);
    MD5Transform(ctx->buf, (unsigned long *) ctx->in);

    // Now fill the next block with 56 bytes
    memset(ctx->, );
    } else {
    // Pad block to 56 bytes
    memset(p, , count - );
    }
    byteReverse(ctx->);

    //Append length in bits and transform
    ((unsigned ] = ctx->bits[];
    ((unsigned ] = ctx->bits[];

    MD5Transform(ctx->buf, (unsigned long *) ctx->in);
    byteReverse((unsigned );
    memcpy(digest, ctx->buf, );
    memset(ctx, , sizeof(ctx));    // In case it's sensitive
}

#ifndef ASM_MD5

/* The four core functions - F1 is optimized somewhat */

/* #define F1(x, y, z) (x & y | ~x & z) */
#define F1(x, y, z) (z ^ (x & (y ^ z)))
#define F2(x, y, z) F1(z, x, y)
#define F3(x, y, z) (x ^ y ^ z)
#define F4(x, y, z) (y ^ (x | ~z))

/* This is the central step in the MD5 algorithm. */
#define MD5STEP(f, w, x, y, z, data, s) \
    ( w += f(x, y, z) + data,  w = w<<s | w>>(-s),  w += x )

/*
 * The core of the MD5 algorithm, this alters an existing MD5 hash to
 * reflect the addition of 16 longwords of new data.  MD5Update blocks
 * the data and converts bytes into longwords for this routine.
 */
], unsigned ])
{
    register unsigned long a, b, c, d;

    a = buf[];
    b = buf[];
    c = buf[];
    d = buf[];

    MD5STEP(F1, a, b, c, d, ] + );
    MD5STEP(F1, d, a, b, c, ] + );
    MD5STEP(F1, c, d, a, b, ] + );
    MD5STEP(F1, b, c, d, a, ] + );
    MD5STEP(F1, a, b, c, d, ] + );
    MD5STEP(F1, d, a, b, c, ] + );
    MD5STEP(F1, c, d, a, b, ] + );
    MD5STEP(F1, b, c, d, a, ] + );
    MD5STEP(F1, a, b, c, d, ] + );
    MD5STEP(F1, d, a, b, c, ] + );
    MD5STEP(F1, c, d, a, b, ] + );
    MD5STEP(F1, b, c, d, a, ] + );
    MD5STEP(F1, a, b, c, d, ] + );
    MD5STEP(F1, d, a, b, c, ] + );
    MD5STEP(F1, c, d, a, b, ] + );
    MD5STEP(F1, b, c, d, a, ] + );

    MD5STEP(F2, a, b, c, d, ] + );
    MD5STEP(F2, d, a, b, c, ] + );
    MD5STEP(F2, c, d, a, b, ] + );
    MD5STEP(F2, b, c, d, a, ] + );
    MD5STEP(F2, a, b, c, d, ] + );
    MD5STEP(F2, d, a, b, c, ] + );
    MD5STEP(F2, c, d, a, b, ] + );
    MD5STEP(F2, b, c, d, a, ] + );
    MD5STEP(F2, a, b, c, d, ] + );
    MD5STEP(F2, d, a, b, c, ] + );
    MD5STEP(F2, c, d, a, b, ] + );
    MD5STEP(F2, b, c, d, a, ] + );
    MD5STEP(F2, a, b, c, d, ] + );
    MD5STEP(F2, d, a, b, c, ] + );
    MD5STEP(F2, c, d, a, b, ] + );
    MD5STEP(F2, b, c, d, a, ] + );

    MD5STEP(F3, a, b, c, d, ] + );
    MD5STEP(F3, d, a, b, c, ] + );
    MD5STEP(F3, c, d, a, b, ] + );
    MD5STEP(F3, b, c, d, a, ] + );
    MD5STEP(F3, a, b, c, d, ] + );
    MD5STEP(F3, d, a, b, c, ] + );
    MD5STEP(F3, c, d, a, b, ] + );
    MD5STEP(F3, b, c, d, a, ] + );
    MD5STEP(F3, a, b, c, d, ] + );
    MD5STEP(F3, d, a, b, c, ] + );
    MD5STEP(F3, c, d, a, b, ] + );
    MD5STEP(F3, b, c, d, a, ] + );
    MD5STEP(F3, a, b, c, d, ] + );
    MD5STEP(F3, d, a, b, c, ] + );
    MD5STEP(F3, c, d, a, b, ] + );
    MD5STEP(F3, b, c, d, a, ] + );

    MD5STEP(F4, a, b, c, d, ] + );
    MD5STEP(F4, d, a, b, c, ] + );
    MD5STEP(F4, c, d, a, b, ] + );
    MD5STEP(F4, b, c, d, a, ] + );
    MD5STEP(F4, a, b, c, d, ] + );
    MD5STEP(F4, d, a, b, c, ] + );
    MD5STEP(F4, c, d, a, b, ] + );
    MD5STEP(F4, b, c, d, a, ] + );
    MD5STEP(F4, a, b, c, d, ] + );
    MD5STEP(F4, d, a, b, c, ] + );
    MD5STEP(F4, c, d, a, b, ] + );
    MD5STEP(F4, b, c, d, a, ] + );
    MD5STEP(F4, a, b, c, d, ] + );
    MD5STEP(F4, d, a, b, c, ] + );
    MD5STEP(F4, c, d, a, b, ] + );
    MD5STEP(F4, b, c, d, a, ] + );

    buf[] += a;
    buf[] += b;
    buf[] += c;
    buf[] += d;
}

#endif

md5.h

#ifndef MD5_H
#define MD5_H

#ifdef __alpha
typedef unsigned int uint32;
#else
typedef unsigned long uint32;
#endif

struct MD5Context {
    uint32 buf[];
    uint32 bits[];
    unsigned ];
};

void MD5Init(struct MD5Context *context);
void MD5Update(struct MD5Context *context, unsigned char const *buf,
           unsigned len);
], struct MD5Context *context);
], uint32 ]);

/*
 * This is needed to make RSAREF happy on some MS-DOS compilers.
 */
typedef struct MD5Context MD5_CTX;

#endif /* !MD5_H */

md5sum.c, md5.c, md5.h的更多相关文章

  1. python接口自动化测试二十七:密码MD5加密 ''' MD5加密 ''' # 由于MD5模块在python3中被移除 # 在python3中使用hashlib模块进行md5操作 import hashlib # 待加密信息 str = 'asdas89799,.//plrmf' # 创建md5对象 hl = hashlib.md5() # Tips # 此处必须声明encode # 若写法为

    python接口自动化测试二十七:密码MD5加密   ''' MD5加密 '''# 由于MD5模块在python3中被移除# 在python3中使用hashlib模块进行md5操作import has ...

  2. java 文件md5+字符串md5 实现

    import org.apache.commons.codec.binary.Hex; import org.apache.commons.codec.digest.DigestUtils; impo ...

  3. How to get the MD5 checksum for a file: md5sum, digest, csum, fciv

    LINUX: md5sum fileName In Linux, the md5sum utility can be used: aemtux1:/ % md5sum binary.file 0c46 ...

  4. linux下md5sum用法 (查看文件或字符串的md5值)

    MD5算法常常被用来验证网络文件传输的完整性,防止文件被人篡改.MD5 全称是报文摘要算法(Message-Digest Algorithm 5),此算法对任意长度的信息逐位进行计算,产生一个二进制长 ...

  5. md5sum/opensll md5

    http://m.blog.csdn.net/article/details?id=42041329 MD5算法常常被用来验证网络文件传输的完整性,防止文件被人篡改.MD5全称是报文摘要算法(Mess ...

  6. Linux下通过md5sum生成MD5文件&校验MD5

    生成md5值 随便找个文件执行:md5sum file_name  即可生成该文件对应md5值. 也可以一次生成多个文件的md5值:md5sum file_name1 file_name2 file_ ...

  7. md5 加密算法和升级

    在这里插一小节加密的吧,使用openssl库进行加密. 使用MD5加密 我们以一个字符串为例,新建一个文件filename.txt,在文件内写入hello ,然后在Linux下可以使用命令md5sum ...

  8. hashlib的md5计算

    hashlib的md5计算 hashlib概述 涉及加密服务:Cryptographic Services 其中 hashlib是涉及 安全散列 和 消息摘要 ,提供多个不同的加密算法借口,如SHA1 ...

  9. md5命令

    AIX 系统md5命令之csum #csum filename (默认使用md5算法) #csum -h SHA1 filename (使用sha1算法)Linux系统命令之md5sum 1. 背景 ...

随机推荐

  1. 【转】基于Qt, TUIO和TSLIB的嵌入式Linux下的多点触摸设计

    这个教程描述了在嵌入式linux下使用Qt如何设置一个支持多点触摸和单点触摸的输入系统.这里假定你已经有了对应的驱动程序,驱动可以从触摸屏的厂商那里获得或者使用一个linux 内核源码中已经存在的驱动 ...

  2. go - 复合类型 array, slice, map

    Go 语言支持复合类型: 数组:array 切片:slice 指针:pointer 字典:map 通道:chan 结构体:struct 接口:interface 1. array   同一类型数据的集 ...

  3. html和css实现一级菜单和二级菜单学习笔记

    实现一级菜单: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> ...

  4. OLEDB简介

    OLE DB(OLEDB)是微软的战略性的通向不同的数据源的低级应用程序接口.OLE DB不仅包括微软资助的标准数据接口开放数据库连通性(ODBC)的结构化查询语言(SQL)能力,还具有面向其他非SQ ...

  5. Oracle:grouping和rollup

    Oracle grouping和rollup简单测试 SQL,,,) group by department_id order by department_id; DEPARTMENT_ID SUM( ...

  6. Linux UGO和ACL权限管理

    自主访问控制(Discretionary Access Control, DAC)是指对象(比如程序.文件.进程)的拥有者可以任意修改或者授予此对象相应的权限.Linux的UGO(User, Grou ...

  7. 禁止 favicon.ico 请求

    favicon.ico 图标用于收藏夹图标和浏览器标签上的显示,如果不设置,浏览器会请求网站根目录的这个图标,如果网站根目录也没有这图标会产生 404.出于优化的考虑,要么就有这个图标,要么就禁止产生 ...

  8. .NET基础拾遗(1)类型语法基础和内存管理基础2

    二.内存管理和垃圾回收 2.1 .NET中栈和堆 每一个.NET应用程序最终都会运行在一个OS进程中,假设这个OS的传统的32位系统,那么每个.NET应用程序都可以拥有一个4GB的虚拟内存..NET会 ...

  9. GridView控件中插入自定义删除按钮并弹出确认框

    GridView控件中插入自定义删除按钮,要实现这个功能其实有多种方法,这里先记下我使用的方法,以后再添加其他方法. 一.实现步骤 1.在GridView中添加模板列(TemplateField). ...

  10. JwPlayer播放器【去除Logo、去除版本信息】

    效果图: <html> <head> <title>JwPlayer播放器@杯中红茶</title> <script type="tex ...