* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-class CryptoHelper extends BaseFrameworkSystem {
+class CryptoHelper extends BaseFrameworkSystem implements Cryptable {
/**
* An instance of this own clas
*/
*/
public final static function createCryptoHelper () {
// Get a new instance
- $cryptoInstance = self::getInstance();
+ $cryptoInstance = new CryptoHelper();
// Initialize the hasher
$cryptoInstance->initHasher();
// Is no instance there?
if (is_null(self::$selfInstance)) {
// Then get a new one
- self::$selfInstance = new CryptoHelper();
+ self::$selfInstance = self::createCryptoHelper();
}
// Return the instance
*/
private function generateSalt () {
// Get a random string from the RNG
- $randomString = $this->rngInstance->makeRandomString();
+ $randomString = $this->rngInstance->randomString();
// Get config entry for salt length
$length = $this->getConfigInstance()->readConfig('salt_length');
}
/**
- * Hashes a password with salt and returns the hash. If an old previous hash
+ * Hashes a string with salt and returns the hash. If an old previous hash
* is supplied the method will use the first X chars of that hash for hashing
* the password. This is useful if you want to check if the password is
* identical for authorization purposes.
*
- * @param $plainPassword The plain password to use
- * @param $oldHash A previously hashed password
- * @return $hashed The hashed and salted password
+ * @param $str Unhashed string
+ * @param $oldHash A hash from previous hashed string
+ * @return $hashed The hashed and salted string
*/
- public function hashPassword ($plainPassword, $oldHash = "") {
+ public function hashString ($str, $oldHash = "") {
+ // Cast the string
+ $str = (string) $str;
+
// Is the old password set?
if (empty($oldHash)) {
// No, then use the current salt
}
// Hash the password with salt
- //* DEBUG: */ echo "salt=".$salt."/plain=".$plainPassword."<br />\n";
+ //* DEBUG: */ echo "salt=".$salt."/plain=".$str."<br />\n";
$hashed = $salt . md5(sprintf($this->getConfigInstance()->readConfig('hash_mask'),
$salt,
$this->rngInstance->getFixedSalt(),
- $plainPassword
+ $str
));
// And return it
return $hashed;
}
+
+ /**
+ * Encrypt the string with fixed salt
+ *
+ * @param $str The unencrypted string
+ * @return $encrypted Encrypted string
+ */
+ public function encryptString ($str) {
+ // Init crypto module
+ $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
+ $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
+
+ // Get key
+ $key = md5($this->rngInstance->getFixedSalt());
+
+ // Encrypt the string
+ $encrypted = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $str, MCRYPT_MODE_ECB, $iv);
+
+ // Return the string
+ return $encrypted;
+ }
+
+ /**
+ * Decrypt the string with fixed salt
+ *
+ * @param $encrypted Encrypted string
+ * @return $str The unencrypted string
+ */
+ public function decryptString ($encrypted) {
+ // Init crypto module
+ $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
+ $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
+
+ // Get key
+ $key = md5($this->rngInstance->getFixedSalt());
+
+ // Encrypt the string
+ $str = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $encrypted, MCRYPT_MODE_ECB, $iv);
+
+ // Trim trailing nulls away
+ $str = rtrim($str, "\0");
+
+ // Return the string
+ return $str;
+ }
}
// [EOF]