Bonesnap;11002952 wrote:Oh, for sure, I'd always use a salt regardless of the algorithm. I was just curious why one would use SHA-256 over SHA-512.
I'm not sure of course, but I think the only reason to use 256 over 512 is for spacial reasons. 512 is 128 characters in length versus 64 for 256. However this seems like a poor reason to choose it IMO, because disk space is stupidly cheap. As far as speed goes, the difference seems almost negligible.
<?php
$times = array();
$times[256] = array();
$times[512] = array();
for( $i=0; $i<1000; $i++ ) {
$stime = microtime(TRUE);
hash('sha256','MySuperSecretPassword');
$times[256][] = microtime(TRUE) - $stime;
$stime = microtime(TRUE);
hash('sha512','MySuperSecretPassword');
$times[512][] = microtime(TRUE) - $stime;
}
printf("<pre>sha256\n\tMax:\t%f\n\tMin:\t%f\n\tMedian:\t%f\n\tAvg:\t%f\n\nsha512\n\tMax:\t%f\n\tMin:\t%f\n\tMedian:\t%f\n\tAvg:\t%f</pre>",
max($times[256]), min($times[256]), median($times[256]), (array_sum($times[256])/1000),
max($times[512]), min($times[512]), median($times[512]), (array_sum($times[512])/1000) );
function median(Array $arr) {
sort($arr);
$cnt = count($arr);
if( $cnt%2 != 0 ) {
return $arr[floor($cnt/2)];
} else {
return ( ( $arr[$cnt/2] + $arr[($cnt/2)-1] ) / 2 );
}
}
sha256
Max: 0.000023
Min: 0.000004
Median: 0.000006
Avg: 0.000006
sha512
Max: 0.000033
Min: 0.000004
Median: 0.000007
Avg: 0.000007