简体   繁体   中英

How to implement SHA-256 encryption in Angular2

I need to encrypt my password in SHA256 before making API request . I am not able to find any implementation of SHA-256 in Angular2

I used sha.js for this purpose, it is so simple and make the trick!

First npm install --save sha.js

Import in your component, service, whatever: import * as shajs from 'sha.js';

And for last, use it like the docs says: shajs('sha256').update({stringToBeHashed}).digest('hex')

Before I answer your question, you should understand that SHA256 should not be used for passwords . You should also be aware that client-side password hashing is not normally done, but there is a push for it from a number of researchers. The catch is that it is easy to do wrong. Guidance here and here .

Now to answer your question, rather than using Angular2, why not just pull in the Stanford JavaScript Crypto Library or Crypto-Js ? APIs on SHA256 are documented on these links.

SHA-256 & md5 both are provide hashing not encryption. SHA-256 not provide any angular2 support still now. If you want to hashstring/hashAsciiStr it's pretty simple in ts-md5....

ts-md5 npm link

Step to use ts-md5 :

  1. npm install

    npm install ts-md5

  2. Import the class in your component where you want to use

    import {Md5} from 'ts-md5/dist/md5';

  3. Hash some things

    Md5.hashStr('blah blah blah') => hex:string Md5.hashStr('blah blah blah', true) => raw:Int32Array(4) Md5.hashAsciiStr('blah blah blah') => hex:string Md5.hashAsciiStr('blah blah blah', true) => raw:Int32Array(4)

hopefully it helps you

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM