pinoyprogammer
Techie
In this tutorial, I will be teaching you how to use PHP hash() function and encrypt your password.
So why encrypt your password?
Encrypting passwords will protect user accounts from attacks that came from inside the organization. This way people who have access to the database won’t know the user’s password. Other sensitive data should be encrypted as well.
Also, if someone steals your database, and uses the usernames, email addresses, and plain text passwords you have stored, they can log in to use email accounts or even bank accounts!
Example
You may also use the following functions!
So why encrypt your password?
Encrypting passwords will protect user accounts from attacks that came from inside the organization. This way people who have access to the database won’t know the user’s password. Other sensitive data should be encrypted as well.
Also, if someone steals your database, and uses the usernames, email addresses, and plain text passwords you have stored, they can log in to use email accounts or even bank accounts!
PHP:
<?php
echo hash(‘hash alogorithm’, ‘text to encrypt’);
?>
Example
PHP:
<?php
echo hash(‘sha256’, ‘The quick brown fox jumped over the lazy dog.’);
//outputs: 68b1282b91de2c054c36629cb8dd447f12f096d3e3c587978dc2248444633483
?>
You may also use the following functions!
- hash_file() – Generate a hash value using the contents of a given file
- hash_hmac() – Generate a keyed hash value using the HMAC method
- hash_init() – Initialize an incremental hashing context
- md5() – Calculate the md5 hash of a string
- sha1() – Calculate the sha1 hash of a string
HTML:
<!DOCTYPE html>
<html>
<head>
<title>Hash Algorithms</title>
<style>
table {
font-family: arial, sans-serif;
border-collapse: collapse;
width: 100%;
}
td, th {
border: 1px solid #dddddd;
text-align: left;
padding: 8px;
}
tr:nth-child(even) {
background-color: #dddddd;
}
</style>
</head>
<body>
<form method="GET" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF'])?>">
<label>Enter your message / password: </label>
<input type="text" name="password" value="<?php if(isset($_GET['password'])) echo $_GET['password']?>">
<input type="submit" name="submit" value="Show Table">
</form>
<table border=1>
<tr>
<th>Hash Alogorithm</th>
<th>Lenght</th>
<th>Output</th>
</tr>
<?php
if(isset($_GET['password'])){
$data = htmlspecialchars($_GET['password']);
foreach (hash_algos() as $v) {
$r = hash($v, $data, false);
echo "
<tr>
<td> $v </td>
<td>".strlen($r)."</td>
<td> $r </td>
</tr>
";
}
}
?>
</table>
</body>
</html>