crypto.createHash(algorithm)
Creates and returns a Hash
object that can be used to generate hash digests using the given algorithm
.
The algorithm
is dependent on the available algorithms supported by the version of OpenSSL on the platform. Examples are 'sha256'
, 'sha512'
, etc. On recent releases of OpenSSL, openssl list-message-digest-algorithms
will display the available digest algorithms.
Example: generating the sha256 sum of a file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | const filename = process.argv[2]; const crypto = require( 'crypto' ); const fs = require( 'fs' ); const hash = crypto.createHash( 'sha256' ); const input = fs.createReadStream(filename); input.on( 'readable' , () => { var data = input.read(); if (data) hash.update(data); else { console.log(`${hash.digest( 'hex' )} ${filename}`); } }); |
Please login to continue.