Caligatio / jsSHA

A JavaScript/TypeScript implementation of the complete Secure Hash Standard (SHA) family (SHA-1, SHA-224/256/384/512, SHA3-224/256/384/512, SHAKE128/256, cSHAKE128/256, and KMAC128/256) with HMAC.

Home Page:https://caligatio.github.io/jsSHA/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Error when i create jsSHA object

dhksrini opened this issue · comments

I have react application. I have implemented jsSHA library installed using npm. When I initiate the object getting Uncaught TypeError: jsSHA is not a constructor.
Below my code

const hash_obj_mobile_number = new jsSHA("SHA-256", "TEXT", { encoding: "UTF8" }), hash_obj_consent_pin = new jsSHA("SHA-256", "TEXT", { encoding: "UTF8" }); hash_obj_mobile_number.update(mobile_number); hash_obj_consent_pin.update(login_pin); const hash_mobile_number = hash_obj_mobile_number.getHash("HEX"); const hash_consent_pin = hash_obj_consent_pin.getHash("HEX");

That looks correct, how are you importing it (I'm not a React guy myself)? Is it the ESM or UMD version?

I have this problem as well; tried importing with import * as jsSHA from 'jssha' and import jsSHA from 'jssha'

What version of node are you using?

commented

This happens to me with node v12.6.3 and v14.4.0 using latest GatsbyJS

Tried a few things:
import jsSHA256 from "jssha/dist/sha256.js";
import jsSHA256 from "jssha"

@insuusvenerati What is the extension on your file? For Node to be happy using ESM, they need to be .mjs files and not .js

Can you try the below snippet?

$ nvm install 14.4
$ node --version
v14.4.0
$ npm install jssha
$ echo -e 'import jsSHA256 from "jssha"\nconst test = new jsSHA256("SHA-256", "TEXT")\nconsole.log(test.getHash("HEX"))' > test.mjs
$ node test.mjs
e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
commented
❯ node .\test.mjs        
e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855

Hmm, I did some Googling at it may be some problem with ESM + minimization + rollup.js: rollup/rollup#2646

Just as a double check while I reach out to the people in the other ticket, can you confirm that

import jsSHA from "jssha/dist/sha.mjs"

does not work? I didn't bundle a ESM version of sha256 so that is expectedly failing and I'm wondering if the ESM/UMD resolution isn't working.

As a reply to myself, it looks like React doesn't support native ESM well: jestjs/jest#9430

The explicit .mjs import should hopefully test this.

I have the same problem with Typescript 3.9.5

import * as jsSHA from "jssha";
import jsSHA from "jssha";

Both does not work

const jsSHA = require("jssha");

This works if i forced commonjs import.

@Juansasa can you try:

import jsSHA from "jssha/dist/sha.mjs"

@Caligatio
I got this error
TS2307: Cannot find module 'jssha/dist/sha.mjs' or its corresponding type declarations.

@Juansasa Are you also something the derives from React?

No this is from our E2E test script which also use typescript.

When i ignore the error and run the script anyways i got this error

Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: Package subpath './dist/sha.mjs' is not defined by "exports" in .../node_modules/jssha/package.json at applyExports (internal/modules/cjs/loader.js:497:9) at resolveExports (internal/modules/cjs/loader.js:513:23) at Function.Module._findPath (internal/modules/cjs/loader.js:641:31) at Function.Module._resolveFilename (internal/modules/cjs/loader.js:1016:27) at Function.Module._load (internal/modules/cjs/loader.js:898:27) at Module.require (internal/modules/cjs/loader.js:1089:19) at require (internal/modules/cjs/helpers.js:73:18) at Object.<anonymous> (.../out-tsc/utils/users.js:7:17) at Module._compile (internal/modules/cjs/loader.js:1200:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:1220:10) at Module.load (internal/modules/cjs/loader.js:1049:32) at Function.Module._load (internal/modules/cjs/loader.js:937:14) at Module.require (internal/modules/cjs/loader.js:1089:19) at require (internal/modules/cjs/helpers.js:73:18) at Object.<anonymous> (.../out-tsc/test-runner.js:3:15) at Module._compile (internal/modules/cjs/loader.js:1200:30) { code: 'ERR_PACKAGE_PATH_NOT_EXPORTED'

@Juansasa Any chance you can share a reproducible code snippet?

I think I pinned it down, TypeScript requires --esModuleInterop which is now also the default when using tsc --init

@Caligatio
We already have this enabled in our tsconfig.json or is it something else you mean ?

@Juansasa: could you please provide the actual full error that you're getting? I'm having to take educated guesses as to what people are encountering as no one has given me the full original error.

I'm closing this due to lack of activity. Please feel to reopen if there are more details.

Error is in typescript environment, I work with typescript with nestjs. So, to reproduce, just create a nest project or any typescript project I guess. Then import like this:

import * as jsSHA from 'jssha';

then create a jsSHA instance:

const jsSHA = new jsSHA('SHA-512', 'TEXT')

Then you will get an error like this:
Screen Shot 2021-01-11 at 21 45 36

@joadr: Did you try the ESM example provided? Try:

import jsSHA from "jssha";

@Caligatio yes, when I do that I get this error:
UnhandledPromiseRejectionWarning: TypeError: jssha_1.default is not a constructor
and when logging it, it is undefined

@joadr: Try adding "esModuleInterop": true into your tsconfig.json if it's not already in there (see my tsconfig.json for an example). I remember some funniness with TypeScript and default imports which I believe was fixed with that config.

import * as SHA from "jssha";
const sha = new SHA.default("SHA-256", "TEXT");

This worked for me.