p2 project
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

56 lines
1.2 KiB

<?php
namespace app\utility;
use Hashids\Hashids;
class UnqId
{
/**
* @desc 生成密码
* @param $string
* @param $salt
* @return string
*/
public function encryptPassword($string, $salt): string
{
if (empty($string)) {
return '';
}
$string = $salt.$string.$salt;
return md5(md5($string).$salt);
}
/**
* 校验密码是否正确
* @param $string
* @param $password
* @param $salt
* @return bool
*/
public function checkPassword($string, $password, $salt): bool
{
$encryptPassword = $this->encryptPassword($string, $salt);
return $encryptPassword == $password;
}
// 生成随机字符
public function generateRandomCode($length = 15): string
{
$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
$charLength = strlen($chars);
$code = '';
// 生成足够长度的安全随机数
$randomBytes = random_bytes($length);
for ($i = 0; $i < $length; $i++) {
$code .= $chars[random_int(0, $charLength - 1)];
}
return $code;
}
}