Beispiel #1
0
/**
 * (Re)-initialize RHash context, to calculate hash sums.
 *
 * @param info the file data
 */
static void re_init_rhash_context(struct file_info *info)
{
	if(rhash_data.rctx != 0) {
		if(opt.mode & (MODE_CHECK | MODE_CHECK_EMBEDDED)) {
			/* a set of hash sums can change from file to file */
			rhash_free(rhash_data.rctx);
			rhash_data.rctx = 0;
		} else {
			info->rctx = rhash_data.rctx;
			if(!opt.bt_batch_file) {
				rhash_reset(rhash_data.rctx);
			} else {
				/* add another file to the torrent batch */
				rhash_transmit(RMSG_BT_ADD_FILE, rhash_data.rctx,
					RHASH_STR2UPTR((char*)file_info_get_utf8_print_path(info)), (rhash_uptr_t)&info->size);
				return;
			}
		}
	}

	if(rhash_data.rctx == 0) {
		info->rctx = rhash_data.rctx = rhash_init(info->sums_flags);
	}

	/* re-initialize BitTorrent data */
	if(info->sums_flags & RHASH_BTIH) {
		init_btih_data(info);
	}
}
Beispiel #2
0
/**
 * Verify for all algorithms, that rhash_final() returns the same result as
 * rhash_print().
 */
static void test_results_consistency(void)
{
	const char * msg = "a";
	size_t msg_size = strlen(msg);

	size_t digest_size;
	struct rhash_context *ctx;
	unsigned char res1[70];
	char res2[70];
	unsigned i, hash_id;

	for(i = 0, hash_id = 1; (hash_id & RHASH_ALL_HASHES); hash_id <<= 1, i++) {
		digest_size = rhash_get_digest_size(hash_id);
		assert(digest_size < 70);

		ctx = rhash_init(hash_id);

#ifdef USE_BTIH_WITH_TEST_FILENAME
		if((hash_id & RHASH_BTIH) != 0) {
			unsigned long long total_size = msg_size;
			rhash_transmit(RMSG_BT_ADD_FILE, ctx, RHASH_STR2UPTR("test.txt"), (rhash_uptr_t)&total_size);
		}
#endif

		rhash_update(ctx, msg, msg_size);
		rhash_final(ctx, res1);
		rhash_print(res2, ctx, hash_id, RHPR_RAW);
		rhash_free(ctx);

		if(memcmp(res1, res2, digest_size) != 0) {
			log_message("failed: inconsistent %s(\"%s\") hash results\n", rhash_get_name(hash_id), msg);
		}
	}
}
Beispiel #3
0
/**
 * Free data allocated by an rhash_t object
 *
 * @param ptr pointer to rhash_t object
 */
void rhash_destroy(struct rhash_t* ptr)
{
	free_print_list(ptr->print_list);
	rsh_str_free(ptr->template_text);
	if(ptr->rctx) rhash_free(ptr->rctx);
	IF_WINDOWS(restore_console());
}
Beispiel #4
0
/**
 * Hash a repeated message chunk by specified hash function.
 *
 * @deprecated This function shall be removed soon.
 *
 * @param hash_id hash function identifier
 * @param message a message chunk to hash
 * @param msg_size message chunk size
 * @param count number of chunks
 * @param out computed hash
 * @return 1 on success, 0 on error
 */
static int hash_in_loop(unsigned hash_id, const unsigned char* message, size_t msg_size, int count, unsigned char* out)
{
	int i;
	struct rhash_context *context = rhash_init(hash_id);
	if (!context) return 0;

	/* process the repeated message buffer */
	for (i = 0; i < count; i++) rhash_update(context, message, msg_size);
	rhash_final(context, out);
	rhash_free(context);
	return 1;
}
Beispiel #5
0
/**
 * Calculate hash of the message specified by chunk string, repeated until
 * the given length is reached.
 *
 * @param chunk a null-terminated string representing the chunk
 * @param msg_size the total message length
 * @param length the total length of the message
 * @param hash_id id of the hash algorithm to use
 */
