user10817219
user10817219

Reputation:

Php-Pack in node js

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

Upvotes: 0

Views: 256

Answers (1)

Petr Zelenka
Petr Zelenka

Reputation: 21

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==

Upvotes: 1

Related Questions