views:

90

answers:

1

Can someone tell me in really slow terms the difference between these 2 lines of PHP?

$hassh =  base64_encode(sha1($word));

$hassh =  hash(’sha1′, $word);
+8  A: 

Not sure what you mean by "different", but the first line :

$hassh =  base64_encode(sha1($word));
var_dump($hassh);

gets you :

string 'YWFmNGM2MWRkY2M1ZThhMmRhYmVkZTBmM2I0ODJjZDlhZWE5NDM0ZA==' (length=56)

Where the second :

$hassh =  hash('sha1', $word);
var_dump($hassh);

Gets you :

string 'aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d' (length=40)


So, first of all, I am not sure you meant to use base64_encode : doesn't seem to be really usefull here, and probably isn't necessary : sha1 already returns a string :

$word = 'hello';
var_dump(sha1($word));

Gets you :

string 'aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d' (length=40)

Excepts for this, those two lines, with the sha1 algorithm, get the same thing. Difference probably is that hash can work with a lot of hashing algorithms.

Oh, and, also :

  • sha1 exists since PHP 4
  • hash only exists since PHP >= 5.1.2
Pascal MARTIN
Thanks Pascal, I had suspicions about the code too, because afaik base encoding is used to go from binary to chars, so used more with encryption. Were your concerns similar?
Chris
You're welcome :-) Not sure what you mean, but, by default, sha1 already returns a string (if the second parameter is not set to true) ; so, no need for base64 encoding
Pascal MARTIN
Sounds good, you got me sorted out already.
Chris