static char* calc_sums_c(const char* chunk, size_t msg_size, size_t length, unsigned hash_id)
{
    struct rhash_context *ctx;
    static char out[130];
    size_t i;

    ctx = rhash_init(hash_id);
    for(i = 0; i < length; i += msg_size) {
        rhash_update(ctx, (const unsigned char*)chunk,
                     ((i + msg_size) <= length ? msg_size : length % msg_size));
    }
    rhash_final(ctx, hash_id, 0);
    rhash_print(out, ctx, hash_id, RHPR_UPPERCASE);
    rhash_free(ctx);
    return out;
}
Beispiel #6
0
/**
 * Test printing of magnet links.
 */
static void test_magnet(void)
{
	unsigned bit;

	rhash ctx = rhash_init(RHASH_ALL_HASHES);
	rhash_update(ctx, "a", 1);
	rhash_final(ctx, 0);

	assert_magnet("magnet:?xl=1&dn=test.txt&xt=urn:tree:tiger:czquwh3iyxbf5l3bgyugzhassmxu647ip2ike4y", ctx, RHASH_TTH, RHPR_FILESIZE | TEST_PATH);
	assert_magnet("magnet:?xl=1&xt=urn:md5:0CC175B9C0F1B6A831C399E269772661", ctx, RHASH_MD5, RHPR_FILESIZE | RHPR_UPPERCASE);
	assert_magnet("xt=urn:ed2k:bde52cb31de33e46245e05fbdbd6fb24&xt=urn:aich:q336in72uwt7zyk5dxolt2xk5i3xmz5y&xt=urn:sha1:q336in72uwt7zyk5dxolt2xk5i3xmz5y&xt=urn:btih:qj6nrgcg7qjs4lth4kocpbggkrb3wtob",
		ctx, RHASH_ED2K | RHASH_AICH | RHASH_SHA1 | RHASH_BTIH, RHPR_NO_MAGNET);

	/* verify length calculation for all hashes */
	for(bit = 1; bit < RHASH_ALL_HASHES; bit <<= 1) {
		assert_magnet(NULL, ctx, bit, RHPR_FILESIZE | RHPR_NO_MAGNET);
	}

	rhash_free(ctx);
}
Beispiel #7
0
/**
 * Calculate hash of the message specified by chunk string, repeated until
 * the given length is reached.
 *
 * @param hash_id id of the hash algorithm to use
 * @param msg_chunk the message chunk as a null-terminated string
 * @param chunk_size the size of the chunk in bytes
 * @param count the number of chunks in the message
 * @param set_filename need to set a filename for BTIH hash
 */
static char* repeat_hash(unsigned hash_id, const char* chunk, size_t chunk_size, size_t msg_size, int set_filename)
{
	struct rhash_context *ctx;
	size_t left, size;
	static char out[130];
	assert(rhash_get_hash_length(hash_id) < 130);

	ctx = rhash_init(hash_id);

	if((hash_id & RHASH_BTIH) && set_filename) {
		unsigned long long total_size = msg_size;
		rhash_transmit(RMSG_BT_ADD_FILE, ctx, RHASH_STR2UPTR("test.txt"), (rhash_uptr_t)&total_size);
	}

	for(left = msg_size; left > 0; left -= size) {
		size = (left > chunk_size ? chunk_size : left);
		rhash_update(ctx, (const unsigned char*)chunk, size);
	}
	rhash_final(ctx, 0);
	rhash_print(out, ctx, hash_id, RHPR_UPPERCASE);
	rhash_free(ctx);
	return out;
}
Beispiel #8
0
/*
 * Class:     org_sf_rhash_Bindings
 * Method:    rhash_free
 * Signature: (J)V
 */
JNIEXPORT void JNICALL Java_org_sf_rhash_Bindings_rhash_1free
(JNIEnv *env, jclass clz, jlong context) {
	rhash_free(TO_RHASH(context));
}