2011-11-23 14 views
1

私はPHP md5ハッシュと比較しているmd5ハッシュを作成しようとしています。c openssl libからのMD5がphp md5と一致しませんどうですか?

2があることを縫い目はありません同じ以下

私のCコードは、PHPのcompairison

なぜ2つのMD5が同じでないと一緒にいるのですか?

メイクコマンド

gcc -Wall -lssl -o test test.c 

コードtest.cの

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

unsigned char result[MD5_DIGEST_LENGTH]; 

// Print the MD5 sum as hex-digits. 
void print_md5_sum(unsigned char* md, char* md5) { 

    int i; 

    for(i=0; i < MD5_DIGEST_LENGTH; i++) { 

      char temp[3]; 
      snprintf(temp,sizeof(temp),"%02x",md[i]); 

      if(i == 0){ 
        strncpy(md5,temp,3); 
      }else{ 
        strncat(md5,temp,MD5_DIGEST_LENGTH); 
      } 
    } 

     printf("md5 is %s \n", md5); 
} 

int main(int argc, char** argv){ 

    char* file_buffer = "testtest"; 
    char buffer[MD5_DIGEST_LENGTH +1]; 

    MD5((unsigned char*) file_buffer, sizeof(file_buffer), result); 

    printf("length %i\n", MD5_DIGEST_LENGTH); 

     print_md5_sum(result,buffer); 
     printf("%s \n" ,buffer); 

     return 0; 
} 

PHPコード

<?php 
    echo md5("testtest"); 
?> 

結果

PHPのMD5

05a671c66aefea124cc08b76ea6d30bb 

CコードのMD5

098f6bcd4621d373cade4e832627b4f6 
+1

:あなたは、おそらくより良い、このようなMD5()関数を呼び出している。この場合

。 '05a671c66aefea124cc08b76ea6d30bb'は確かに' testtest''の正確な16進ダイジェストです – NullUserException

+1

FYI:md5( 'test')= 098f6bcd4621d373cade4e832627b4f6 – hakre

答えて

7

は、MD5の合計を超える計算にあなたの正確な長さを与えるものではありません。それはあなたのプラットフォームに応じて4または8のいずれかの可能性が高いポインタfile_bufferのサイズを与えるだけです。あなたのCコードと間違って何かがあります

MD5((unsigned char*) file_buffer, strlen(file_buffer), result); 
+0

ありがとうございました。 – MrNemus

+1

@MrNemus:どうぞよろしくお願いします。あなたの質問に答えた場合は、その答えを受け入れることを忘れないでください。 [方法はこちら](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) – tinman

関連する問題