Last active
August 29, 2015 14:14
-
-
Save benjaminmullard/5cd48f43cd0f870ae8e6 to your computer and use it in GitHub Desktop.
Extension to CI_Encrypt CodeIgniter library
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php if (!defined('BASEPATH')) { exit; } | |
class MY_Encrypt extends CI_Encrypt { | |
public function __construct() { | |
parent::__construct(); | |
} | |
/** | |
* Encrypt a given string using SHA512 encryption. | |
* @param String $str | |
*/ | |
public function encrypt($str, $salt='') { | |
if (empty($salt)) { | |
$salt = $this->generate_sha512_salt(); | |
} | |
return crypt($str, $salt); | |
} | |
/** | |
* Generate a random alpha-numeric string to the given length. | |
* @param int $len | |
* @return string | |
*/ | |
public function generate_random_string($len) { | |
$chr = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; | |
$str = ''; | |
for ($i = 0; $i < $len; $i++) { | |
$str .= $chr[rand(0, strlen($chr) - 1)]; | |
} | |
return $str; | |
} | |
/** | |
* Generate a salt for the SHA512 encryption algorithm. | |
* @return string | |
*/ | |
public function generate_sha512_salt() { | |
return '$6$rounds=10000$' . $this->generate_random_string(16) . '$'; | |
} | |
} | |
/* End of file MY_Encrypt.php */ | |
/* Location: ./application/libraries/MY_Encrypt.php */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This extends the CI_Encrypt class to give seriously strong encryption to user passwords.