Skip to content

Instantly share code, notes, and snippets.

@sarciszewski
Last active August 29, 2015 14:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sarciszewski/443f094dac3d0b4de84a to your computer and use it in GitHub Desktop.
Save sarciszewski/443f094dac3d0b4de84a to your computer and use it in GitHub Desktop.
Bcrypt + AES

Use Case

If you encrypt your hashes before you store them in a relational database, this creates another layer of defense against password leaking IF AND ONLY IF the database is on separate hardware from the webserver.

If an attacker can compromise your database, it's very likely they can also compromise your filesystem. "SELECT '<?php reverse_shell_code_here();' INTO OUTFILE /var/www/llehs.php;' and whatnot.

Dependencies

Depends on this:

And one of these two:

In response to http://blog.ircmaxell.com/2015/03/security-issue-combining-bcrypt-with.html

<?php
namespace Sarciszewski\Gists;
/**
* This is a proof of concept code! DO NOT USE THIS!
*/
class PasswordLock
{
const KEY = '4ff556f7b1db2dd95906c21a45ef00344abdb38934450263ee7a908facc05070';
// ^-- SHA-256("Has James Clapper Been Indicted For Perjury Yet?")
/**
* 1. Hash password
* 2. Encrypt-then-MAC the hash
*
* @param string $password
* @return string
*/
public static function hashAndEncrypt($password)
{
return \Crypto::encrypt(
\password_hash($password, PASSWORD_DEFAULT),
\hex2bin(self::KEY)
);
}
/**
* 1. VerifyHMAC-then-Decrypt the ciphertext to get the hash
* 2. Verify that the password matches the hash
*
* @param string $password
* @param string $ciphertext
* @return boolean
*/
public static function decryptAndVerify($password, $ciphertext)
{
$hash = \Crypto::decrypt(
$ciphertext,
\hex2bin(self::KEY)
);
return \password_verify($password, $hash);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment