2012-04-23 61 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

相關問題