JavaScript中的函数crypt() [英] PHP function crypt() in JavaScript

查看:131
本文介绍了JavaScript中的函数crypt()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在服务器端,我创建了一个密码哈希:
$ b $ pre $ public static function salt()
{
返回'$ 1 $'。 StringUtil :: random(6,array('encode'=> StringUtil :: ENCODE_BASE_64));

$ b $ public static hash($ password,$ salt = null)
{
return crypt($ password,$ salt?:static :: salt ));
}

在客户端,我想用CryptoJS做同样的事情。
在javascript中有没有类似的东西用于PHP crypt(),而CryptoJS不需要?



UPD:
I想在客户端执行此操作,因为我不想将密码发送到服务器,但是像clientId这样用hash加密,在服务器上解密并获得下一个操作的哈希值。
<解决方案

CryptoJS实现PHP的crypt MD5哈希值(我猜这太大了,无法粘贴)。所以它不是一个完整的类地方的东西,但在你的代码示例中,你正在设置一个基于MD5的散列(使用 $ 1 $ 盐前缀)。



如何使用它:


  1. 存储在名为 php- crypt-md5.js

  2. 像这样使用它(rollups位于CryptoJS目录中,只需使用正确的路径):

     < script src =rollups / md5.js>< / script> 
    < script src =php-crypt-md5.js>< / script>

    < script>
    函数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) );
    }

    返回salt;
    }

    //在您的JavaScript代码中:

    var salt = createSalt(8);
    var pw =您的密码;

    var hash = CryptoJS.PHP_CRYPT_MD5(pw,salt);



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());
}

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: 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.

解决方案

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).

How to use it:

  1. Store in a file named php-crypt-md5.js
  2. 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);
    

这篇关于JavaScript中的函数crypt()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