2017-02-28 80 views
-1

保存隐窝安全散列:如何解密哈希安全尔康

$usuario = new User(); 
$usuario->password = $this->security->hash($this->request->getPost("pass")); 
if (!$usuario->save()) { 
    foreach ($usuario->getMessages() as $message) { 
    $this->flash->error($message); 
    } 
    $this->dispatcher->forward([ 
     'controller' => "usuario", 
     'action' => 'new' 
    ]); 
    return; 
} 

现在,如何解密安全散列发送我的表格:

$usuario = User::findFirstByid($iduser); 
$this->tag->setDefault("pass", $this->encryption->decrypt($usuario->password)); 

我有这样的:通知:访问未定义财产加密...

+0

您无法解密哈希值。按定义散列只是单向的。此外,我没有像phalcon加密这样的服务。 – Juri

回答

0

由于@Juri说哈希只是一种方式。但是,您可以使用两种方式的encryptBase64(),并且可以将其解码。 不要使用它作为密码,使用它的一些数据,你需要传递给别人,并读回,像API令牌等。

下面是如何设置它:

$di->setShared('crypt', function() use ($config) { 
    $crypt = new \Phalcon\Crypt(); 
    $crypt->setKey('i$4^&/:%[email protected]<@{(e=*!<7u|rI~0'); 
    return $crypt; 
}); 

我创建的帮助函数,但您可以直接使用它:

... 
... 

private static $cryptKey = 'i$4^&/:%[email protected]<@{(e=*!<7u|rI~0'; 

/** 
* Generate url safe encoded string 
* 
* @param string $string string to be encoded 
* @return encoded string 
*/ 
public static function encodeString($string) 
{ 
    $crypt = new \Phalcon\Crypt(); 
    return $crypt->encryptBase64($string, self::$cryptKey, true); 
} 

/** 
* Decode string generated with Common::encodeString() 
* 
* @param string $string Encoded string 
* @return decoded string 
*/ 
public static function decodeString($string) 
{ 
    $crypt = new \Phalcon\Crypt(); 
    return $crypt->decryptBase64($string, self::$cryptKey, true); 
}