java - xenforo登录认证软件
问题描述
我想创建一个第三方登录应用程序,为此我需要将密码的哈希值与用户输入的哈希值相匹配。据我所知 xenforo 使用
sha1(sha1(password) . salt)
or:
sha256(sha256(password) . salt)
但是如何在我的 java 程序中实现相同的加密。我找到了一些答案,例如使用
$password = substr($password, 22, -3);
但这现在不起作用。对于那些想知道 xenforo 使用的加密方法的人,这里是代码
protected $_data = array();
/**
* Hash function to use for generating salts and passwords
*
* @var string
*/
protected $_hashFunc = '';
/**
* Setup the hash function
*/
protected function _setupHash()
{
if ($this->_hashFunc)
{
return;
}
if (extension_loaded('hash'))
{
$this->_hashFunc = 'sha256';
}
else
{
$this->_hashFunc = 'sha1';
}
}
/**
* Perform the hashing based on the function set
*
* @param string
*
* @return string The new hashed string
*/
protected function _createHash($data)
{
$this->_setupHash();
switch ($this->_hashFunc)
{
case 'sha256':
return hash('sha256', $data);
case 'sha1':
return sha1($data);
default:
throw new XenForo_Exception("Unknown hash type");
}
}
protected function _newPassword($password, $salt)
{
$hash = $this->_createHash($this->_createHash($password) . $salt);
return array('hash' => $hash, 'salt' => $salt, 'hashFunc' => $this->_hashFunc);
}
/**
* Initialize data for the authentication object.
*
* @param string Binary data from the database
*/
public function setData($data)
{
$this->_data = unserialize($data);
$this->_hashFunc = $this->_data['hashFunc'];
}
/**
* Generate new authentication data
* @see XenForo_Authentication_Abstract::generate()
*/
public function generate($password)
{
if (!is_string($password) || $password === '')
{
return false;
}
$salt = $this->_createHash(self::generateSalt());
$data = $this->_newPassword($password, $salt);
return serialize($data);
}
/**
* Authenticate against the given password
* @see XenForo_Authentication_Abstract::authenticate()
*/
public function authenticate($userId, $password)
{
if (!is_string($password) || $password === '' || empty($this->_data))
{
return false;
}
$userHash = $this->_createHash($this->_createHash($password) . $this->_data['salt']);
return ($userHash === $this->_data['hash']);
}
解决方案
推荐阅读
- zsh - 单元测试 Zsh 完成脚本
- google-cloud-dns - 内部 DNS 名称无法解析
- firebase - Kotlin,Firebase 数据库关于它的连接
- sql - 一个事务范围内的主键冲突
- python - 使用sql从python中的输入将数据插入数据库
- javascript - 尝试在按钮单击时触发 API.graphql aws-amplify createPost 突变返回错误
- discord - 不和谐投票机器人
- api-design - 具有两个操作的相同资源的最佳 REST 端点是什么?
- git - 我可以在不同计算机之间共享的文件夹中使用 git,还是应该将项目克隆到不同的文件夹中?
- reactjs - 如何使用 ESLint 检查 React/Typescript 项目中的 prop 错误?