Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use 'crypto' module in Angular2?

I install module:

npm install --save crypto

I import it to my component:

import { createHmac } from "crypto";

But I get error:

ERROR in -------------- (4,28): Canno t find module 'crypto'.

What am I doing wrong?

like image 886
j809809jkdljfja Avatar asked Apr 11 '17 18:04

j809809jkdljfja


People also ask

What is Crypto node module?

The node:crypto module provides cryptographic functionality that includes a set of wrappers for OpenSSL's hash, HMAC, cipher, decipher, sign, and verify functions.

How use crypto JS in react JS?

To encrypt and decrypt data, simply use encrypt() and decrypt() function from an instance of crypto-js. var bytes = CryptoJS. AES. decrypt(ciphertext, 'my-secret-key@123');

What is Crypto Browserify?

A port of node's crypto module to the browser. The goal of this module is to reimplement node's crypto module, in pure javascript so that it can run in the browser. Here is the subset that is currently implemented: createHash (sha1, sha224, sha256, sha384, sha512, md5, rmd160)


1 Answers

I am developing with the latest versions of Angular and 'crypto-js' seems to work fine.

Install the package and the definitions:

npm install crypto-js
npm install --save @types/crypto-js

Use it:

import { SHA256, enc } from "crypto-js";
...
login() {
...
   const hashedPass = SHA256(this.loginForm.value.password).toString(enc.Hex);
...
}
like image 126
Javi Avatar answered Oct 07 '22 01:10

Javi