Search code examples
node.jsopensslmd5crypt

How to translate hashing with 'openssl passwd -1 -salt $salt $data' to node.js


I am trying to imitate the behaviour of openssl passwd -1 -salt k7Ko8SgF di6cqUaQBuUOd7WhURXDq04022 in nodejs.

The result of this operation is $1$k7Ko8SgF$CN8PPKddtp5gONSRp4B56/ and I know the hash is the 4th section after the third '$'.

If I try:

const crypto = require('crypto');
const newsalt = 'k7Ko8SgF'; 
const key = 'di6cqUaQBuUOd7WhURXDq04022';
let sign = crypto.createHash('md5').update(newsalt + key).digest("base64");
console.log(sign);

I get n0cLvfi4LQFpIVbZ94gCIA==

I am not even sure the digest("base64") is even correct to be honest, as I don't know what exactly does openssl spit out.

I am unable to obtain the same hash string that I get with openssl. I need to get the same hash, as the program with which I am trying to communicate doesn't work unless the hash provided is produced with openssl as specified above.

Maybe someone can point me in the write direction?

Thanks in advance!


Solution

  • Thanks to @Topaco the best way to emulate the behaviour of openssl passwd -1 -salt yoursalt password on NodeJS is using the nano-MD5 library found here Hope this helps someone else looking into this same problem!