問題描述
在服務(wù)器端,我創(chuàng)建了一個密碼哈希:
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:我想在客戶端執(zhí)行此操作,因?yàn)槲也幌雽⒚艽a發(fā)送到服務(wù)器,但是諸如使用哈希加密的 clientId 之類的東西,在服務(wù)器上對其進(jìn)行解密并獲取用于下一次操作的哈希.
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 實(shí)現(xiàn)對于 MD5 哈希(我想它太大而無法粘貼).所以它不是一個完整的 crypt-like 東西,但在你的代碼示例中你正在設(shè)置一個基于 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="rollups/md5.js"></script>
<script src="php-crypt-md5.js"></script>
<script>
function createSalt(len) {
var saltAlpha = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" +
"abcdefghijklmnopqrstuvwxyz./-+_"
var salt = '$1$';
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);
這篇關(guān)于JavaScript 中的 PHP 函數(shù) crypt()的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!