求助:如何计算MD5值

xiximama 2010-08-30 10:32:55
文件经过设备,我需要计算文件的MD5值。从文件开始传输开始计算MD5值,到文件结束得到一个最终的MD5的值。
文件数据是STREAM模式传输的。并不是得到一个完整的文件计算MD5值。
哪位大侠知道,不胜感激。
...全文
1028 14 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
bobo364 2010-08-31
  • 打赏
  • 举报
回复
顶楼上的答案,我这没md5.h这个头文件没测试过

#include <openssl/md5.h>
#include <string.h>
#include <stdio.h>

int main()
{
struct MD5_CTX *ctx;
unsigned char outmd[32]={0};
int i=0;
unsigned char *String = "abcd";
printf("data=%s/n",String);
MD5_Init(ctx);
MD5_Update(ctx,String,strlen(String));
MD5_Final(outmd,ctx);
printf("MD5SUM:/n");
for(i=0;i<32;i++)
{
printf("%c",outmd[i]);
}
system("pause");
return 0;
}
justkk 2010-08-31
  • 打赏
  • 举报
回复
struct MD5_CTX *ctx; // 删除struct关键字
MD5_Init的参数是指针,而不是指针的地址。。MD5_Update、MD5_Final类似

编译时,gcc md5test.c -lssl
xiximama 2010-08-31
  • 打赏
  • 举报
回复
写了一个计算md5的程序,但是有错
1 #include <openssl/md5.h>
2 #include <string.h>
3 #include <stdio.h>
4
5 int main(void)
6 {
7
8 struct MD5_CTX *ctx;
9 unsigned char outmd[32]={0};
10 int i=0;
11 unsigned char *String = "abcd";
12 printf("data=%s/n",String);
13
14 MD5_Init(&ctx);
15 MD5_Update(&ctx,String,strlen(String));
16 MD5_Final(outmd,&ctx);
17
18 printf("MD5SUM:/n");
19 for(i=0;i<32;i++)
20 {
21 printf("%c",outmd[i]);
22 }
23
24 return 0;
25
26 }

md5test.c: In function 'main':
md5test.c:14: warning: passing argument 1 of 'MD5_Init' from incompatible pointer type
md5test.c:15: warning: passing argument 1 of 'MD5_Update' from incompatible pointer type
md5test.c:16: warning: passing argument 2 of 'MD5_Final' from incompatible pointer type
/tmp/cceDduO5.o: In function `main':
/home/yyy/test/md5test.c:14: undefined reference to `MD5_Init'
/home/yyy/test/md5test.c:15: undefined reference to `MD5_Update'
/home/yyy/test/md5test.c:16: undefined reference to `MD5_Final'
collect2: ld returned 1 exit status

请问哪位大侠知道是什么原因吗?
xiximama 2010-08-31
  • 打赏
  • 举报
回复
写了一个计算md5的程序,但是有错
1 #include <openssl/md5.h>
2 #include <string.h>
3 #include <stdio.h>
4
5 int main(void)
6 {
7
8 struct MD5_CTX *ctx;
9 unsigned char outmd[32]={0};
10 int i=0;
11 unsigned char *String = "abcd";
12 printf("data=%s/n",String);
13
14 MD5_Init(&ctx);
15 MD5_Update(&ctx,String,strlen(String));
16 MD5_Final(outmd,&ctx);
17
18 printf("MD5SUM:/n");
19 for(i=0;i<32;i++)
20 {
21 printf("%c",outmd[i]);
22 }
23
24 return 0;
25
26 }

md5test.c: In function 'main':
md5test.c:14: warning: passing argument 1 of 'MD5_Init' from incompatible pointer type
md5test.c:15: warning: passing argument 1 of 'MD5_Update' from incompatible pointer type
md5test.c:16: warning: passing argument 2 of 'MD5_Final' from incompatible pointer type
/tmp/cceDduO5.o: In function `main':
/home/yyy/test/md5test.c:14: undefined reference to `MD5_Init'
/home/yyy/test/md5test.c:15: undefined reference to `MD5_Update'
/home/yyy/test/md5test.c:16: undefined reference to `MD5_Final'
collect2: ld returned 1 exit status

请问哪位大侠知道是什么原因吗?
xiximama 2010-08-31
  • 打赏
  • 举报
回复
写了一个计算md5的程序,但是有错
1 #include <openssl/md5.h>
2 #include <string.h>
3 #include <stdio.h>
4
5 int main(void)
6 {
7
8 struct MD5_CTX *ctx;
9 unsigned char outmd[32]={0};
10 int i=0;
11 unsigned char *String = "abcd";
12 printf("data=%s/n",String);
13
14 MD5_Init(&ctx);
15 MD5_Update(&ctx,String,strlen(String));
16 MD5_Final(outmd,&ctx);
17
18 printf("MD5SUM:/n");
19 for(i=0;i<32;i++)
20 {
21 printf("%c",outmd[i]);
22 }
23
24 return 0;
25
26 }

