📄 mad_md5.c
字号:
/* $Author: peltotas $ $Date: 2006/03/14 11:03:00 $ $Revision: 1.3 $ *//* * MAD-FLUTELIB: Implementation of FLUTE protocol. * Copyright (c) 2003-2006 TUT - Tampere University of Technology * main authors/contacts: jani.peltotalo@tut.fi and sami.peltotalo@tut.fi * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */#include "flute_inc.h"#ifdef USE_OPENSSL/* * This function calculates MD5 for file. * * Params: char *filename: Pointer to buffer containg file name. * * Return: char*: Pointer to buffer containing file's MD5, * NULL: In error cases. */char* file_md5(char *filename) { /*char *buf;*/#ifdef WIN32 struct __stat64 file_stats;#else struct stat64 file_stats;#endif int nbytes; FILE *fp; char *md5 = NULL; char b64_digest[MD5_DIGEST_LENGTH*2]; BIO *bio, *b64, *mem; unsigned char md5_digest[MD5_DIGEST_LENGTH]; MD5_CTX ctx; int i; char zBuf[10240]; /*10240*/ memset(md5_digest, 0, MD5_DIGEST_LENGTH); memset(b64_digest, 0, MD5_DIGEST_LENGTH*2); MD5_Init(&ctx);#ifdef WIN32 fp = fopen(filename, "rb");#else fp = fopen64(filename, "rb");#endif if(fp < 0) { printf("mad_md5.c: fopen error\n"); fflush(stdout); return NULL; }#ifdef WIN32 if(_stat64(filename, &file_stats) == -1) {#else if(stat64(filename, &file_stats) == -1) {#endif printf("Error: %s is not valid file name\n", filename); fflush(stdout); return NULL; } for(;;) { nbytes = fread(zBuf, 1, sizeof(zBuf), fp); if(nbytes <= 0) { break; } MD5_Update(&ctx, (unsigned char*)zBuf, (unsigned)nbytes); } /*if(ferror(fp)) { printf("fread error, file: %s\n", filename); fflush(stdout); free(buf); fclose(fp); return NULL; }*/ MD5_Final(md5_digest, &ctx); b64 = BIO_new(BIO_f_base64()); mem = BIO_new(BIO_s_mem()); bio = BIO_push(b64, mem); BIO_write(bio, md5_digest, MD5_DIGEST_LENGTH); BIO_flush(bio); BIO_gets(mem, b64_digest, MD5_DIGEST_LENGTH*2); BIO_free_all(bio); for(i = 0; i < MD5_DIGEST_LENGTH*2; i++) { if(b64_digest[i] <= ' ') { b64_digest[i] = '\0'; } } if(!(md5 = (char*)calloc((strlen(b64_digest) + 1), sizeof(char)))) { printf("Could not alloc memory for md5 buffer!\n"); fflush(stdout); return NULL; } memcpy(md5, b64_digest, strlen(b64_digest)); if(fclose(fp) != 0) { printf("fclose failed, errno: %i\n", errno); } return md5;}/* * This function calculates MD5 for buffer. * * Params: char *buffer: Pointer to buffer containg data, * ULONGLONG/unsigned long long length: Buffer length. * * Return: char*: Pointer to buffer containing data's MD5, * NULL: In error cases. */char* buffer_md5(char *buffer, #ifdef WIN32 ULONGLONG length#else unsigned long long length#endif ) { char b64_digest[MD5_DIGEST_LENGTH*2]; BIO *bio, *b64, *mem; unsigned char md5_digest[MD5_DIGEST_LENGTH]; MD5_CTX ctx; int i; char *md5 = NULL; memset(md5_digest, 0, MD5_DIGEST_LENGTH); memset(b64_digest, 0, MD5_DIGEST_LENGTH*2); MD5_Init(&ctx); MD5_Update(&ctx, buffer, (unsigned int)length); MD5_Final(md5_digest, &ctx); b64 = BIO_new(BIO_f_base64()); mem = BIO_new(BIO_s_mem()); bio = BIO_push(b64, mem); BIO_write(bio, md5_digest, MD5_DIGEST_LENGTH); BIO_flush(bio); BIO_gets(mem, b64_digest, MD5_DIGEST_LENGTH*2); BIO_free_all(bio); for(i = 0; i < MD5_DIGEST_LENGTH*2; i++) { if(b64_digest[i] <= ' ') { b64_digest[i] = '\0'; } } if(!(md5 = (char*)calloc((strlen(b64_digest) + 1), sizeof(char)))) { printf("Could not alloc memory for md5 buffer!\n"); fflush(stdout); return NULL; } memcpy(md5, b64_digest, strlen(b64_digest)); return md5;}#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -