public function testMcrypt() { $crypt = new Crypt\Mcrypt(); $crypt->setSalt('Test Salt'); $this->assertEquals('Test Salt', $crypt->getSalt()); $this->assertEquals(MCRYPT_RIJNDAEL_256, $crypt->getCipher()); $this->assertEquals(MCRYPT_MODE_CBC, $crypt->getMode()); $this->assertEquals(MCRYPT_RAND, $crypt->getSource()); $hash = $crypt->create('12password34'); $this->assertTrue($crypt->verify('12password34', $hash)); $this->assertNotNull($crypt->getIv()); $this->assertNotNull($crypt->getIvSize()); $this->assertEquals('12password34', $crypt->decrypt($hash)); }
/** * Static method decrypt a field value * * @param string $value * @param int $encryption * @param array $options * @return string */ public static function decrypt($value, $encryption, $options = array()) { $decValue = $value; $salt = !empty($options['salt']) ? $options['salt'] : null; // Decrypt the value switch ($encryption) { case Auth::ENCRYPT_NONE: $decValue = $value; break; case Auth::ENCRYPT_MCRYPT: $crypt = new Crypt\Mcrypt(); $crypt->setSalt($salt); // Set cipher, mode and source, if applicable if (!empty($options['cipher'])) { $crypt->setCipher($options['cipher']); } if (!empty($options['mode'])) { $crypt->setMode($options['mode']); } if (!empty($options['source'])) { $crypt->setSource($options['source']); } $decValue = $crypt->decrypt($value); break; default: $decValue = '[Encrypted]'; } return $decValue; }
<?php require_once '../../bootstrap.php'; use Pop\Crypt; try { $mc = new Crypt\Mcrypt(); $hash = $mc->create('12password34'); echo 'Hash: ' . $hash . '<br/ >'; echo 'Salt: ' . $mc->getSalt() . '<br />'; echo 'Decrypted: ' . $mc->decrypt($hash) . '<br />'; if ($mc->verify('12password34', $hash)) { echo 'Verified!<br />'; } else { echo 'NOT Verified!<br />'; } } catch (\Exception $e) { echo $e->getMessage(); }