2014-05-25 135 views
0

我需要在main函數中打印一個字符串(名爲hex)。該字符串是digestInHex函數的參數,該函數用於keccak函數中。所以這裏是功能:如何打印C中另一個函數使用的函數的參數?

void digestInHex(unsigned long long state[][5], unsigned char *buffer, 
       unsigned char *bufferLocation, int bufferSize, int size, char *hex) 
{ 
    unsigned char *byte; 
    int i, j; 
    unsigned long long *x; 

    const char *hexValues = "abcdef"; 

    byte = (unsigned char *)malloc(size * sizeof(char)); 

    hex = (char *)malloc(((size << 1) + 1) * sizeof(char)); 

    hex[size << 1] = '\0'; 

    /* Padding */ 
    bufferLocation[0] = 1; 
    ++bufferLocation; 

    while (bufferLocation != &buffer[bufferSize/8]) 
    { 
     bufferLocation[0] = 0; 
     ++bufferLocation; 
    } 

    buffer[(bufferSize >> 3) - 1] |= 0x80; 

    bufferLocation = buffer; 

    x = (unsigned long long *)buffer; 

    for (j = 0; j * 64 < bufferSize; ++j) 
    { 
     state[j/5][j % 5] |= x[j]; 
    } 

    round(state); 

    /* Squeezing */ 
    memcpy(byte, state, size); 
    reset(state); 

    bufferLocation = buffer; 

    for (i = 0; i < size; ++i) 
    { 
     hex[i << 1] = hexValues[byte[i] >> 4]; 
     hex[(i << 1) + 1] = hexValues[byte[i] & 15]; 
    } 

    free(byte); 

    // printf("%s\n", hex); 

    free(hex); 
} 

void keccak(const char *str, enum bitLength hashValueBitLength, char *hex) 
{ 
    int i = 0; 
    int j; 
    unsigned char *buffer; 
    unsigned char *bufferLocation; 
    const int bufferSize = 1600 - (hashValueBitLength * 2); 
    unsigned long long *x; 
    unsigned long long state[5][5]; 

    buffer = (unsigned char *)malloc(bufferSize * sizeof(char)); 
    bufferLocation = buffer; 

    reset(state); 

    while (str[i] != '\0') 
    { 
     bufferLocation[0] = (unsigned char)str[i]; 
     ++bufferLocation; 

     if (bufferLocation == &buffer[bufferSize/8]) 
     { 
      bufferLocation = buffer; 
      x = (unsigned long long *)buffer; 

      for (j = 0; j * 64 < bufferSize; ++j) 
      { 
       state[j/5][j % 5] |= x[j]; 
      } 

      round(state); 
     } 

     ++i; 
    } 

    digestInHex(state, buffer, bufferLocation, bufferSize, hashValueBitLength/8, hex); 

    free(buffer); 
} 

正如你所看到的,keccak函數在最後使用了digestInHex函數。 digestInHex中的十六進制字符串保留給定輸入的哈希輸出。

主要我需要使用switch-case來比較我的新舊項目的時間值。要做到這一點,我需要運行keccak 100萬次才能看清時間差異。不能看到散列輸出100萬次,我無法直接在digestInHex中打印十六進制字符串,這就是爲什麼我在digestInHex中創建了十六進制註釋的printf。

另外我想在開關櫃中顯示散列輸出。但是當我這樣做時它會打印出空白。那麼我怎麼能打印像「4d741b6f1eb29cb2a9b9911c82f56fa8d73b04959d3d9d222895df6c0b28aa15」哈希輸出?下面是主要的:

int main() 
{ 
    int i; 
    clock_t begin, end; 
    double timeSpend; 
    int n; 

    printf("Enter 1 to see Old Project's time value\n"); 
    printf("Enter 2 to see New Project's time value\n\n"); 
    printf("Enter 3 to see Old Project's hash output\n"); 
    printf("Enter 4 to see New Project's hash output\n\n"); 
    printf("Please enter a value according to above: "); 

iterator: 

    scanf_s("%d", &n); 

    switch (n) 
    { 
    case 1: 
     begin = clock(); 
     for (i = 0; i < 1000000; ++i) 
      keccakOld("The quick brown fox jumps over the lazy dog", KECCAK_256, hexOld); 
     end = clock(); 
     timeSpend = (double)(end - begin)/CLOCKS_PER_SEC; 

     printf("%f sec.\n", timeSpend); 

     break; 

    case 2: 
     begin = clock(); 
     for (i = 0; i < 1000000; ++i) 
      keccak("The quick brown fox jumps over the lazy dog", KECCAK_256, hex); 
     end = clock(); 
     timeSpend = (double)(end - begin)/CLOCKS_PER_SEC; 

     printf("%f sec.\n", timeSpend); 

     break; 

    case 3: 
     keccakOld("The quick brown fox jumps over the lazy dog", KECCAK_256, hexOld); 
     printf("%s\n", hexOld); 

     break; 

    case 4: 
     keccak("The quick brown fox jumps over the lazy dog", KECCAK_256, hex); 
     printf("%s\n", hex); 

     break; 

    default: 
     printf("Please re-enter a correct value: "); 
     goto iterator; 

     break; 
    } 

    return 0; 
} 

回答

1

要得到分配,並在下面digestInHex()定下了到main()內存的引用傳遞一個參考指針hex

還調整free()在它被重新分配之前,但在它被打印之前釋放它。

要做到如下這樣調整代碼:

變化

void digestInHex(unsigned long long state[][5], unsigned char* buffer, 
    unsigned char* bufferLocation, int bufferSize, int size, char* hex) 
{ 

成爲

void digestInHex(unsigned long long state[][5], unsigned char* buffer, 
    unsigned char* bufferLocation, int bufferSize, int size, char ** hex) 
{ 
    free(*hex); 

digestInHex()末刪除調用free(hex)

digestInHex()將所有hex更改爲(*hex)

變化

void keccak(const char* str, enum bitLength hashValueBitLength, char* hex) 
{ 

成爲

void keccak(const char* str, enum bitLength hashValueBitLength, char** hex) 
{ 

保留通話digestInHex()因爲它是隻使用hex

main()定義和初始化:

char * hex = NULL; 

更改爲keccak()digestInHex)所有呼叫採取&hexhex

同樣在main()在返回之前添加最後的free(hex)

相關問題