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
5 Answers
I used sha.js for this purpose, it is so simple and does the trick!
First npm install --save sha.js
Import in your component, service, etc... :
import * as shajs from 'sha.js';
And then use it as the docs suggests:
shajs('sha256').update({stringToBeHashed}).digest('hex')
5 Comments
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.
1 Comment
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....
Step to use ts-md5 :
npm install
npm install ts-md5Import the class in your component where you want to use
import {Md5} from 'ts-md5/dist/md5';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
4 Comments
Angular 14:
I had to use js-sha256 for the purpose with the latest version.
Simply import in the component
import { sha256 } from 'js-sha256';
and use like
console.log(sha256.update("secretPass").hex());
Comments
you shoud import shajs directly fom sha.js
import shajs from 'sha.js';
and then reuse it :
export class AppComponent {
hash = shajs('sha256').update('Abc123$').digest('hex');
}