yii2 密码和加解密
生成hash
$hash = Yii::$app->getSecurity()->generatePasswordHash($password);
验证hash
if (Yii::$app->getSecurity()->validatePassword($password, $hash)) {
// all good, logging user in
} else {
// wrong password
}
生成随机字符串
$key = Yii::$app->getSecurity()->generateRandomString();
var_dump($key);
加密
$secretKey = 'q3TFrVdjc2CQJPHlSJtY3Sd1QEX37ZUR';
$data = [
'name'=>'huyongjian',
'password'=>'123456'
];
$encryptedData = Yii::$app->getSecurity()->encryptByPassword(json_encode($data), $secretKey);
var_dump($encryptedData);
解密
$data = Yii::$app->getSecurity()->decryptByPassword($encryptedData, $secretKey);
var_dump($data);
echo "<pre>";
var_dump(json_decode($data,true));
数据传输前添加hash
$secretKey = 'q3TFrVdjc2CQJPHlSJtY3Sd1QEX37ZUR';
$data = [
'name'=>'huyongjian',
'password'=>'123456'
];
$data = Yii::$app->getSecurity()->hashData(json_encode($data), $secretKey);
var_dump($data);
校验数据Hash完整性
$data = 'ce8ea3740e7859f0f3e1e4a30d5e0b4be01c6d1cc7eae5c9a24c3186772c381e{"name":"huyongjian","password":"123456"}';
$data = Yii::$app->getSecurity()->validateData($data, $secretKey);
var_dump(json_decode($data,true));
标签:getSecurity,dump,Yii,app,加解密,secretKey,密码,yii2,data
From: https://www.cnblogs.com/hu308830232/p/18105475