Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Created May 30, 2023 03:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save code-boxx/68d805b48d612d0be0e36df264a1030f to your computer and use it in GitHub Desktop.
Save code-boxx/68d805b48d612d0be0e36df264a1030f to your computer and use it in GitHub Desktop.
PHP Password Encrypt Decrypt Verify

PHP PASSWORD ENCRYPT DECRYPT VERIFY

https://code-boxx.com/password-encrypt-decrypt-php/

NOTES

  1. Create a dummy database and import 0a-users.sql.
  2. Change the database settings in 0b-database.php to your own.
  3. Walkthrough 1-aaa.php to 4-bbb.php for the different methods.

LICENSE

Copyright by Code Boxx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

CREATE TABLE `users` (
`email` varchar(255) NOT NULL,
`password` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
ALTER TABLE `users`
ADD PRIMARY KEY (`email`);
<?php
class User {
// (A) CONSTRUCTOR - CONNECT TO DATABASE
private $pdo = null;
private $stmt = null;
public $error = null;
function __construct () {
$this->pdo = new PDO(
"mysql:host=".DB_HOST.";dbname=".DB_NAME.";charset=".DB_CHARSET,
DB_USER, DB_PASSWORD, [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
]);
}
// (B) DESTRUCTOR - CLOSE DATABASE CONNECTION
function __destruct () {
if ($this->stmt !== null) { $this->stmt = null; }
if ($this->pdo !== null) { $this->pdo = null; }
}
// (C) HELPER - RUN QUERY
function query ($sql, $data=null) : void {
$this->stmt = $this->pdo->prepare($sql);
$this->stmt->execute($data);
}
// (D) ADD/UPDATE USER
function save ($email, $password) {
$this->query("REPLACE INTO `users` (`email`, `password`) VALUES (?,?)", [$email, $password]);
return true;
}
// (E) GET USER
function get ($email) {
$this->query("SELECT * FROM `users` WHERE `email`=?", [$email]);
return $this->stmt->fetch();
}
}
// (F) DATABASE SETTINGS - CHANGE TO YOUR OWN!
define("DB_HOST", "localhost");
define("DB_NAME", "test");
define("DB_CHARSET", "utf8mb4");
define("DB_USER", "root");
define("DB_PASSWORD", "");
// (G) USER OBJECT
$_USR = new User();
<?php
// (A) USERS LIBRARY + USER + PASSWORD
require "0b-lib.php";
$email = "job@doe.com";
$password = "ABCD1234";
// (B) ENCRYPT PASSWORD
echo $_USR->save($email, password_hash($password, PASSWORD_DEFAULT))
? "OK" : "ERROR" ;
// (C) VERIFY PASSWORD
$user = $_USR->get($email);
echo password_verify($password, $user["password"])
? "VALID" : "INVALID" ;
<?php
// (A) USERS LIBRARY + USER + PASSWORD + SECRET KEY
require "0b-lib.php";
$email = "joe@doe.com";
$password = "ABCD1234";
define ("SECRETKEY", "mysecretkey1234"); // keep in protected config file!
// (B) ENCRYPT PASSWORD
echo $_USR->save($email, openssl_encrypt($password, "AES-128-ECB", SECRETKEY))
? "OK" : "ERROR" ;
// (C) VERIFY PASSWORD
$user = $_USR->get($email);
echo openssl_decrypt($user["password"], "AES-128-ECB", SECRETKEY) == $password
? "VALID" : "INVALID" ;
<?php
// (A) USERS LIBRARY + USER + PASSWORD
require "0b-lib.php";
$email = "jon@doe.com";
$password = "ABCD1234";
// (B) ENCRYPT PASSWORD
// CREDITS : https://www.thecodedeveloper.com/generate-random-alphanumeric-string-with-php/
$salt = substr(base_convert(sha1(uniqid(mt_rand())), 16, 36), 0, 14);
echo $_USR->save($email, crypt($password, $salt))
? "OK" : "ERROR" ;
// (C) VERIFY PASSWORD
$user = $_USR->get($email);
echo hash_equals($user["password"], crypt($password, $user["password"]))
? "VALID" : "INVALID" ;
<?php
// (A) DATABASE LIBRARY + USER + PASSWORD
require "0b-database.php";
$email = "joy@doe.com";
$password = "ABCD1234";
// (B) ENCRYPT PASSWORD
// CREDITS : https://www.thecodedeveloper.com/generate-random-alphanumeric-string-with-php/
$salt = substr(base_convert(sha1(uniqid(mt_rand())), 16, 36), 0, 14);
$hash = $salt . md5($salt . $password);
// $hash = $salt . sha1($salt . $password);
echo $_USR->save($email, $hash)
? "OK" : "ERROR" ;
// (C) VERIFY PASSWORD
$user = $_USR->get($email);
$dbsalt = substr($user["password"], 0, 14);
$dbpass = substr($user["password"], 14);
// echo sha1($dbsalt . $password) == $dbpass
echo md5($dbsalt . $password) == $dbpass
? "VALID" : "INVALID" ;
<?php
// (A) PHP PASSWORD HASH
$start = microtime(true);
$clear = "MyPassw@rd!23";
$hash = password_hash($clear, PASSWORD_DEFAULT);
$endA = microtime(true);
$verified = password_verify("MyPassw@rd!23", $hash);
$endB = microtime(true);
$tenc = $endA - $start;
$tdec = $endB - $endA;
$ted = $tenc + $tdec;
echo "PHP password_hash() + password_verify()<br>";
echo "Time taken to encode = " . $tenc . " sec <br>";
echo "Time taken to verify = " . $tdec . " sec <br>";
echo "Total time taken = " . $ted . " sec <br><br>";
// (B) OPENSSL (AES-128-ECB)
$start = microtime(true);
$clear = "MyPassw@rd!23";
$hash = openssl_encrypt($clear, "AES-128-ECB", "mysecretkey1234");
$endA = microtime(true);
$decrypt = openssl_decrypt($hash, "AES-128-ECB", "mysecretkey1234");
$verified = $decrypt == $clear;
$endB = microtime(true);
$tenc = $endA - $start;
$tdec = $endB - $endA;
$ted = $tenc + $tdec;
echo "OpenSSL 128-bit AES<br>";
echo "Time taken to encode = " . $tenc . " sec <br>";
echo "Time taken to verify = " . $tdec . " sec <br>";
echo "Total time taken = " . $ted . " sec <br><br>";
// (C) CRYPT
$start = microtime(true);
$clear = "MyPassw@rd!23";
$salt = substr(base_convert(sha1(uniqid(mt_rand())), 16, 36), 0, 14);
$hash = crypt($clear, $salt);
$endA = microtime(true);
$verified = hash_equals($hash, crypt($clear, $hash));
$endB = microtime(true);
$tenc = $endA - $start;
$tdec = $endB - $endA;
$ted = $tenc + $tdec;
echo "Salted Crypt + Hash Equals<br>";
echo "Time taken to encode = " . $tenc . " sec <br>";
echo "Time taken to verify = " . $tdec . " sec <br>";
echo "Total time taken = " . $ted . " sec <br><br>";
// (D) SALTY MD5
$start = microtime(true);
$clear = "MyPassw@rd!23";
$salt = substr(base_convert(sha1(uniqid(mt_rand())), 16, 36), 0, 14);
$hash = $salt . md5($salt . $clear);
$endA = microtime(true);
$dbSalt = substr($hash,0,14);
$dbPass = substr($hash,14);
$verified = md5($dbSalt . $clear) == $dbPass;
$endB = microtime(true);
echo "MD5 Salted<br>";
echo "Time taken to encode = " . $tenc . " sec <br>";
echo "Time taken to verify = " . $tdec . " sec <br>";
echo "Total time taken = " . $ted . " sec <br><br>";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment