static int find_hashid(const char *name) { int res = -1; size_t hnum, b, i; char *mname; hnum = mhash_count(); for (i = 0; i <= hnum; i++) { mname = mhash_get_hash_name(i); if (mname == NULL) continue; b = strcasecmp(name, mname); free(mname); if (!b) { res = i; break; } } return res; }
/* **! method: string query_name() **! Get the name of the selected hash routine. **! name: query_name - Get hash routine name **! returns: **! The name of the selected hash routine, zero if none is selected or **! -1 if the selected hash is invalid. */ void f_hash_query_name(INT32 args) { char *name; pop_n_elems(args); if(THIS->type != -1) { name = mhash_get_hash_name(THIS->type); if(name == NULL) { push_int(-1); } else { push_text(name); free(name); } } else { push_int(0); } }
int main(void) { int i, buf_len; MHASH td1, td2, td3; const unsigned char *buf = "This is a test buffer to test saving and restoring, see?"; unsigned char *hash1, *hash2; hashid alg; char mem[1024]; int mem_size = sizeof(mem); buf_len = strlen(buf); /* NOTE: For laziness sake, I just loop through the enum, skipping invalid integers. If the enum should change, this loop will have to change! */ for (alg = 0; alg <= mhash_count(); ++alg) { /* if algorithm does not exist */ if (mhash_get_hash_name_static( alg)==NULL) continue; printf("Testing save/restore for algorithm %s: ", mhash_get_hash_name(alg)); td1 = mhash_init(alg); if (td1 == MHASH_FAILED) { fprintf(stderr, "Failed to init td1.\n"); exit(1); } for (i = 0; i < buf_len; ++i) mhash(td1, buf+i, 1); hash1 = mhash_end(td1); /* printf("Hash 1: "); for (i = 0; i < mhash_get_block_size(alg); ++i) printf("%.2x", hash1[i]); printf("\n"); */ td2 = mhash_init(alg); if (td2 == MHASH_FAILED) { fprintf(stderr, "Failed to init td2.\n"); exit(1); } for (i = 0; i < buf_len/2; ++i) mhash(td2, buf+i, 1); if (mhash_save_state_mem(td2, mem, &mem_size)!=0) { fprintf(stderr, "Error saving state. Size: %d\n", mem_size); exit(1); } td3 = mhash_restore_state_mem( mem); if (td3 == MHASH_FAILED) { fprintf(stderr, "Error restoring state.\n"); exit(1); } for (i = buf_len/2; i < buf_len; ++i) mhash(td3, buf+i, 1); hash2 = mhash_end(td3); /* printf("Hash 2: "); for (i = 0; i < mhash_get_block_size(alg); ++i) printf("%.2x", hash2[i]); printf("\n"); */ if (memcmp(hash1, hash2, mhash_get_block_size(alg)) == 0) { printf("Ok\n"); } else { printf("Failed\n"); exit(1); } } exit(0); }
int main(void) { mutils_word32 i; mutils_word32 buf_len; MHASH td1, td2, td3; const mutils_word8 *buf = (mutils_word8 *) "This is a test buffer to test saving and restoring, see?"; mutils_word8 *hash1; mutils_word8 *hash2; hashid alg; mutils_word8 mem[1024]; mutils_word32 mem_size = sizeof(mem); buf_len = mutils_strlen(buf); /* * NOTE: For laziness sake, I just loop through the enum, * skipping invalid integers. * * If the enum should change, this loop will have to change! */ for (alg = 0; alg <= mhash_count(); ++alg) { /* if algorithm does not exist */ if (mhash_get_hash_name_static(alg) == (mutils_word8 *) NULL) continue; printf("Testing save/restore for algorithm %s: ", mhash_get_hash_name(alg)); td1 = mhash_init(alg); if (td1 == MHASH_FAILED) { fprintf(stderr, "Failed to init td1.\n"); exit(MUTILS_INVALID_FUNCTION); } for (i = 0; i < buf_len; ++i) { mhash(td1, buf+i, 1); } hash1 = mhash_end(td1); td2 = mhash_init(alg); if (td2 == MHASH_FAILED) { fprintf(stderr, "Failed to init td2.\n"); exit(MUTILS_INVALID_FUNCTION); } for (i = 0; i < buf_len/2; ++i) { mhash(td2, buf+i, 1); } if (mhash_save_state_mem(td2, mem, &mem_size)!=0) { fprintf(stderr, "Error saving state. Size: %d\n", mem_size); exit(MUTILS_INVALID_RESULT); } td3 = mhash_restore_state_mem( mem); if (td3 == MHASH_FAILED) { fprintf(stderr, "Error restoring state.\n"); exit(MUTILS_INVALID_RESULT); } for (i = buf_len/2; i < buf_len; ++i) { mhash(td3, buf+i, 1); } hash2 = mhash_end(td3); if (mutils_memcmp(hash1, hash2, mhash_get_block_size(alg)) == 0) { printf("Ok\n"); } else { printf("Failed\n"); exit(MUTILS_INVALID_RESULT); } } exit(MUTILS_OK); }