Posts Tagged ‘function’

eD2k hash generation in PHP

Tuesday, January 12th, 2010

Spent some time last night fixing a function I wrote ages ago to generate an ed2k hash for a given file. I then failed to test and fix it with regard to files larger than the chunk size (9,728,000 bytes).

Here’s an updated version:

function ed2kHash_file ($name) {
	# Calculates eDonkey2000 hash for any given file
	# By: Tom Higginson
	# Date: 9th January 2008
	# Modified: 11th January 2010
	# License: Public domain

	$chunkSize = 9728000;
	$md4str = '';

	if( !file_exists( $name ) ) {
		return false;
	}

	if( filesize( $name ) < $chunkSize ) {
		return hash_file( 'md4', $name );
	} else {
		$fH = fopen( $name, 'r' );
		while( !feof( $fH ) ) {
			$md4str .= hash( 'md4', fread( $fH, $chunkSize ), true );
		}

		fclose( $fH );

		return hash( 'md4', $md4str );
	}
}

Here's some more about the eD2k hash: http://en.wikipedia.org/wiki/Ed2k_URI_scheme

Edit: I've turned off comments for this post due to a huge amount of spam.