0

I'm currently using PHP-PACK in Nodejs

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

But unfortunately, it does not work on windows

so does anyone know a js code has the same functionality as

pack('H*', md5('password')

I tried this code from this reference but doesn't help resolve the issue

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

result:

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

Thanks for your help

1 Answer 1

1

This gives same result

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==
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.