简介
npm包 @nodert-win8.1/windows.security.cryptography.core 是一个为前端开发者提供安全加密功能的工具库。该库可以让开发者在浏览器中使用 Windows.Security.Cryptography.Core 中的加密算法,提供了 SHA-1、 SHA-256、 SHA-384 和 SHA-512等多种加密功能。该库使用时需要注意的是,只支持 windows 系统中的使用,其他系统不支持。
安装
在使用该库前,需要先安装它。
npm install @nodert-win8.1/windows.security.cryptography.core
安装完成后,可以在项目中直接引入它。
import * as WindowsSecurityCryptographyCore from "@nodert-win8.1/windows.security.cryptography.core";
使用方法
Sha-1 加密
const plainText = "my message"; const sha1Hasher = new WindowsSecurityCryptographyCore.Security.Cryptography.Core.HashAlgorithmProvider("SHA1"); const utf8Buffer = WindowsSecurityCryptographyCore.Windows.Security.Cryptography.CryptographicBuffer.convertStringToBinary(plainText, WindowsSecurityCryptographyCore.Windows.Security.Cryptography.BinaryStringEncoding.utf8); const hashedBuffer = sha1Hasher.hashData(utf8Buffer); const hashedString = WindowsSecurityCryptographyCore.Windows.Security.Cryptography.CryptographicBuffer.encodeToHexString(hashedBuffer); console.log(hashedString); // 输出: 6f8f7993b0203c0dc256a5ba30942dbb3e4b4a5b
Sha-256 加密
const plainText = "my message"; const sha256Hasher = new WindowsSecurityCryptographyCore.Security.Cryptography.Core.HashAlgorithmProvider("SHA256"); const utf8Buffer = WindowsSecurityCryptographyCore.Windows.Security.Cryptography.CryptographicBuffer.convertStringToBinary(plainText, WindowsSecurityCryptographyCore.Windows.Security.Cryptography.BinaryStringEncoding.utf8); const hashedBuffer = sha256Hasher.hashData(utf8Buffer); const hashedString = WindowsSecurityCryptographyCore.Windows.Security.Cryptography.CryptographicBuffer.encodeToHexString(hashedBuffer); console.log(hashedString); // 输出: 547d6a2bf8ddee7f10e67b23b4296e8e8bceccd6c2a6fcbfb1301dbb113993b7
Sha-384 加密
const plainText = "my message"; const sha384Hasher = new WindowsSecurityCryptographyCore.Security.Cryptography.Core.HashAlgorithmProvider("SHA384"); const utf8Buffer = WindowsSecurityCryptographyCore.Windows.Security.Cryptography.CryptographicBuffer.convertStringToBinary(plainText, WindowsSecurityCryptographyCore.Windows.Security.Cryptography.BinaryStringEncoding.utf8); const hashedBuffer = sha384Hasher.hashData(utf8Buffer); const hashedString = WindowsSecurityCryptographyCore.Windows.Security.Cryptography.CryptographicBuffer.encodeToHexString(hashedBuffer); console.log(hashedString); // 输出: 2c2fe1f0798337b8f620d055f7a270efaa506117d34a55e6004642feaea015add0619b2a97fcb8d270f5e5fa5c5f0be4
Sha-512 加密
const plainText = "my message"; const sha512Hasher = new WindowsSecurityCryptographyCore.Security.Cryptography.Core.HashAlgorithmProvider("SHA512"); const utf8Buffer = WindowsSecurityCryptographyCore.Windows.Security.Cryptography.CryptographicBuffer.convertStringToBinary(plainText, WindowsSecurityCryptographyCore.Windows.Security.Cryptography.BinaryStringEncoding.utf8); const hashedBuffer = sha512Hasher.hashData(utf8Buffer); const hashedString = WindowsSecurityCryptographyCore.Windows.Security.Cryptography.CryptographicBuffer.encodeToHexString(hashedBuffer); console.log(hashedString); // 输出: d6c85e6bf98b6c9ee5d05197d433fbd5ed5a5f5c3d4e49f4bee15e3030494c1adde722b7a8c92133014aa7f340afc213d21a7cd69d8103282b7c37b9a0a96aa2
总结
npm包 @nodert-win8.1/windows.security.cryptography.core 为前端开发者提供了在浏览器中使用 Windows.Security.Cryptography.Core 中的加密算法的功能。本篇文章介绍了 SHA-1、 SHA-256、 SHA-384 和 SHA-512 等多种加密功能的使用方法,希望对大家的学习和开发有所帮助。
来源:JavaScript中文网 ,转载请注明来源 https://www.javascriptcn.com/post/60066bce967216659e244ce4