面向 JS 开发者的密码学要点速览。
Buffer 完成各类编码格式转换。Math.random !!crypto 模块中的 random 函数,如 crypto.randomBytescrypto.createHash('sha256')crypto.createHmac('sha256', 'secret')crypto.scryptcrypto.createCipheriv('aes-256-cbc', ...)crypto.createDecipheriv('aes-256-cbc', ...)crypto.createCipheriv('aes-256-gcm', ...)crypto.createDecipheriv('aes-256-gcm', ...)crypto.createCipheriv('chacha20-poly1305', ...)crypto.createDecipheriv('chacha20-poly1305', ...)// 导入
crypto.createPrivateKey(fs.readFileSync("public.pem"));
//导入
crypto.createPrivateKey(fs.readFileSync("private.pem"));
crypto.KeyObject.export(option)crypto.generateKeyPair('rsa', ...)crypto.publicEncrypt(
{
key: publicKey,
padding: crypto.constants.RSA_PKCS1_OAEP_PADDING,
oaepHash: "sha256",
},
plaintext
);
crypto.privateDecrypt(
{
key: privateKey,
padding: crypto.constants.RSA_PKCS1_OAEP_PADDING,
oaepHash: "sha256",
},
message
);
crypto.sign("sha256", message, {
key: privateKey,
padding: crypto.constants.RSA_PKCS1_PSS_PADDING,
});
crypto.verify(
"sha256",
message,
{
key: publicKey,
padding: crypto.constants.RSA_PKCS1_PSS_PADDING,
},
signature
);
crypto.generateKeyPair('ec', ...)shareSecret(A 公钥, B 私钥) = shareSecret(B 公钥, A 私钥)crypto.diffieHellman({对方公钥,自己私钥})crypto.diffieHellman 得到 shareSecretsha256(shareSecret, salt) 为公共密钥,注:salt 记得随密文传给对方。crypto.sign('sha256', message, privateKey)crypto.verify('sha256', message, publicKey, signature)crypto.sign(null, message, privateKey)crypto.verify(null, message, publicKey, signature)ArrayBufferatob 和 btoaTextEncoderWindow.crypto.getRandomValuesWindow.crypto.randomUUIDwindow.crypto.subtle.generateKey(algorithm, extractable, usages)window.crypto.subtle.importKey(format, data, algorithm, extractable, usages)window.crypto.subtle.exportKey(format, key)window.crypto.subtle.wrapKeywindow.crypto.subtle.unwrapKeywindow.crypto.subtle.deriveKeywindow.crypto.subtle.digest('SHA-256', ...)window.crypto.subtle.sign("HMAC", key, encoded)crypto.subtle.encrypt(algorithm, key, data)crypto.subtle.decrypt(algorithm, key, data)window.crypto.subtle.signwindow.crypto.subtle.verify如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!