sri-calc
v1.1.2
Published
Module to calculate a SRI hash of a file
Downloads
840
Readme
sri-calc
sri-calc
is a simple module to generate SRI hashes of files, which then can be used to implement sub-resource integrity.
This module was inspired by odino/node-sri, but it operates differently:
- it does not require a Linux environment;
- it uses NodeJS Crypto API instead of launching an external process to calculate a digest
Installation
npm install --save sri-calc
Usage
Using the module is pretty straightforward, as you can use it both with callbacks:
const sri = require('sri-calc');
sri.hash('/path/to/my/file.js', (err, hash) => {
if (err) {
throw err;
}
console.log('The hash is', hash);
});
and with promises:
const sri = require('sri-calc');
sri.hash('/path/to/my/file.js')
.then(hash => console.log('The hash is', hash))
.catch(err => console.log(err))
;
Options
The first parameter of sri.hash()
can either be a name of the file to process, or an object with the following configuration options:
hash
: digest to use, the default value issha256
. In theory you can use any digest supported by crypto.createHash, but the specification allows only forsha256
,sha384
, andsha512
.prefix
: iftrue
(default), the name of the digest algorithm will be prepended to the digest value, i.e.,sha512-z4PhNX7vuL3xVChQ1m2AB9Yg5AULVxXcg/SpIdNs6c5H0NE8XYXysP+DGNKHfuwvY7kxvUdBeoGlODJ6+SfaPg==
.file
: name of the file to process
sri.hash({file: '/path/to/my/file.js', algo: 'sha512', prefix: false}) // z4PhNX7vuL3xVChQ1m2AB9Yg5AULVxXcg/SpIdNs6c5H0NE8XYXysP+DGNKHfuwvY7kxvUdBeoGlODJ6+SfaPg==
Tests
Run npm test