9

Im writting a web with Angular 6. I need to hash a string client-side, so i'm using createHash("sha256") from 'crypto'.

actually, I just wrote createHash and vscode suggest me the import, which looks like this:

import { createHash } from "crypto";

(this way of importing seems to be fine, and it's used in some typescript tutorial, here) and then:

var hashed = createHash("sha256").update(data).digest()

all syntax is being suggested by vscode, with docstrings and everything. But at the moment of compile with npm start I get:

ERROR in src/domain/User.ts(2,28): error TS2307: Cannot find module 'crypto'.

as far as I could understand, crypto is now built-in into node, and I shouldn't have any problem importing it.

also notice that if I run node in terminal to open a REPL, entering 'crypto' gives me an output that suggest that everything works well.

Here are the versions of everything I think that cares:

node --version: v10.15.1
ng --version:
Angular CLI: 6.2.9
Node: 10.15.1
OS: linux x64
Angular: 6.1.10
typescript 2.9.2
webpack 4.16.4

Any help will be appreciated.

alete
  • 608
  • 2
  • 8
  • 23

4 Answers4

24

For Typescript 2.* and Angular 2+ -

  1. Install and add this package to the devDependencies.

    npm install @types/node --save-dev
    
  2. In tsconfig.app.json under the compilerOptions, add this -

    "types": [ "node" ],
    
Pushpak
  • 301
  • 3
  • 6
1

You need to install the dependency. There was the same question, try to do the same, it must help How to use 'crypto' module in Angular2?

  • Yes, I've been there. But note that there is not selected answer. Notice [this](https://stackoverflow.com/questions/43353718/how-to-use-crypto-module-in-angular2#comment73770954_43353846) about first answer. I've tried second answer without success. Thanks – alete Feb 12 '19 at 23:20
  • There is an issue about that on github https://github.com/angular/angular-cli/issues/1548#issuecomment-286151056 Unfortunately, crypto is not supported as a native client-side module yet. Module crypto-browserify contains createHash, check, if it is appropriate for you. It installs as a regular dependency and shouldn't cause serious troubles. – Andrey Dobryakov Feb 13 '19 at 15:05
1

I had the same issue. The first solution I found was to add the following to packages.json

"browser": {
    "crypto": false
}

Since all I wanted was to generate a sha256 digest to use a an index I removed crypto-js and @types/crypto=js and replaced it with jshashed.

yarn add jshashes

I then modified my digest service to reflect the new library:

import { Injectable } from '@angular/core';
import * as JsHashes  from 'jshashes';

@Injectable({
  providedIn: 'root'
})

export class CryptoService {
  getSha256HEX(value: string): string {
     const hash =  new JsHashes.SHA256;
     return hash.hex(value);
  }
}

Simply add CryptoService to your module file apps.module.ts

import { CryptoService }  from '@services/crypto.service';

Note: The @services path defined in tsconfig.json

All that is left is to declaref CryptoService in the constructor were needed:

constructor( private _crypto: CryptoService; ) {}

and use it by passing a string, or if compound data (like and HTTP query string to build a cache key) stringify it.

 myHash = this._crypto.getSha256HEX(JSON.stringify(_compoundDataStruccture));

Note: jshashes supports

Digests:

  • MD5
  • SHA1
  • SHA256
  • SHA512
  • HMAC
  • RIPEND-160

Also:

  • Base64 encoding/decoding
  • CRC-32 calculation
  • UTF-8 encoding/decoding
alindber
  • 188
  • 15
0

If you are making a server side application for NodeJs using typescript then you simply need to install @types/node using npm i @types/node -D and then you should be able to import it using import * as crypto from "crypto"

Blaze
  • 1,642
  • 2
  • 22
  • 20