md5test.c: In function 'main':
md5test.c:14: warning: passing argument 1 of 'MD5_Init' from incompatible pointer type
md5test.c:15: warning: passing argument 1 of 'MD5_Update' from incompatible pointer type
md5test.c:16: warning: passing argument 2 of 'MD5_Final' from incompatible pointer type
/tmp/cceDduO5.o: In function `main':
/home/yyy/test/md5test.c:14: undefined reference to `MD5_Init'
/home/yyy/test/md5test.c:15: undefined reference to `MD5_Update'
/home/yyy/test/md5test.c:16: undefined reference to `MD5_Final'
collect2: ld returned 1 exit status

请问哪位大侠知道是什么原因吗?
xiximama 2010-08-31
  • 打赏
  • 举报
回复
又做了一个实验,对于一个文件可以分段求MD5的值,最终得到的MD5的值都是一样的,不论是怎样分段的
程序secmd5test.c 如下:
#include <stdio.h>
#include <openssl/md5.h>
#include <openssl/rand.h>
#include <string.h>

void test1( unsigned char* input , int len , int n )
{
int i;
unsigned char md5[16]={0};
MD5_CTX ctx;
MD5_Init( &ctx );
printf("len=%d\n",len);
for( ;len >= n; input += n , len -= n )
MD5_Update( &ctx , input , n );
printf("len=%d\n",len);
if( len )
MD5_Update( &ctx , input , len );
MD5_Final( md5 , &ctx );

for( i = 0; i < 16; ++i )
printf( "%02x" , md5[i] );
printf( " : %d\n" , n );
}
int main()
{
int n;
unsigned char *String = "hello world!\n";
for(n=1;n<=strlen(String);n++)
test1( String, strlen(String) , n);
return 0;
}
[root@local md5]# gcc secmd5test.c -lssl
[root@local md5]# ./a.out
len=13
len=0
c897d1410af8f2c74fba11b1db511e9e : 1
len=13
len=1
c897d1410af8f2c74fba11b1db511e9e : 2
len=13
len=1
c897d1410af8f2c74fba11b1db511e9e : 3
len=13
len=1
c897d1410af8f2c74fba11b1db511e9e : 4
len=13
len=3
c897d1410af8f2c74fba11b1db511e9e : 5
len=13
len=1
c897d1410af8f2c74fba11b1db511e9e : 6
len=13
len=6
c897d1410af8f2c74fba11b1db511e9e : 7
len=13
len=5
c897d1410af8f2c74fba11b1db511e9e : 8
len=13
len=4
c897d1410af8f2c74fba11b1db511e9e : 9
len=13
len=3
c897d1410af8f2c74fba11b1db511e9e : 10
len=13
len=2
c897d1410af8f2c74fba11b1db511e9e : 11
len=13
len=1
c897d1410af8f2c74fba11b1db511e9e : 12
len=13
len=0
c897d1410af8f2c74fba11b1db511e9e : 13
[root@local md5]#

我的问题解决了,谢谢大家
xiximama 2010-08-31
  • 打赏
  • 举报
回复
谢谢 12楼的大侠
重新修改如下:
#include <openssl/md5.h>
#include <string.h>
#include <stdio.h>

int main(void)
{

MD5_CTX ctx;
unsigned char outmd[16]={0};
int i=0;
unsigned char *String = "hello world!\n";
printf("data=%s\n",String);

MD5_Init(&ctx);
MD5_Update(&ctx,String,strlen(String));
MD5_Final(outmd,&ctx);

printf("MD5SUM:\n");
for(i=0;i<16;i++)
{
printf("%02x",outmd[i]);
}
printf("\n");
return 0;

}

编译以后得到的md5的值与md5sum a.txt的结果一样
justkk 2010-08-31
  • 打赏
  • 举报
回复
那是因为你的a.txt中还有一个不可见字符\n
你的程序还有点问题
MD5_CTX *ctx; // 去掉*
MD5_Init(ctx); // 添加& MD5_Update、MD5_Final类似

12行修改一下,添加\n
unsigned char *String = "hello\n";

22行修改一下printf("%02x",outmd[i]);
xiximama 2010-08-31
  • 打赏
  • 举报
