node.js - NodeJS pre-signed URLs for Secure S3 Downloads - Help w/simple spec -
looking review of flow can pinpoint signing calculation going wrong.
the main issue in generating signing key , signature--the hashing of canonical request seems match reference here http://docs.aws.amazon.com/amazons3/latest/api/sigv4-query-string-auth.html
the sha256hmac , getsignature functions this:
function sha256hmac(key, string, encoding = 'hex') { return crypto.createhmac('sha256', key).update(string, 'utf8').digest(encoding); }; function getsignature(stringtosign, signingdates) { const datekey = sha256hmac('aws4' + s3_secret_key, signingdates.shortdate); const dateregionkey = sha256hmac(datekey, s3_region); const dateregionservicekey = sha256hmac(dateregionkey, s3_service); const signingkey = sha256hmac(dateregionservicekey, s3_request_type); const signature = sha256hmac(signingkey, stringtosign); return signature; }
thanks looking!
the fix:
// remove default hex encoding function sha256hmac(key, string, encoding) { return crypto.createhmac('sha256', key).update(string, 'utf8').digest(encoding); }; function getsignature(stringtosign, signingdates) { // not supply encoding argument sha256hmac() // each of these return buffer const datekey = sha256hmac('aws4' + s3_secret_key, signingdates.shortdate); const dateregionkey = sha256hmac(datekey, s3_region); const dateregionservicekey = sha256hmac(dateregionkey, s3_service); const signingkey = sha256hmac(dateregionservicekey, s3_request_type); // output hex encoding here const signature = sha256hmac(signingkey, stringtosign, 'hex'); return signature; }
did not grok https://nodejs.org/api/crypto.html#crypto_hmac_digest_encoding
calculates hmac digest of of data passed using hmac.update(). encoding can 'hex', 'latin1' or 'base64'. if encoding provided string returned; otherwise buffer returned;
the hmac object can not used again after hmac.digest() has been called. multiple calls hmac.digest() result in error being thrown.
wiki
Comments
Post a Comment