Reputation: 1298
This is the solution: fix it by converting the MD5 hex to base64 instead of the string.
Example for JS (Node.js or similar, browserify, etc.
new Buffer(md5, 'hex').toString('base64')
I just spent few minutes to figure this out so I thought I could share it. :)
Upvotes: 16
Views: 11146
Reputation: 76
For anyone looking for code examples or alternatives, here are three different solutions using built-in and external libs.
NodeJS built-in:
const crypto = require("crypto");
const hash = crypto.createHash("md5").update(content).digest("base64");
Using md5 lib:
const md5 = require("md5");
const hash = Buffer.from(md5(content), "hex").toString("base64");
or
const md5 = require("md5");
const md5Bytes = md5(content, {
encoding: "binary",
asBytes: true,
});
const hash = Buffer.from(md5Bytes).toString("base64");
The
content
variable may be astring
or atyped array
(e.g.:Buffer
,Uint8Array
,Int32Array
...).
Upvotes: 1
Reputation: 109
I stumbled upon this question when implementing blob upload to AWS S3 from the browser. While the chosen answer helped me understand that the Content-MD5
needs to be a base64 string, I didn't realize that the library I used - md5
was not producing MD5 in the base64 format.
I scratched my head to understand why the MD5 was invalid because it was the same value as the Etag shown on the Amazon S3 console when I manually upload it there and it turns out the ETag was also not base64.
Then I tried another library called js-md5
that has an explicit base64
function to produce MD5 in the base64 format and it went through.
So if you are doing the same thing from Javascript, using the js-md5
library will help you solve your problem.
Upvotes: 2
Reputation: 1428
Content-MD5
is always a base64 header.
The base64-encoded 128-bit MD5 digest of the message (without the headers) according to RFC 1864. This header can be used as a message integrity check to verify that the data is the same data that was originally sent. Although it is optional, we recommend using the Content-MD5 mechanism as an end-to-end integrity check. For more information about REST request authentication, go to REST Authentication in the Amazon Simple Storage Service Developer Guide Type: String Default: None Constraints: None
Upvotes: 10