回复
按照9楼的大侠修改程序如下:
1 #include <openssl/md5.h>
2 #include <string.h>
3 #include <stdio.h>
4
5
6 int main(void)
7 {
8
9 MD5_CTX *ctx;
10 unsigned char outmd[16]={0};
11 int i=0;
12 unsigned char *String = "hello";
13 printf("data=%s\n",String);
14
15 MD5_Init(ctx);
16 MD5_Update(ctx,String,strlen(String));
17 MD5_Final(outmd,ctx);
18
19 printf("MD5SUM:\n");
20 for(i=0;i<16;i++)
21 {
22 printf("%x",outmd[i]);
23 }
24 printf("\n");
25 return 0;
26
27 }
使用gcc md5test.c -lssl编译
运行结果如下:
data=hello
MD5SUM:
5d41402abc4b2a76b9719d911017c592

但是使用linux的命令:md5sum a.txt
结果是
b1946ac92492d2347c6235b4d2611184 a.txt
(a.txt的内容就是hello)

请问大侠我需要从一个文件中得到数据,算出MD5值,希望这个MD5的值就是文件的MD5的值。为什么这样算出的不一样的。
ForestDB 2010-08-30
  • 打赏
  • 举报
回复
man MD5,即可得到答案。

#include <openssl/md5.h>

// 这个是一次性算的
unsigned char *MD5(const unsigned char *d, unsigned long n,
unsigned char *md);

// 这3个是多次算的
int MD5_Init(MD5_CTX *c);
int MD5_Update(MD5_CTX *c, const void *data,
unsigned long len);
int MD5_Final(unsigned char *md, MD5_CTX *c);

xiximama 2010-08-30
  • 打赏
  • 举报
回复
当前任务的计算MD5的要求:对于到来的一段文件数据就要计算出MD5值,直到文件结尾,计算出整个文件的MD5值。文件是分成很多段到来的,来过的数据计算出MD5值以后,数据就没有了。
有做过这方面的吗 或者了解这方面的
谢谢
justkk 2010-08-30
  • 打赏
  • 举报
回复
印象中md5的计算可以单次完成,也可以是一个循环过程
  • 打赏
  • 举报
回复
http://www.5dlinux.com/article/1/2007/linux_8846.html

linux下有md5sum可以直接计算文件的md5值。

xiximama 2010-08-30
  • 打赏
  • 举报
