-1

I am trying to hash a file more than 1GB using SHA256 in an Ionic 4 app based on Angular 7. I want to use this NPM module. But there is no documentation. How and where to import this module? or suggest a better alternative.

Following error I am getting: enter image description here

I have tried: import * as sha256File from 'sha256-file';

This is index.js file: there is no 'crypto' or 'fs' in the folder.

'use strict';

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

module.exports = function (filename, callback) {
  var sum = crypto.createHash('sha256');
  if (callback && typeof callback === 'function') {
    var fileStream = fs.createReadStream(filename);
    fileStream.on('error', function (err) {
      return callback(err, null)
    });
    fileStream.on('data', function (chunk) {
      try {
        sum.update(chunk)
      } catch (ex) {
        return callback(ex, null)
      }
    });
    fileStream.on('end', function () {
      return callback(null, sum.digest('hex'))
    })
  } else {
    sum.update(fs.readFileSync(filename));
    return sum.digest('hex')
  }
};
  • you're more likely to get a helpful answer if you post code you've already tried and error messages you're getting. – bryan60 Apr 27 '19 at 15:14
  • I have attached the screenshot or error with the question. Kindly check that. – Supinder Singh Apr 27 '19 at 15:33
  • (1) The code you posted is the of the module you linked to, but where is _your code that implements it?_ (2) This is a nodejs package designed for server-side file paths, and uses [node's built-in](https://nodejs.org/api/fs.html) [`fs` module](https://stackoverflow.com/questions/27019242/node-js-fs-module-inside-browser). There is, as far as I can see, no Angular or Ionic anywhere. Are you actually trying to hash this file on a server or in the browser? This is very unclear. – msanford Apr 27 '19 at 21:05

4 Answers4

1

Take a look at Sodium (libsodium.js).

Sodium is a modern, easy-to-use software library for encryption, decryption, signatures, password hashing and more.

It is a portable, cross-compilable, installable, packageable fork of NaCl, with a compatible API, and an extended API to improve usability even further.

Its goal is to provide all of the core operations needed to build higher-level cryptographic tools.

Sodium is cross-platforms and cross-languages. It runs on a variety of compilers and operating systems, including Windows (with MinGW or Visual Studio, x86 and x86_64), iOS and Android. Javascript and WebAssembly versions are also available and are fully supported. Bindings for all common programming languages are available and well-supported.

The design choices emphasize security and ease of use. But despite the emphasis on high security, primitives are faster across-the-board than most implementations.

Version 1.0.17 was released on January 7, 2019.

Ref:

Robinyo
  • 674
  • 3
  • 10
0

You should post code you've tried in any questions, but based on this particular package, you probably just need to install it and do something like:

import * as sha256File from 'sha256-file';

then usage is simple according to the docs:

const sum = sha256File('./path/to/a_file');
bryan60
  • 22,247
  • 3
  • 31
  • 47
  • Failed to compile using this approach. I think import is working but there is some other issues. I have attached the error screenshot with the question. Kindly check that. – Supinder Singh Apr 27 '19 at 15:33
  • Seems like this package is poorly constructed and doesn’t list its own dependencies well / it assumes crypto will be available. You can install crypto but fs will be a problem, though it would be available in a more env it’s not in a browser environment. You’ll need to edit the function to eliminate the fs dependency and use the FileReader api instead. – bryan60 Apr 27 '19 at 16:15
  • 2
    My recommendation is to ditch this package, just install crypto and rewrite the function with the file readerAPI instead – bryan60 Apr 27 '19 at 16:21
0

Finally I solved the problem. Thank you everyone for suggesting me the solutions. In my solution I user 'crypto-js' library. I import it as:

import * as crypto from 'crypto-js';

Then user file reader to read file as below:

const reader = new FileReader();
    reader.onloadend = () => {
      const imgBlob = new Blob([reader.result], {
        type: filePath.type
      });
      var file_wordArr = crypto.lib.WordArray.create(imgBlob); //convert blob to WordArray
          var sha256_hash = crypto.SHA256(file_wordArr); //calculate SHA256 hash
          //alert("Calculated SHA1:" + sha256_hash.toString()); 
          console.log(sha256_hash.toString());
}
this.file.resolveLocalFilesystemUrl(filePath) //file path of android system
.then(entry => {
  (<FileEntry>entry).file(file => reader.readAsArrayBuffer(file))
})

I got what I wanted with this code.

-1

You should have a look at the index.js file of the package you are trying to use. It seems like you are missing some packages (namely crypto and fs, which I assume is filesystem). Try installing them with npm

RomanHDev
  • 64
  • 4
  • I looked into this post and found that 'fs' is no longer by Angular 7. https://github.com/angular/angular-cli/issues/8272 – Supinder Singh Apr 27 '19 at 16:00
  • getting following warnings while installing 'fs'. – Supinder Singh Apr 27 '19 at 16:21
  • npm install fs npm WARN @ionic/pro@2.0.4 requires a peer of cordova-plugin-ionic@^5.0.0 but none is installed. You must install peer dependencies yourself. npm WARN ajv-keywords@3.4.0 requires a peer of ajv@^6.9.1 but none is installed. You must install peer dependencies yourself. npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.8 (node_modules\fsevents): npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.8: wanted {"os":"darwin","arch":"any"} (current: {"os":"win32","arch":"x64"}) – Supinder Singh Apr 27 '19 at 16:24
  • I had a look at the package. It hasn‘t been updated for a year now. So either have to rollback to an older distribution of angular, which I would probably not recommend or write your own function with crypto as bryan60 suggested – RomanHDev Apr 27 '19 at 17:05
  • The package is written for nodejs and uses the `fs` and `crypto` from its standard lib. There aren't any missing packages: it's just not written for the browser, which is what OP seems to be trying to do. – msanford Apr 27 '19 at 21:08