C計算檔案的MD5值

2021-09-14 08:29:45 字數 3354 閱讀 2204

filemd5.c檔案

#include#include#include#define f(x, y, z) (((x) & (y)) | ((~x) & (z)))

#define g(x, y, z) (((x) & (z)) | ((y) & (~z)))

#define h(x, y, z) ((x) ^ (y) ^ (z))

#define i(x, y, z) ((y) ^ ((x) | (~z)))

#define rl(x, y) (((x) << (y)) | ((x) >> (32 - (y)))) //x向左迴圈移y位

#define pp(x) (x<<24)|((x<<8)&0xff0000)|((x>>8)&0xff00)|(x>>24) //將x高低位互換,例如pp(aabbccdd)=ddccbbaa

#define ff(a, b, c, d, x, s, ac) a = b + (rl((a + f(b,c,d) + x + ac),s))

#define gg(a, b, c, d, x, s, ac) a = b + (rl((a + g(b,c,d) + x + ac),s))

#define hh(a, b, c, d, x, s, ac) a = b + (rl((a + h(b,c,d) + x + ac),s))

#define ii(a, b, c, d, x, s, ac) a = b + (rl((a + i(b,c,d) + x + ac),s))

static unsigned a,b,c,d,a,b,c,d,i,len,flen[2],x[16]; //i臨時變數,len檔案長,flen[2]為64位二進位制表示的檔案初始長度

static char filename[200]; //檔名

static file *fp;

typedef struc***5_stru;

static void _md5(md5_stru *pmd5)

static void md5()

int calcfilemd5(const char* filename,unsigned char md5[16])

; unsigned int flen[2];

unsigned int len,temp;

int i;

if (!(fp=fopen(filename,"rb")))

//以二進位制開啟檔案

fseek(fp, 0, seek_end); //檔案指標轉到檔案末尾

if((len=ftell(fp))==-1)

//ftell函式返回long,最大為2gb,超出返回-1

rewind(fp); //檔案指標復位到檔案頭

strumd5.a=0x67452301,strumd5.b=0xefcdab89,strumd5.c=0x98badcfe,strumd5.d=0x10325476; //初始化鏈結變數

flen[1]=len/0x20000000; //flen單位是bit

flen[0]=(len%0x20000000)*8;

memset(strumd5.x,0,64); //初始化x陣列為0

fread(strumd5.x,4,16,fp); //以4位元組為一組,讀取16組數

for(i=0;i55)

memcpy(strumd5.x+14,flen,8); //檔案末尾加入原檔案的bit長度

_md5(&strumd5);

fclose(fp);

//printf("md5 code:%08x%08x%08x%08x\n",pp(strumd5.a),pp(strumd5.b),pp(strumd5.c),pp(strumd5.d)); //高低位逆反輸出

//sprintf(md5buff,"%08x%08x%08x%08x", pp(strumd5.a), pp(strumd5.b), pp(strumd5.c), pp(strumd5.d))

i = 0;

temp = pp(strumd5.a);

md5[i++] = (temp >> 24 & 0xff);

md5[i++] = (temp >> 16 & 0xff);

md5[i++] = (temp >> 8 & 0xff);

md5[i++] = (temp >> 0 & 0xff);

temp = pp(strumd5.b);

md5[i++] = (temp >> 24 & 0xff);

md5[i++] = (temp >> 16 & 0xff);

md5[i++] = (temp >> 8 & 0xff);

md5[i++] = (temp >> 0 & 0xff);

temp = pp(strumd5.c);

md5[i++] = (temp >> 24 & 0xff);

md5[i++] = (temp >> 16 & 0xff);

md5[i++] = (temp >> 8 & 0xff);

md5[i++] = (temp >> 0 & 0xff);

temp = pp(strumd5.d);

md5[i++] = (temp >> 24 & 0xff);

md5[i++] = (temp >> 16 & 0xff);

md5[i++] = (temp >> 8 & 0xff);

md5[i++] = (temp >> 0 & 0xff);

return 0;

}​

測試檔案 test.c

#include #include #include static void printf_hexstream(unsigned char *pdata, int len, const char* strtag)

; int ret = 0;

int pos = 0;

for (i = 0; iif (strtag)

else

// printf("\r\n%s:%s\r\n",strtag,buff);}

extern int calcfilemd5(const char* filename, unsigned char md5[16]);

void testfilemd5()

; printf("\r\n--------test file md5-----------\r\n");

rslt = calcfilemd5("hexeditor.dll",md5);

if (rslt == 0)

else }

int main(int argc, char *ar**)

c 計算檔案的MD5值

計算檔案的 md5 值 要計算 md5 值的檔名和路徑 md5 值16進製制字串 public static string md5file string filename 計算檔案的雜湊值 要計算雜湊值的檔名和路徑 演算法 sha1,md5 雜湊值16進製制字串 public static stri...

python計算檔案的MD5值

一 import sys import hashlib import os.path filename sys.argv 1 if os.path.isfile filename fp open filename,rb contents fp.read fp.close print hashlib....

python計算檔案的md5值

最近要開發乙個基於python的合併資料夾 目錄的程式,本來的想法是基於修改時間的比較,即判斷檔案有沒有改變,比較兩個檔案的修改時間即可。這個想法在windows的pc端下測試沒有問題。但是當把乙個檔案從pc端複製到優盤時出現了乙個問題,複製到優盤的檔案比pc端的檔案慢了兩秒鐘!這裡我用的複製函式是...