0

I am trying to find a md5-js library which will return raw hash output equivalent of PHP's md5 function. What I'm trying to do here is:

In PHP:

echo base64_encode(md5('city', true)); // returns TtXS6u0aH63MQa0dWO1gPg==
// here setting the second parameter as true means md5 will return the raw out which is something like N������A�X�`>

In javascript I am using two npm packages. One for md5 and another for Base64 encoding. Below are the libraries:

  1. https://www.npmjs.com/package/blueimp-md5
  2. https://www.npmjs.com/package/js-base64n

The JS code line is below:

console.log('city: ', Base64.encode(md5('city', null, true))) // returns TsOVw5LDqsOtGh/CrcOMQcKtHVjDrWA+ 

Why both results are not identical? I'm trying to find a solution in Javascript which will return the same result as php.

  • 5
    Possible duplicate of [identical md5 for JS and PHP](https://stackoverflow.com/questions/10856743/identical-md5-for-js-and-php): `"The important detail here is the character encoding. MD5, like all hashes, works on the binary, which differs between the different encodings"` – paulsm4 Nov 26 '19 at 05:31

1 Answers1

0

In PHP: 1. In PHP we are using Encodes data with MIME base64. 2. MIME (Multipurpose Internet Mail Extensions) base64 is used to encode the string in base64. 3. The base64_encoded data takes 33% more space then original data.

In javascript: 1. Base64 is a group of similar binary-to-text encoding schemes that represent binary data in an ASCII string format by translating it into a radix-64 representation. 2. Each Base64 digit represents exactly 6-bits of data that means 3 bytes can therefore be represented by 4 6-bit Base64 digits 3. We can use btoa() and atob()

John Clinton
  • 204
  • 1
  • 6