【问题标题】:Php-Pack in node js节点 js 中的 PHP-Pack
【发布时间】:2020-05-04 07:28:27
【问题描述】:

我目前在 Nodejs 中使用 PHP-PACK

https://www.npmjs.com/package/php-pack

但不幸的是,它在 windows 上不起作用

那么有谁知道 js 代码具有与

相同的功能
pack('H*', md5('password')

我尝试了此参考中的此代码,但无助于解决问题

PHP Pack/Unpack implementation in Javascript Mismatch

                  String.prototype.packHex = function () {
                var source = this.length % 2 ? this + '0' : this
                    var result = ''

                for (var i = 0; i < source.length; i = i + 3) {
                    result += String.fromCharCode(parseInt(source.substr(i, 2), 16))
                }

                return result
              }
            console.log('Pack-hex: --->', Buffer.from(md5('password').packHex()))
            console.log('php-pack: --->', Buffer.from(pack('H*', md5('password'))))

结果:

        Pack-hex: ---> X8OcO8KqZWHCg33CuCzCmQ==
        php-pack: ---> X03MO1qnZdYdgyfeuILPmQ==

感谢您的帮助

【问题讨论】:

    标签: javascript node.js hash


    【解决方案1】:

    这给出了相同的结果

    const md5 = require("md5");
    String.prototype.packHex = function() {
        var source = this.length % 2 ? this + '0' : this
            ,result = '';
        for( var i = 0; i < source.length; i = i + 2 ) {
            result += String.fromCharCode( parseInt( source.substr( i , 2 ) ,16 ) );
        }
        return result;
    }
    console.log(Buffer.from(md5("password").packHex(),"ascii").toString("base64"));
    result : X03MO1qnZdYdgyfeuILPmQ==
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-01-27
      • 2018-06-13
      • 2014-04-26
      • 2015-04-17
      • 2011-04-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多