简体   繁体   English

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

[英]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 LibraryCrypto-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中非常简单....

ts-md5 npm link ts-md5 npm链接

Step to use ts-md5 : 步骤使用ts-md5:

  1. npm install npm安装

    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 希望它能帮助你

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何让 Google Apps 脚本进行 SHA-256 加密? - How do I get Google Apps Script to do SHA-256 encryption? 使用应用脚本在 Google 表格中进行 sha-256 加密 - sha-256 encryption within Google Sheets using App Scripts SHA-256密码生成器 - SHA-256 password generator 如何使用 SubtleCrypto 对 chrome 扩展中的文本进行 SHA-256 hash 文本处理 - How to SHA-256 hash text in a chrome extension with SubtleCrypto 如何在nodejs中验证java生成的密码hash(sha-256)? - How to validate a java generated password hash (sha-256) in nodejs? 如何将 SHA-256 Java 转换为 JavaScript(带有 NPM 的 IONIC)? - How to convert SHA-256 Java to JavaScript( IONIC with NPM )? 当现代 web 浏览器在您不在安全上下文 (https) 中时禁用 SubtleCrypto 时,如何使用 SHA-256 实现摘要身份验证? - How do you implement digest authentication with SHA-256 when modern web browsers disable SubtleCrypto when you're not in a secure context (https)? 在Angular2中,生成的文件的SHA256哈希与其他站点的SHA256不匹配 - In Angular2 the SHA256 hash of a file generated does not match with SHA256 from other sites 如何使用 window.crypto.subtle 验证 ES256(使用 P-256 和 SHA-256 的 ECDSA)签名? - How to verify an ES256 (ECDSA using P-256 and SHA-256) signature with window.crypto.subtle? 如何使用 FileReader 读取二进制文件以便在 CryptoJS 中使用 SHA-256 对其进行散列? - How to read a binary file with FileReader in order to hash it with SHA-256 in CryptoJS?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM