节点-易感-cypto

提供围绕节点加密执行的简单包装. 库提供两个界面:简单和高级. 简单模式是为易用而设计的,而高级模式在某些用例中提供了一些性能效益. 详情见下文.
所有基础密码操作都是相同的.
简单使用( 建议)
刚开始就需要解放 并立即创建一个实例.
const crypto = require('crypto');
const easyCrypto = require('@emartech/easy-crypto');
const password = crypto.randomBytes(24).toString('hex');
const randomData = crypto.randomBytes(1024).toString('hex');
const ecrypto = easyCrypto(password);
async function exampleAsyncFunction() {
const encrypted = await ecrypto.encrypt(randomData);
const decrypted = await ecrypto.decrypt(encrypted);
randomData === decrypted; //true
}
高级用途(用于性能)
密钥衍生 是一个资源密集的过程。 默认设置在每个加密/解密进程之前重算密钥 .
这些选项允许您缓存密钥衍生结果 。 如果需要用同一衍生密钥加密/解密多次,则需要这样做。 打开密钥 节点缓存 节省您在每次加密/解密前重算的时间 .
刚开始就需要解放 并立即创建一个实例.
const crypto = require('crypto');
const easyCrypto = require('@emartech/easy-crypto');
const password = crypto.randomBytes(24).toString('hex');
const randomData = [
crypto.randomBytes(1024).toString('hex'),
crypto.randomBytes(1024).toString('hex'),
crypto.randomBytes(1024).toString('hex')
];
const ecrypto = easyCrypto(password, {
encryptCacheTtl: 3600,
decryptCachePoolSize: 100,
});
async function exampleAsyncFunction() {
const encrypted = await Promise.all(
randomData.map(item => ecrypto.encrypt(item))
);
const decrypted = await Promise.all(
encrypted.map(item => ecrypto.decrypt(item))
);
return data.reduce((allValid, item, index) => {
return allValid && item === decrypted[index];
}, true);
}
接口
初始化
没有太多的选择 你可以改变,这是故意的。 此小包装库默认安全 。 您可以更改两个配置 : encryptCacheTtl, (中文(简体) ), decryptCachePoolSize 以下列方式传递给初始化函数:
let ecrypto = require('@emartech/easy-crypto')('password', {
encryptCacheTtl: 3600,
decryptCachePoolSize: 100,
});
password
password 应该是任何正常的字符串。 用于生成加密密钥 .
encryptCacheTtl
在加密时同一密钥被再用时, 时间为秒 。 一定是整数.
decryptCachePoolSize
解密时保存在缓存中的最大密钥数 。 一定是整数.
加密( i)plaintext) - > 时间 ciphertext
plaintext 必须是 utf-8 编码字符串。 它将被"转换"为 bytes 用于加密操作 这项业务的产出是: base64 编码缓冲器。 这将用作 decrypt 操作。 此返回值为 Promise。 。 。 .
解密( n)ciphertext) - > 时间 plaintext
ciphertext 必须是 encrypt 方法。 库与盒子外的任何其他加密库不兼容! 此操作的输出为原始 utf-8 编码字符串。 此返回值为 Promise。 。 。 .
密码部件
图书馆只是节点的薄薄包装 crypto 模块。 它使用众所周知并经过战斗测试的加密技术. 它提供了一种围绕这些函数的方便的包装器,取去正确使用加密的细节. 自由探索源头吧!
加密过程
- 它为以后的操作生成随机字节
passwordSaltSize随机bytes用于创建256 bit长加密密钥password使用pbkdf2和给定的iteration count- 这个
plaintext正在使用aes-256-gcm与生成的密钥和 a12 bytes长随机initialization vector,该操作还生成16 bytes长authentication tag,用于验证加密数据的完整性 - 它将以下数据汇编成缓冲器:
passwordSalt bytes, (中文(简体) ),initialization vector bytes, (中文(简体) ),ciphertext bytes, (中文(简体) ),authentication tag bytes - 它用
base64还原它
解密过程
- 它解码
base64输入到字节 - 它将这些数据切入:
passwordSalt bytes, (中文(简体) ),initialization vector bytes, (中文(简体) ),ciphertext bytes, (中文(简体) ),authentication tag bytes - 这个
passwordSalt bytes编辑password用于生成256 bit使用长加密密钥pbkdf2和给定的iteration count - 这个
ciphertext bytes使用aes-256-gcm用生成的密钥initialization vector bytes。在加密期间,还使用authentication tag bytes - 它使用
utf-8还原它
找到窃听器了吗? 有意见吗?
请找到我们,我们很乐意你的反馈!