Files
documentation/node_modules/react-loadable-ssr-addon-v5-slorber/source/utils/computeIntegrity.js
2024-03-22 03:47:51 +05:30

30 lines
833 B
JavaScript

/**
* react-loadable-ssr-addon
* @author Marcos Gonçalves <contact@themgoncalves.com>
* @version 1.0.1
*/
import crypto from 'crypto';
/**
* Compute SRI Integrity
* @func computeIntegrity
* See {@link https://developer.mozilla.org/en-US/docs/Web/Security/Subresource_Integrity Subresource Integrity} at MDN
* @param {array} algorithms - The algorithms you want to use when hashing `content`
* @param {string} source - File contents you want to hash
* @return {string} SRI hash
*/
function computeIntegrity(algorithms, source) {
return Array.isArray(algorithms)
? algorithms.map((algorithm) => {
const hash = crypto
.createHash(algorithm)
.update(source, 'utf8')
.digest('base64');
return `${algorithm}-${hash}`;
}).join(' ')
: '';
}
export default computeIntegrity;