[英]How to implement SHA-256 encryption in Angular2
I need to encrypt my password in SHA256 before making API request . 在发出API请求之前,我需要在SHA256中加密我的密码。 I am not able to find any implementation of SHA-256 in Angular2
我无法在Angular2中找到任何SHA-256的实现
I used sha.js for this purpose, it is so simple and make the trick! 我为此目的使用了sha.js ,它是如此简单并且成功!
First npm install --save sha.js
首先是
npm install --save sha.js
Import in your component, service, whatever: import * as shajs from 'sha.js';
导入组件,服务,无论如何:
import * as shajs from 'sha.js';
And for last, use it like the docs says: shajs('sha256').update({stringToBeHashed}).digest('hex')
最后,像文档中所说的那样使用它:
shajs('sha256').update({stringToBeHashed}).digest('hex')
Before I answer your question, you should understand that SHA256 should not be used for passwords . 在回答您的问题之前,您应该了解SHA256不应该用于密码 。 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 ? 现在回答你的问题,而不是使用Angular2,为什么不直接拉入Stanford JavaScript Crypto Library或Crypto-Js ? APIs on SHA256 are documented on these links.
SHA256上的API记录在这些链接上。
SHA-256 & md5 both are provide hashing not encryption. SHA-256和md5都提供散列不加密。 SHA-256 not provide any angular2 support still now.
SHA-256现在还没有提供任何angular2支持。 If you want to hashstring/hashAsciiStr it's pretty simple in ts-md5....
如果你想要hashstring / hashAsciiStr,它在ts-md5中非常简单....
Step to use ts-md5 : 步骤使用ts-md5:
npm install npm安装
npm install ts-md5
Import 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 希望它能帮助你
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.