node.js内置模块之---crypto 模块
crypto 模块的作用在 Node.js 中crypto模块提供了多种加密功能包括哈希、对称加密、非对称加密和数字签名等。通过crypto模块可以进行各种加密和解密操作保护敏感数据的安全性。crypto 模块1. 哈希算法Hashing哈希函数如 SHA、MD5 等用于将输入数据映射为一个固定长度的字符串哈希值。它是单向的不可逆的通常用于数据完整性验证。createHash(algorithm)创建一个哈希对象algorithm指定使用的哈希算法例如sha256,md5。update(data)向哈希对象添加数据可以调用多次。digest(encoding)返回哈希值encoding可以是hex、base64或binary。示例生成 SHA-256 哈希值const hash crypto.createHash(sha256); hash.update(Hello, world!); const result hash.digest(hex); console.log(result); // 输出 SHA-256 哈希值2、加密与解密Encryption and Decryption对称加密对称加密使用相同的密钥进行加密和解密。crypto模块支持多种对称加密算法如 AESAES-128、AES-256等。createCipheriv(algorithm, key, iv)创建加密对象algorithm是加密算法key是密钥iv是初始化向量IV。update(data, inputEncoding, outputEncoding)将明文数据输入并指定编码返回加密数据。final(outputEncoding)返回最后的加密数据。示例使用 AES-256-CBC 加密和解密const algorithm aes-256-cbc; const key crypto.randomBytes(32); // 32 字节的密钥 const iv crypto.randomBytes(16); // 16 字节的初始化向量 // 加密 const cipher crypto.createCipheriv(algorithm, key, iv); let encrypted cipher.update(Hello, world!, utf8, hex); encrypted cipher.final(hex); console.log(Encrypted:, encrypted); // 解密 const decipher crypto.createDecipheriv(algorithm, key, iv); let decrypted decipher.update(encrypted, hex, utf8); decrypted decipher.final(utf8); console.log(Decrypted:, decrypted);非对称加密非对称加密使用一对密钥——公钥和私钥。公钥用于加密私钥用于解密。generateKeyPairSync(type, options)同步生成公钥和私钥对type指定密钥类型如rsaoptions指定密钥的参数。publicEncrypt(publicKey, data)使用公钥对数据进行加密。privateDecrypt(privateKey, data)使用私钥对数据进行解密。示例使用 RSA 非对称加密const { publicKey, privateKey } crypto.generateKeyPairSync(rsa, { modulusLength: 2048, // 公钥的位数 }); // 使用公钥加密 const encrypted crypto.publicEncrypt(publicKey, Buffer.from(Hello, world!)); console.log(Encrypted:, encrypted.toString(hex)); // 使用私钥解密 const decrypted crypto.privateDecrypt(privateKey, encrypted); console.log(Decrypted:, decrypted.toString());3. HMACHash-based Message Authentication CodeHMAC 是一种基于哈希的消息认证码用于验证消息的完整性和真实性。它结合了哈希函数和密钥能够防止中间人攻击。createHmac(algorithm, key)创建 HMAC 对象algorithm指定哈希算法如sha256key是密钥。update(data)向 HMAC 对象输入数据。digest(encoding)返回 HMAC 的结果通常是hex或base64编码。示例生成 HMACconst secret my-secret-key; const hmac crypto.createHmac(sha256, secret); hmac.update(Hello, world!); const result hmac.digest(hex); console.log(HMAC:, result);4. 随机数生成Random Number Generationcrypto提供了生成安全随机数的功能用于生成随机密码、令牌等。randomBytes(size)生成指定字节数的随机数据。示例生成随机字节const randomBytes crypto.randomBytes(16); // 生成16个随机字节 console.log(randomBytes.toString(hex)); // 输出十六进制字符串5. 数字签名Digital Signature数字签名用于验证数据的完整性和身份认证通常用于公钥基础设施PKI中。它使用私钥对数据签名使用公钥验证签名。createSign(algorithm)创建一个签名对象algorithm指定哈希算法。createVerify(algorithm)创建一个验证签名对象algorithm指定哈希算法。sign(privateKey, encoding)使用私钥对数据进行签名。verify(publicKey, signature, encoding)使用公钥验证签名。示例生成签名const { privateKey, publicKey } crypto.generateKeyPairSync(rsa, { modulusLength: 2048, }); // 生成签名 const sign crypto.createSign(SHA256); sign.update(Hello, world!); const signature sign.sign(privateKey, hex); console.log(Signature:, signature); // 验证签名 const verify crypto.createVerify(SHA256); verify.update(Hello, world!); const isVerified verify.verify(publicKey, signature, hex); console.log(Verified:, isVerified); // true 或 false6. 密钥对生成Key Pair Generation非对称加密中公钥和私钥的生成可以通过crypto.generateKeyPairSync方法。generateKeyPairSync(type, options)生成公钥和私钥对type指定加密算法类型如rsaoptions包含密钥的相关参数。export(options)将密钥导出为 PEM 格式。示例生成 RSA 密钥对const { privateKey, publicKey } crypto.generateKeyPairSync(rsa, { modulusLength: 2048, }); console.log(Private Key:, privateKey.export({ type: pkcs1, format: pem })); console.log(Public Key:, publicKey.export({ type: pkcs1, format: pem }));
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2422643.html
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!