Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 413 Vote(s) - 3.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Obtaining the hash of a file using the stream capabilities of crypto module (ie: without hash.update and hash.digest)

#1
The [`crypto`](

[To see links please register here]

) module of node.js (at the time of this writing at least) is not still deemed stable and so the API may change. In fact, the methods that everyone in the internet use to get the hash (md5, sha1, ...) of a file are considered legacy (from the documentation of [`Hash` class](

[To see links please register here]

)) (note: emphasis mine):

> Class: Hash
>
> The class for creating hash digests of data.
>
> It is a stream that is both readable and writable. The written data is
> used to compute the hash. Once the writable side of the stream is
> ended, use the read() method to get the computed hash digest. The
> **legacy update and digest methods** are also supported.
>
> Returned by crypto.createHash.

Despite `hash.update` and `hash.digest` being considered legacy, the example shown just above the quoted snippet is using them.

What's the correct way of obtaining hashes without using those legacy methods?
Reply

#2
From the quoted snippet in the question:

> [the Hash class] It is a stream that is both readable and writable. The written data is
> used to compute the hash. Once the writable side of the stream is
> ended, use the read() method to get the computed hash digest.

So what you need to hash some text is:

var crypto = require('crypto');

// change to 'md5' if you want an MD5 hash
var hash = crypto.createHash('sha1');

// change to 'binary' if you want a binary hash.
hash.setEncoding('hex');

// the text that you want to hash
hash.write('hello world');

// very important! You cannot read from the stream until you have called end()
hash.end();

// and now you get the resulting hash
var sha1sum = hash.read();


If you want to get the hash of a file, the best way is create a ReadStream from the file and pipe it into the hash:

var fs = require('fs');
var crypto = require('crypto');

// the file you want to get the hash
var fd = fs.createReadStream('/some/file/name.txt');
var hash = crypto.createHash('sha1');
hash.setEncoding('hex');

fd.on('end', function() {
hash.end();
console.log(hash.read()); // the desired sha1sum
});

// read all file and pipe it (write it) to the hash object
fd.pipe(hash);

Reply

#3
Short version of Carlos' answer:

var fs = require('fs')
var crypto = require('crypto')

fs.createReadStream('/some/file/name.txt').
pipe(crypto.createHash('sha1').setEncoding('hex')).
on('finish', function () {
console.log(this.read()) //the hash
})
Reply

#4
var fs = require('fs');
var crypto = require('crypto');
var fd = fs.createReadStream('data.txt');
var hash = crypto.createHash('md5');
hash.setEncoding('hex');
fd.pipe(hash);
hash.on('data', function (data) {
console.log('# ',data);
});

Reply

#5
An ES6 version returning a Promise for the hash digest:

function checksumFile(hashName, path) {
return new Promise((resolve, reject) => {
const hash = crypto.createHash(hashName);
const stream = fs.createReadStream(path);
stream.on('error', err => reject(err));
stream.on('data', chunk => hash.update(chunk));
stream.on('end', () => resolve(hash.digest('hex')));
});
}

Reply

#6
I use Node module hasha successfully, the code becomes very clean and short. It returns a promise, so you can use it with await:

const hasha = require('hasha');

const fileHash = await hasha.fromFile(yourFilePath, {algorithm: 'md5'});
Reply

#7
Further polish, ECMAScript 2015

`hash.js`:

```js
'use strict';

function checksumFile(algorithm, path) {
return new Promise(function (resolve, reject) {
let fs = require('fs');
let crypto = require('crypto');

let hash = crypto.createHash(algorithm).setEncoding('hex');
fs.createReadStream(path)
.once('error', reject)
.pipe(hash)
.once('end', function () {
resolve(hash.read());
});
});
}

checksumFile('sha1', process.argv[2]).then(function (hash) {
console.log('hash:', hash);
});
```

```bash
node hash.js hash.js
hash: 9c92ec7acf75f943aac66ca17427a4f038b059da
```

Works at least as early as v10.x:

```bash
node --version
v10.24.1
```
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through