2012-04-23 63 views
-2

我正在使用SHA256在循环中生成C中的散列值以及要生成散列的值,同时循环的计数器变量也被传递。由于计数器变量的值在每次迭代中都会发生变化,所以我希望SHA256每次都返回不同的散列值。但是每次都会返回相同的散列值。 请帮忙!SHA256为每个输入生成相同的散列值

由于提前

代码:

int generate_sha256hash { 
    int loop = 0; 
    unsigned char hash_paramters[2] = {0}; 
    unsigned char device_ids[2] = {0,0}; 
    hash_paramters[0] = 0; 
    unsigned char pass_string = "PASSWORD"; 

    for(loop = 1; loop < 10; loop++) { 
    hash_paramters[1] = loop; 
    memcpy((unsigned char)(&input_info[0]),(unsigned char)hash_paramters ,2); 
    memcpy((unsigned char)(&input_info[2]),(unsigned char)device_ids,2); 
    memcpy((unsigned char)(&device_info[4]),(unsigned char*)au8defaultPwd,8); 

    printf("\n Generating Hash Value "); 

    hash_value = SHA256(device_info,14,au8HashValue); 
    } 
} 
+4

请问你可以发表一些代码吗?我猜你的计数器没有更新或没有被正确使用。 – KryptoniteDove 2012-04-23 11:42:39

+0

感谢KryptoniteDove的回应。我为此添加了代码片段。 – user1299759 2012-04-23 11:56:31

+0

int generate_sha256hash { \t int loop = 0; \t unsigned char hash_paramters [2] = {0}; \t unsigned char device_ids [2] = {0,0}; \t hash_paramters [0] = 0; \t unsigned char * pass_string =“PASSWORD”; (循环= 1;循环<10;循环++) \t {hash_paramters [1] = loop; memcpy((unsigned char *)(&input_info [0]),(unsigned char *)hash_paramters,2); memcpy((unsigned char *)(&input_info [2]),(unsigned char *)device_ids,2); memcpy((unsigned char *)(&device_info [4]),(unsigned char *)au8defaultPwd,8); \t \t printf(“\ n Generating Hash Value”); \t \t hash_value = SHA256(device_info,14,au8HashValue); \t} } – user1299759 2012-04-23 12:01:45

回答

2

您更改循环(依赖于计数器)input_info,但不使用它的哈希生成。您只使用device_info)。

而且我认为memcpy调用中的unsigned char的所有演员实际上都会转换为unsigned char*,因为他们应该这样做。您的代码中还有其他语法特性。

+0

sha256hash int loop = 0; unsigned char hash_paramter [2] = {0}; unsigned char device_id [2] = {0,0}; unsigned char * HashValue = NULL; unsigned char pwd [8] = {'P','A','S','S','W','O','R','D'}; hash_paramter [0] = 0; for(loop = 1; loop <10; loop ++){ hash_paramter [1] = loop; memcpy((unsigned char *)(&input_info [0]),(unsigned char *)hash_paramter,2); memcpy((unsigned char *)(&input_info [2]),(unsigned char *)device_id,2); memcpy((unsigned char *)(&input_info [4]),(unsigned char *)pwd,8); hash_value = SHA256(input_info,14,HashValue); } } – user1299759 2012-04-24 04:19:07

+0

感谢jpalecek的回复,我现在已经粘贴了正确的代码,请建议。 – user1299759 2012-04-24 04:22:13

相关问题