What encoding should I use to correctly generate ETag with crypto in nodeJS?

In my nodeJS application, I would like to generate ETags for all the content that I return to the client. I need the ETag to be based on the actual contents of the file instead of the date, so that the same file in different node processes has the same ETag.

Now I am doing the following:

var fs = require('fs'), crypto = require('crypto'); fs.readFile(pathToFile, function(err, buf){ var eTag = crypto.createHash('md5').update(buf).digest('hex'); res.writeHead(200, {'ETag': '"' + eTag + '"','Content-Type':contentType}); res.end(buf); }); 

I'm not sure which encodings I should use for different cryptographic functions in order to have a proper system. Should I use something other than hex ? Should I get a fs.readFile call to return a hex encoded buffer? If so, will this affect the content returned to users?

Best and thanks,
Themselves

+5
source share
1 answer

You are doing everything well. There is no reason to encode a file in any special format, and using hex for output is pretty standard. Fluent requirements:

  • the same document must always return the same ETag
  • any changes to the document lead to a change in the ETag
  • ETag data should fit neatly into the HTTP header
+7
source

Source: https://habr.com/ru/post/956772/


All Articles