Reputation: 308
I have what is probably a very simple question, but seeing as I am a beginner I cant seem to find an answer. I want to print my hash to a website (to confirm that it gets hashed properly). But when i echo it, I dont get anything back. What I am expecting is a hashed string. Here is the code, please help:
<?php
$index = 1;
$data = rand(1,4);
$myhash = hash($index, $data);
?>
<html>
<body>
<?php
echo "$index <br>";
echo "$data <br>";
echo "$myhash <br>";
?>
</body>
</html>
Upvotes: 0
Views: 1996
Reputation: 1
hash function requires hashing algorithm as 1st argument.
string hash(string $algo , string $data [, bool $raw_output = FALSE ])
You are not proving a hashing algorithm. Try putting md5, sha256, haval160 etc as hashing algorithm . check php.net for more info. php.net
Upvotes: 0
Reputation: 2675
A good practice is to debug your code.
Use this at the start of your page (for development purpose only, not production!):
<?php
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
$index = 1;
$data = rand(1,4);
$myhash = hash($index, $data);
?>
<html>
<body>
<?php
echo "$index <br>";
echo "$data <br>";
echo "$myhash <br>";
?>
</body>
</html>
So you'll get an error like:
E_WARNING : type 2 -- hash(): Unknown hashing algorithm: 1 -- at line 5
It's because, first argument of hash() function must be a name of a hash algorithm, like "md5", "sha256", "haval160,4".
So in your case, you should change $index by something like:
$index = "sha256";
and not a number like you did.
Upvotes: 4
Reputation: 2833
The hash
function needs a hashing algorithm specified as its first argument. You are providing the number 1 which is not a valid algorithm.
Read up on the hash function and choose the appropriate algorithm. For example:
$myhash = hash('sha256', $data);
Upvotes: 2