<?php

if ( isset( $argv[2] ) ) {
	$complexity = intval( $argv[2] );
} else {
	$complexity = 7;
}

if ( isset( $argv[3] ) ) {
	$salt = $argv[3];
} else {
	$salt = false;
}

$t = -microtime( true );
print proposedHash( $argv[1], $complexity, $salt ) . "\n";
$t += microtime( true );
printf( "%3.3f ms\n", $t * 1000 );


function proposedHash( $pw, $complexity, $salt = false ) {
	if ( $salt === false ) {
		for ( $i = 0; $i < 4; $i++ ) {
			$salt .= base_convert( mt_rand( 0, 0xffff ), 10, 16 );
		}
	}
	$bHash = md5( $salt . '-' . md5( $pw ) );

	$iter = pow( 2, $complexity );

	$h = $bHash;
	for ( $i = 0; $i < $iter; $i++ ) {
		$h = hash( 'whirlpool', str_repeat( $h . $salt, 100 ), true );
		$h = substr( $h, 7, 32 );
	}
	return sprintf( ":C:%03d:%s:%s\n", $complexity, $salt, bin2hex( $h ) );
}