PHP function crypt() in JavaScript(JavaScript 中的 PHP 函数 crypt())
问题描述
在服务器端,我创建了一个密码哈希:
On the server side I create a password hash:
public static function salt()
{
return '$1$' . StringUtil::random(6, array('encode' => StringUtil::ENCODE_BASE_64));
}
public static function hash($password, $salt = null)
{
return crypt($password, $salt ?: static::salt());
}
在客户端,我想使用 CryptoJS 做同样的事情.javascript 中是否有用于 PHP crypt() 的类似物,而 CryptoJS 则不需要?
And on client side I want to do the same using CryptoJS. Is there any analogues in javascript for PHP crypt(), not necessary with CryptoJS?
UPD:我想在客户端执行此操作,因为我不想将密码发送到服务器,但是诸如使用哈希加密的 clientId 之类的东西,在服务器上对其进行解密并获取用于下一次操作的哈希.
UPD: I want to do this on client side because I don't want to send password to server, but something like clientId crypted with hash, decrypt it on the server and get the hash for the next manipulations.
推荐答案
好吧,这里是:一个 PHP 的 crypt 的 CryptoJS 实现对于 MD5 哈希(我想它太大而无法粘贴).所以它不是一个完整的 crypt-like 东西,但在你的代码示例中你正在设置一个基于 MD5 的哈希(带有 $1$
salt 前缀).
Well, here it is: a CryptoJS implementation of PHP's crypt for MD5-hashes (I guess it's too large to paste). So it's not a complete crypt-like thing but in your code example you are setting up a MD5-based hash (with the $1$
salt prefix).
使用方法:
- 存储在名为
php-crypt-md5.js
的文件中 像这样使用它(rollups"在您的 CryptoJS 目录中,只需使用正确的路径):
- Store in a file named
php-crypt-md5.js
Use it like that ("rollups" is in your CryptoJS directory, just use the correct path):
<script src="cm9sbHVwcy9tZDUuanM="></script>
<script src="cGhwLWNyeXB0LW1kNS5qcw=="></script>
<script>
function createSalt(len) {
var saltAlpha = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" +
"abcdefghijklmnopqrstuvwxyz./-+_"
var salt = "JDEk";
for(var i = 0; i < len; ++i) {
salt += saltAlpha.charAt(
Math.floor(Math.random() * saltAlpha.length));
}
return salt;
}
// in your JavaScript code:
var salt = createSalt(8);
var pw = "your password";
var hash = CryptoJS.PHP_CRYPT_MD5(pw, salt);
这篇关于JavaScript 中的 PHP 函数 crypt()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:JavaScript 中的 PHP 函数 crypt()


- 使用 GD 和 libjpeg 支持编译 PHP 2022-01-01
- 覆盖 Magento 社区模块控制器的问题 2022-01-01
- 如何在 Symfony2 中正确使用 webSockets 2021-01-01
- 如何从数据库中获取数据以在 laravel 中查看页面? 2022-01-01
- Laravel 5:Model.php 中的 MassAssignmentException 2021-01-01
- openssl_digest vs hash vs hash_hmac?盐与盐的区别HMAC? 2022-01-01
- PHP foreach() 与数组中的数组? 2022-01-01
- Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01
- PHP - if 语句中的倒序 2021-01-01
- 如何使用 Google API 在团队云端硬盘中创建文件夹? 2022-01-01