Javascript 如何在 Angular2 中实现 SHA-256 加密

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43893516/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 02:13:47  来源:igfitidea点击:

How to implement SHA-256 encryption in Angular2

javascriptangularsha256

提问by Shifs

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

在发出 API 请求之前,我需要在 SHA256 中加密我的密码。我在 Angular2 中找不到任何 SHA-256 的实现

回答by hmartos

I used sha.jsfor 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')

回答by TheGreatContini

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 hereand here.

在我回答你的问题之前,你应该明白SHA256 不应该用于密码。您还应该知道客户端密码散列通常不会完成,但许多研究人员都在推动它。问题是很容易做错。此处此处的指南。

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

现在来回答你的问题,与其使用 Angular2,为什么不直接引入Stanford JavaScript Crypto LibraryCrypto-Js呢?这些链接中记录了 SHA256 上的 API。

回答by Sayan Samanta

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....

SHA-256 和 md5 都提供散列而不是加密。SHA-256 现在仍然不提供任何 angular2 支持。如果你想 hashstring/hashAsciiStr,它在 ts-md5 中非常简单......

ts-md5 npm link

ts-md5 npm 链接

Step to use ts-md5 :

使用 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)

  1. 安装

    npm install ts-md5

  2. 在要使用的组件中导入类

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

  3. 散列一些东西

    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

希望它可以帮助你