linux openssl计算文件的md5值

https://www.openssl.org/source/
下载openssl安装包后,解压。进入解压后目录执行1 ./config 2 make 3 sudo make install(三连)
/usr/include/openssl/ 放了一些加密算法的头文件md2/md4/md5 ,sha, aes ,rsa,rc2/rc4/rc5等

md5对文件内容进行计算求出文件的md5值

#include 
#include 
#include 
#include 
#include 
#include 

int main()
{
     
	unsigned char buf[1024] = "";
	unsigned char md[16] = "";
	int fd = open("/root/work/file/print.cc",O_RDONLY);
	MD5_CTX ctx;
	MD5_Init(&ctx);
	while(1)
	{
     
		int size = read(fd,buf,1024);
		MD5_Update(&ctx,buf,size);
		if(size == 0 || size < 1024)
		{
     
			break;
		}

	}
	close(fd);
	MD5_Final(md,&ctx);
	for(auto i = 0;i < 16;i++)
	{
     
		printf("%02x",md[i]);
	}
	return 0;
}
root@jin:~/work/file# !g
gcc md_.c -o md_ -lcrypto -lssl
md_.c: In function ‘main’:
md_.c:27:11: warning: type defaults to ‘int’ in declaration of ‘i’ [-Wimplicit-int]
  for(auto i = 0;i < 16;i++)
           ^
root@jin:~/work/file# md5sum print.cc 
11122d64925aa51526fa9d2b01113651  print.cc
root@jin:~/work/file# ./md_ 
11122d64925aa51526fa9d2b01113651
root@jin:~/work/file# 

md5.h

/*
 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
 *
 * Licensed under the OpenSSL license (the "License").  You may not use
 * this file except in compliance with the License.  You can obtain a copy
 * in the file LICENSE in the source distribution or at
 * https://www.openssl.org/source/license.html
 */

#ifndef HEADER_MD5_H
# define HEADER_MD5_H

# include 

# ifndef OPENSSL_NO_MD5
# include 
# include 
# ifdef  __cplusplus
extern "C" {
     
# endif

/*
 * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
 * ! MD5_LONG has to be at least 32 bits wide.                     !
 * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
 */
# define MD5_LONG unsigned int
# define MD5_CBLOCK      64
# define MD5_LBLOCK      (MD5_CBLOCK/4)
# define MD5_DIGEST_LENGTH 16

typedef struct MD5state_st {
     
    MD5_LONG A, B, C, D;
    MD5_LONG Nl, Nh;
    MD5_LONG data[MD5_LBLOCK];
    unsigned int num;
} MD5_CTX;

int MD5_Init(MD5_CTX *c);
int MD5_Update(MD5_CTX *c, const void *data, size_t len);
int MD5_Final(unsigned char *md, MD5_CTX *c);
unsigned char *MD5(const unsigned char *d, size_t n, unsigned char *md);
void MD5_Transform(MD5_CTX *c, const unsigned char *b);
# ifdef  __cplusplus
}
# endif
# endif

#endif

你可能感兴趣的:(linux openssl计算文件的md5值)