| 29118 | |
| 29119 | module.exports.create = createIntegrity |
| 29120 | function createIntegrity (opts) { |
| 29121 | opts = opts || {} |
| 29122 | const algorithms = opts.algorithms || ['sha512'] |
| 29123 | const optString = opts.options && opts.options.length |
| 29124 | ? `?${opts.options.join('?')}` |
| 29125 | : '' |
| 29126 | |
| 29127 | const hashes = algorithms.map(crypto.createHash) |
| 29128 | |
| 29129 | return { |
| 29130 | update: function (chunk, enc) { |
| 29131 | hashes.forEach(h => h.update(chunk, enc)) |
| 29132 | return this |
| 29133 | }, |
| 29134 | digest: function (enc) { |
| 29135 | const integrity = algorithms.reduce((acc, algo) => { |
| 29136 | const digest = hashes.shift().digest('base64') |
| 29137 | const hash = new Hash( |
| 29138 | `${algo}-${digest}${optString}`, |
| 29139 | opts |
| 29140 | ) |
| 29141 | if (hash.algorithm && hash.digest) { |
| 29142 | const algo = hash.algorithm |
| 29143 | if (!acc[algo]) { acc[algo] = [] } |
| 29144 | acc[algo].push(hash) |
| 29145 | } |
| 29146 | return acc |
| 29147 | }, new Integrity()) |
| 29148 | |
| 29149 | return integrity |
| 29150 | } |
| 29151 | } |
| 29152 | } |
| 29153 | |
| 29154 | const NODE_HASHES = new Set(crypto.getHashes()) |
| 29155 | |