面向 JS 开发者的密码学要点速览。
Buffer
完成各类编码格式转换。Math.random
!!crypto
模块中的 random
函数,如 crypto.randomBytes
crypto.createHash('sha256')
crypto.createHmac('sha256', 'secret')
crypto.scrypt
crypto.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)
ArrayBuffer
atob
和 btoa
TextEncoder
Window.crypto.getRandomValues
Window.crypto.randomUUID
window.crypto.subtle.generateKey(algorithm, extractable, usages)
window.crypto.subtle.importKey(format, data, algorithm, extractable, usages)
window.crypto.subtle.exportKey(format, key)
window.crypto.subtle.wrapKey
window.crypto.subtle.unwrapKey
window.crypto.subtle.deriveKey
window.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.sign
window.crypto.subtle.verify
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!