2

I've faced with following issue: i try to convert some string str to md5 bytestring hash. In PHP we can use md5(str, true), but in JS (nodejs express) i can't find some way to receive the same result. I've included npm module js-md5, but arrayBuffer method of this module returns another result (differes from PHP md5(str, true)).

Could somebody help me, please.

Thanks

3 Answers3

1
var md5 = require('md5');
console.log(md5('text'))
Mridul
  • 258
  • 3
  • 9
0

Use CryptoJS module : NPM link here

And do something like :

// Requires
var crypto = require('crypto');

// Constructor
function Crypto() {
    this.hash;
}

// Hash method
Crypto.prototype.encode = function(data) {
    this.hash = crypto.createHash('md5').update(data);
    var result = this.hash.digest('hex');
    return result;
};

// Comparison method (return true if === else false)
Crypto.prototype.equals = function(data, model) {
    var bool = false;
    var data = data.toUpperCase();
    var model = String(model).toUpperCase();
    if (data == model){
        bool = true;
    } else {
        bool = false;
    }
    return bool;
};

// Exports
module.exports = Crypto;

Then instantiate this "tool" object in your code and use methods.
Easy as pie, and the same thing can be done with anothers encryption methods like AES, SHA256, etc.
About the raw_output option (binary answer, padded on 16 bits) you can easily convert the returned var in binary format with a simple function, see this SO post to know how. Have fun.

Community
  • 1
  • 1
Aethyn
  • 695
  • 1
  • 4
  • 16
0

Short answer:

const crypto = require('crypto');
const buffer = crypto.createHash('md5').update(str).digest();

Long answer: you need to use NodeJS’ default crypto module (no need for a dependency here), which contains utility function and classes. It is able to create hashes (for instance MD5 or SHA-1 hashes) for you using synchronous or asynchronous methods. A short utility function named crypto.createHash(algorithm) is useful to create a hash with minimal coding. As the docs specifies:

The algorithm is dependent on the available algorithms supported by the version of OpenSSL on the platform. Examples are 'sha256', 'sha512', etc. On recent releases of OpenSSL, openssl list-message-digest-algorithms will display the available digest algorithms.

Now, this createHash function returns a Hash object, which can be used with a stream (you can feed it a file, HTTP request, etc.) or a string, as you asked. If you want to use a string, use hash.update(string) to hash it. This method returns the hash itself, so you can chain it with .digest(encoding) to generate a string (if encoding is set) or a Buffer (if it’s not). Since you asked for bytes, I believe a Buffer is what you want (Buffers are Uint8Array instances).

Iso
  • 3,148
  • 23
  • 31
  • You are absolutely right, I wrote this in a hurry and forgot about it. Please let me edit my answer. – Iso Jul 29 '16 at 12:41
  • As your answer is now much better than the old, I removed my previous comment. Thx ;). Additional informations : don't try to digest a hash two times or you'll have an error. A hash must be digest only once. And if you compare two password for example, take care of the type of the object (probably a string if it comes from an input, but a object attribute if it comes from an object like a database's resultset) that's why I created my "equals" method (inspired by .equals() in Java). – Aethyn Jul 29 '16 at 12:58