Skip to content
This repository has been archived by the owner on Sep 25, 2021. It is now read-only.

Binary data hash error #36

Open
qilx opened this issue Sep 3, 2020 · 3 comments
Open

Binary data hash error #36

qilx opened this issue Sep 3, 2020 · 3 comments

Comments

@qilx
Copy link

qilx commented Sep 3, 2020

I am trying to migrate from node-forge to blueimp-mp5 + web crypto api.
I created test script:

` var hex = 'B955EF8C83544AD74F803321866F005A';

var bytesStr = forge.util.hexToBytes(hex);

var data = bytesStr;
var h1 = forge.md.md5.create().start().update(data).digest().getBytes();
var h2 = md5(data, null, true);

console.log("H1: ", h1);
console.log("H2: ", h2);`

If 'data' is normal string, everything seems to work fine. But if 'data' is binary (like in example), hashes are different.
Am I doing something wrong or is it bug please?

@blueimp
Copy link
Owner

blueimp commented Sep 6, 2020

The input to the md5 function is always expected to be a string:
https://github.com/blueimp/JavaScript-MD5/blob/master/README.md#api

This project was developed as a cross-browser MD5 library that also works in NodeJS and was built before ArrayBuffer was widely available.

If you need support for non-string inputs, you probably want to use a different implementation.

@qilx
Copy link
Author

qilx commented Sep 11, 2020

Here is example rewritten to use 'atob' to get input data. This function returns string.

    var data = atob('uVXvjINUStdPgDMhhm8AWg==');
    console.log("Data: ", data);
    var h1 = forge.md.md5.create().start().update(data).digest().toHex();
    var h2 = md5(data);

    console.log("H1: ", h1);
    console.log("H2: ", h2);

    var b1 = forge.md.md5.create().start().update(data).digest().getBytes();
    var b2 = md5(data, null, true);

    console.log("B1: ", b1);
    console.log("B2: ", b2);

@BinBashBanana
Copy link

You could use something like,

function btou(data) {
	var s = "";
	for (var i = 0; i < data.length; i++) {
		s += String.fromCharCode(data[i]);
	}
	return s;
}

Pass a Uint8Array to btou, and it will return a UTF-8 string.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants