glacier.js (3558B)
1 var AWS = require('../core'); 2 3 AWS.util.update(AWS.Glacier.prototype, { 4 /** 5 * @api private 6 */ 7 setupRequestListeners: function setupRequestListeners(request) { 8 if (Array.isArray(request._events.validate)) { 9 request._events.validate.unshift(this.validateAccountId); 10 } else { 11 request.on('validate', this.validateAccountId); 12 } 13 request.removeListener('afterBuild', 14 AWS.EventListeners.Core.COMPUTE_SHA256); 15 request.on('build', this.addGlacierApiVersion); 16 request.on('build', this.addTreeHashHeaders); 17 }, 18 19 /** 20 * @api private 21 */ 22 validateAccountId: function validateAccountId(request) { 23 if (request.params.accountId !== undefined) return; 24 request.params = AWS.util.copy(request.params); 25 request.params.accountId = '-'; 26 }, 27 28 /** 29 * @api private 30 */ 31 addGlacierApiVersion: function addGlacierApiVersion(request) { 32 var version = request.service.api.apiVersion; 33 request.httpRequest.headers['x-amz-glacier-version'] = version; 34 }, 35 36 /** 37 * @api private 38 */ 39 addTreeHashHeaders: function addTreeHashHeaders(request) { 40 if (request.params.body === undefined) return; 41 42 var hashes = request.service.computeChecksums(request.params.body); 43 request.httpRequest.headers['X-Amz-Content-Sha256'] = hashes.linearHash; 44 45 if (!request.httpRequest.headers['x-amz-sha256-tree-hash']) { 46 request.httpRequest.headers['x-amz-sha256-tree-hash'] = hashes.treeHash; 47 } 48 }, 49 50 /** 51 * @!group Computing Checksums 52 */ 53 54 /** 55 * Computes the SHA-256 linear and tree hash checksums for a given 56 * block of Buffer data. Pass the tree hash of the computed checksums 57 * as the checksum input to the {completeMultipartUpload} when performing 58 * a multi-part upload. 59 * 60 * @example Calculate checksum of 5.5MB data chunk 61 * var glacier = new AWS.Glacier(); 62 * var data = new Buffer(5.5 * 1024 * 1024); 63 * data.fill('0'); // fill with zeros 64 * var results = glacier.computeChecksums(data); 65 * // Result: { linearHash: '68aff0c5a9...', treeHash: '154e26c78f...' } 66 * @param data [Buffer, String] data to calculate the checksum for 67 * @return [map<linearHash:String,treeHash:String>] a map containing 68 * the linearHash and treeHash properties representing hex based digests 69 * of the respective checksums. 70 * @see completeMultipartUpload 71 */ 72 computeChecksums: function computeChecksums(data) { 73 if (!AWS.util.Buffer.isBuffer(data)) data = new AWS.util.Buffer(data); 74 75 var mb = 1024 * 1024; 76 var hashes = []; 77 var hash = AWS.util.crypto.createHash('sha256'); 78 79 // build leaf nodes in 1mb chunks 80 for (var i = 0; i < data.length; i += mb) { 81 var chunk = data.slice(i, Math.min(i + mb, data.length)); 82 hash.update(chunk); 83 hashes.push(AWS.util.crypto.sha256(chunk)); 84 } 85 86 return { 87 linearHash: hash.digest('hex'), 88 treeHash: this.buildHashTree(hashes) 89 }; 90 }, 91 92 /** 93 * @api private 94 */ 95 buildHashTree: function buildHashTree(hashes) { 96 // merge leaf nodes 97 while (hashes.length > 1) { 98 var tmpHashes = []; 99 for (var i = 0; i < hashes.length; i += 2) { 100 if (hashes[i + 1]) { 101 var tmpHash = new AWS.util.Buffer(64); 102 tmpHash.write(hashes[i], 0, 32, 'binary'); 103 tmpHash.write(hashes[i + 1], 32, 32, 'binary'); 104 tmpHashes.push(AWS.util.crypto.sha256(tmpHash)); 105 } else { 106 tmpHashes.push(hashes[i]); 107 } 108 } 109 hashes = tmpHashes; 110 } 111 112 return AWS.util.crypto.toHex(hashes[0]); 113 } 114 });