Skip to content

Instantly share code, notes, and snippets.

@benjaminmullard
Last active August 29, 2015 14:14
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 benjaminmullard/5cd48f43cd0f870ae8e6 to your computer and use it in GitHub Desktop.
Save benjaminmullard/5cd48f43cd0f870ae8e6 to your computer and use it in GitHub Desktop.
Extension to CI_Encrypt CodeIgniter library
<?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 */
@benjaminmullard
Copy link
Author

This extends the CI_Encrypt class to give seriously strong encryption to user passwords.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment