语言

Menu
Sites
Language
Tizen Native MD5 사용방법 문의 / How to use incoding MD5?

Tizen Native 로 MD5 인코딩 기능을 구현하려고 합니다.
I want MD5 incoding in Tizen Native App.

 

아래와 같이 코드를 작성하였습니다.
My source code is below.

 

 

===================================================
include <openssl/md5.h>

 

    char lsBuff[] = "abc";
    unsigned char lucpMD5[100];
    unsigned long lulLen = strlen(lsBuff);
    MD5_CTX lmdContext;

    MD5_Init(&lmdContext);
    MD5_Update(&lmdContext,lsBuff,lulLen);
    MD5_Final(lucpMD5, &lmdContext);
    dlog_print(DLOG_INFO, "tag", "MD5 result: %s", lucpMD5);
===================================================

 

"abc" 를 인코딩한 결과는 아래와 같이 만들어 집니다.
The result of "abc" is below.

 

MD5 result: РPШ<╥O░╓Ц?}(сr░Юл╕╕Ц│╕иuр┐√Dм╢алл╕

 

 

하지만 정상적으로는 '90150983cd24fb0d6963f7d28e17f72' 이 나와야 합니다.
But the correct result is '90150983cd24fb0d6963f7d28e17f72'.

 

이 문제를 어떻게 해결할수 있을까요?
How can I fix this problem.

 

 

编辑者为: Dong-Geun Jung 15 6月, 2016
查看选择的答案

响应

4 回复
Mark as answer
Nafisul Islam Kiron

Hello, please try this:

char *my_md5(const char* md5_str) {
    unsigned char digest[16];
	char* string = malloc (1 + strlen (md5_str));

	if (string)
	       strcpy (string, md5_str);

	dlog_print(DLOG_INFO, "MD5", "String = %s", string);

	MD5_CTX ctx;
	MD5_Init(&ctx);
	MD5_Update(&ctx, string, strlen(string));
	MD5_Final(digest, &ctx);

	char mdString[33];
	for (int i = 0; i < 16; i++)
		sprintf(&mdString[i*2], "%02x", (unsigned int)digest[i]);

    return mdString;
}

Then use it like:

/* MD5 */
/* Create the MD5 of string here */
char *output = my_md5("tizen");
dlog_print(DLOG_INFO, "MD5", "MD5 = %s", output);
free(output);

 

Enjoy!

Dong-Geun Jung

Thank you for your answer and source code.

If incode "099997" by MD5 the correct result is [195ce4341ef32dc4198deba9de0bd67]

But when run your code in Wearable Emulator, the result is [´95ce4341ef32dc4198d0eba9de0bd67]

And when run your code in Gear S2, the result is [195ce434Ð@8f]

Is there any way to fix this?

Dong-Geun Jung

I tried again.

The result is 32 chars. But in Android the result is 31 chars.

When encode '099997' in Android, the result is '195ce4341ef32dc4198deba9de0bd67'.

And encode same value in Gear S2, the result is '195ce4341ef32dc4198d0eba9de0bd67'.

 

When encode '123456' in Android, the result is 'e1adc3949ba59abbe56e057f2f883e';

And encode same value in Gear S2, the result is 'e10adc3949ba59abbe56e057f20f883e'.

Which is correct?
Is there any way match together?

Dong-Geun Jung

My android code has bug.

Thank you very much.

Have a nice day!