回复
自己顶 有知道的吗 帮忙说一下啊
trade_create_by_buyer-CSHARP-UTF-8 │ ├app_code ┈┈┈┈┈┈┈┈┈┈类文件夹 │ │ │ ├AlipayConfig.cs┈┈┈┈┈基础配置类文件 │ │ │ ├AlipayCore.cs┈┈┈┈┈┈支付宝接口公用函数类文件 │ │ │ ├AlipayNotify.cs┈┈┈┈┈支付宝通知处理类文件 │ │ │ ├AlipaySubmit.cs┈┈┈┈┈支付宝各接口请求提交类文件 │ │ │ └MD5.cs ┈┈┈┈┈┈┈┈┈MD5类库 │ ├log┈┈┈┈┈┈┈┈┈┈┈┈┈日志文件夹 │ ├default.aspx ┈┈┈┈┈┈┈┈支付宝接口入口文件 ├default.aspx.cs┈┈┈┈┈┈┈支付宝接口入口文件 │ ├notify_url.aspx┈┈┈┈┈┈┈服务器异步通知页面文件 ├notify_url.aspx.cs ┈┈┈┈┈服务器异步通知页面文件 │ ├return_url.aspx┈┈┈┈┈┈┈页面跳转同步通知文件 ├return_url.aspx.cs ┈┈┈┈┈页面跳转同步通知文件 │ ├Web.Config ┈┈┈┈┈┈┈┈┈配置文件(集成时删除) │ └readme.txt ┈┈┈┈┈┈┈┈┈使用说明文本 ※注意※ 需要配置的文件是: alipay_config.cs default.aspx default.aspx.csreturn_url.aspx return_url.aspx.cs notify_url.aspx notify_url.aspx.cs统一命名空间为:namespace Com.Alipiay ───────── 类文件函数结构 ───────── AlipayCore.cs public static Dictionary ParaFilter(SortedDictionary dicArrayPre) 功能:除去数组中的空和签名参数并以字母a到z的顺序排序 输入:SortedDictionary dicArrayPre 过滤前的参数组 输出:Dictionary 去掉空与签名参数后的新签名参数组 public static string CreateLinkString(Dictionary dicArray) 功能:把数组所有元素,按照“参数=参数”的模式用“&”字符拼接成字符串 输入:Dictionary dicArray 需要拼接的数组 输出:string 拼接完成以后的字符串 public static string CreateLinkStringUrlencode(Dictionary dicArray, Encoding code) 功能:把数组所有元素,按照“参数=参数”的模式用“&”字符拼接成字符串,并对参数做urlencode 输入:Dictionary dicArray 需要拼接的数组 Encoding code 字符编码 输出:string 拼接完成以后的字符串 public static void log_result(string sPath, string sWord) 功能:写日志,方便测试(看网站需求,也可以改成存入数据库) 输入:string sPath 日志的本地绝对路径 string sWord 要写入日志里的文本内容 public static string GetAbstractToMD5(Stream sFile) 功能:获取文件的md5摘要 输入:Stream sFile 文件流 输出:string MD5摘要结果 public static string GetAbstractToMD5(byte[] dataFile) 功能:获取文件的md5摘要 输入:byte[] dataFile 文件流 输出:string MD5摘要结果 ┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉ MD5.cs public static string Sign(string prestr, string key, string _input_charset) 功能:签名字符串 输入:string prestr 需要签名的字符串 string key 密钥 string _input_charset 编码格式 输出:string 签名结果 public static bool Verify(string prestr, string sign, string key, string _input_charset) 功能:验证签名 输入:string prestr 需要签名的字符串 string sign 签名结果 string key 密钥 string _input_charset 编码格式 输出:string 验证结果 ┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉ AlipayNotify.cs public Notify() 功能:构造函数 从配置文件中初始化变量 public bool Verify(SortedDictionary inputPara, string notify_id, string sign) 功能:验证消息是否是支付宝发出的合法消息 输入:SortedDictionary inputPara 通知返回参数数组 string notify_id 通知验证ID string sign 支付宝生成的签名结果 输出:bool 验证结果 private string GetPreSignStr(SortedDictionary inputPara) 功能:获取待签名字符串(调试用) 输入:SortedDictionary inputPara 通知返回参数数组 输出:string 待签名字符串 private bool GetSignVeryfy(SortedDictionary inputPara, string sign) 功能:获取返回回来的待签名数组签名后结果 输入:SortedDictionary inputPara 通知返回参数数组 string sign 支付宝生成的签名结果 输出:bool 签名验证结果 private string GetResponseTxt(string notify_id) 功能:获取是否是支付宝服务器发来的请求的验证结果 输入:string notify_id 通知验证ID 输出:string 验证结果 private string Get_Http(string strUrl, int timeout) 功能:获取远程服务器ATN结果 输入:string strUrl 指定URL路径地址 int timeout 超时时间设置 输出:string 服务器ATN结果字符串 ┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉ AlipaySubmit.cs private static string BuildRequestMysign(Dictionary sPara) 功能:生成签名结果 输入:Dictionary sPara 要签名的数组 输出:string 签名结果字符串 private static Dictionary BuildRequestPara(SortedDictionary sParaTemp) 功能:生成要请求给支付宝的参数数组 输入:SortedDictionary sParaTemp 请求前的参数数组 输出:Dictionary 要请求的参数数组 private static string BuildRequestParaToString(SortedDictionary sParaTemp, Encoding code) 功能:生成要请求给支付宝的参数数组 输入:SortedDictionary sParaTemp 请求前的参数数组 Encoding code 字符编码 输出:string 要请求的参数数组字符串 public static string BuildRequest(SortedDictionary sParaTemp, string strMethod, string strButtonValue) 功能:建立请求,以表单HTML形式构造(默认) 输入:SortedDictionary sParaTemp 请求参数数组 string strMethod 提交方式。两个可选:post、get string strButtonValue 确认按钮显示文字 输出:string 提交表单HTML文本 public static string BuildRequest(SortedDictionary sParaTemp) 功能:建立请求,以模拟远程HTTP的POST请求方式构造并获取支付宝的处理结果 输入:SortedDictionary sParaTemp 请求参数数组 输出:string 支付宝处理结果 public static string BuildRequest(SortedDictionary sParaTemp, string strMethod, string fileName, byte[] data, string contentType, int lengthFile) 功能:建立请求,以模拟远程HTTP的POST请求方式构造并获取支付宝的处理结果 输入:SortedDictionary sParaTemp 请求参数数组 string strMethod 提交方式。两个可选:post、get string fileName 文件绝对路径 byte[] data 文件数据 string contentType 文件内容类型 int lengthFile 文件长度 输出:string 支付宝处理结果 public static string Query_timestamp() 功能:用于防钓鱼,调用接口query_timestamp来获取时间戳的处理函数 输出:string 时间戳字符串 ────────── 出现问题,求助方法 ────────── 如果在集成支付宝接口时,有疑问或出现问题,可使用下面的链接,提交申请。 https://b.alipay.com/support/helperApply.htm?action=supportHome 我们会有专门的技术支持人员为您处理

70,023

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