@kernelift/utils
v1.0.7
Published
kernelift 前端工具方法库
Maintainers
Readme
@kernelift/utils
Kernelift 前端工具方法库,提供加密解密、颜色处理、跨标签页通信等实用功能。
安装
pnpm add @kernelift/utils包已标记 sideEffects: false,根入口的命名导出可参与摇树优化。加密相关能力仍建议通过
@kernelift/utils/crypto 单独导入,避免默认入口带入可选加密依赖。
功能模块
1. AES-GCM 加密
提供 AES-GCM(高级加密标准-伽罗瓦/计数器模式)加密解密功能。
使用示例
import { AesGcmUtils, AesGcmEncryption } from '@kernelift/utils/crypto';
// 创建加密实例
const aes = new AesGcmEncryption(256, 12, 128);
// 生成密钥
const key = aes.generateKey();
// 加密数据
const result = aes.encrypt('Hello World', key);
console.log(result.ciphertext); // 加密后的密文
console.log(result.iv); // 初始化向量
console.log(result.tag); // 认证标签
// 解密数据
const decrypted = aes.decrypt(result.ciphertext, key, result.iv, result.tag);
console.log(decrypted); // "Hello World"
// 快速加密(自动生成IV)
const quickResult = aes.quickEncrypt('Quick encrypt', key);主要方法
| 方法 | 说明 |
|------|------|
| generateKey() | 生成随机密钥 |
| generateKeyFromPassword(password, salt?, iterations?) | 从密码生成密钥(PBKDF2) |
| generateIv() | 生成随机IV |
| encrypt(plaintext, key, iv?, additionalData?) | 加密数据 |
| decrypt(ciphertext, key, iv, tag, additionalData?) | 解密数据 |
| verifyIntegrity(ciphertext, key, iv, tag, additionalData?) | 验证数据完整性 |
| quickEncrypt(plaintext, key) | 快速加密(自动生成IV) |
2. RSA-OAEP 加密
提供 RSA-OAEP(最优非对称加密填充)加密解密功能。
使用示例
import { RSAUtils } from '@kernelift/utils/crypto';
// 生成密钥对
const keyPair = RSAUtils.generateKeyPair();
console.log(keyPair.publicKeyPem);
console.log(keyPair.privateKeyPem);
// 加密
const publicKey = RSAUtils.pemToPublicKey(keyPair.publicKeyPem);
const encrypted = RSAUtils.encrypt('Secret message', publicKey);
// 解密
const privateKey = RSAUtils.pemToPrivateKey(keyPair.privateKeyPem);
const decrypted = RSAUtils.decrypt(encrypted, privateKey);
console.log(decrypted); // "Secret message"主要方法
| 方法 | 说明 |
|------|------|
| generateKeyPair() | 生成 RSA 密钥对(2048位) |
| pemToPublicKey(pem) | PEM 格式转公钥对象 |
| pemToPrivateKey(pem) | PEM 格式转私钥对象 |
| encrypt(text, publicKey) | RSA-OAEP 加密 |
| decrypt(ciphertext, privateKey) | RSA-OAEP 解密 |
3. 颜色工具
提供颜色转换和主题色生成功能。
使用示例
import { hexToRgb, generateLightColors } from '@kernelift/utils';
// 十六进制转RGB
const rgb = hexToRgb('#409eff');
console.log(rgb); // { r: 64, g: 158, b: 255 }
// 生成主题色系(11个等级,类似Tailwind的50-950)
const colors = generateLightColors('#409eff');
console.log(colors);
// ["rgb(237,245,255)", "rgb(229,240,254)", ..., "rgb(64,158,255)", ..., "rgb(15,39,88)"]主要方法
| 方法 | 说明 |
|------|------|
| hexToRgb(hex) | 十六进制颜色转RGB对象 |
| generateLightColors(primaryColor, levels?, intensity?, targetIndex?) | 生成不同亮度的主题色系 |
4. 广播通道
提供跨标签页/窗口通信功能,基于 BroadcastChannel API。
使用示例
import {
createBroadcastChannel,
postBroadcastMessage,
onBroadcastMessage,
closeBroadcastChannel,
BroadcastChannelWrapper
} from '@kernelift/utils';
// 方式1:使用函数式API
const channel = createBroadcastChannel('my-channel');
// 发送消息
postBroadcastMessage(channel, { type: 'message', data: 'Hello' });
// 监听消息
onBroadcastMessage(channel, (data) => {
console.log('收到消息:', data);
});
// 关闭频道
closeBroadcastChannel(channel);
// 方式2:使用封装类
const wrapper = new BroadcastChannelWrapper<{ type: string; data: string }>('my-channel');
wrapper.postMessage({ type: 'message', data: 'Hello' });
wrapper.onMessage((data) => console.log('收到消息:', data));
wrapper.close();主要方法
| 方法 | 说明 |
|------|------|
| createBroadcastChannel(channelName) | 创建广播频道 |
| postBroadcastMessage(channel, message) | 发送消息 |
| onBroadcastMessage(channel, callback) | 监听消息 |
| closeBroadcastChannel(channel) | 关闭频道 |
| BroadcastChannelWrapper | 广播频道封装类 |
5. 安全执行器
提供安全的 JavaScript 代码执行环境,限制可用的全局对象。
使用示例
import { SafeEvaluator } from '@kernelift/utils';
const evaluator = new SafeEvaluator({
console,
Math,
Date
});
// 安全执行代码
const result = evaluator.evaluate('Math.max(1, 2, 3)', {});
console.log(result); // 3
// 使用上下文变量
const contextResult = evaluator.evaluate('a + b * 2', { a: 10, b: 5 });
console.log(contextResult); // 20主要方法
| 方法 | 说明 |
|------|------|
| evaluate(code, context?) | 安全执行 JavaScript 代码 |
6. 数据工具
提供常用的数据处理工具函数。
使用示例
import {
uniqueArray,
cloneDeep,
merge,
omit,
formatCurrency
} from '@kernelift/utils';
// 数组去重
const arr = [1, 2, 2, 3, 3, 4];
const unique = uniqueArray(arr);
console.log(unique); // [1, 2, 3, 4]
// 深拷贝
const obj = { a: 1, b: { c: 2 } };
const cloned = cloneDeep(obj);
cloned.b.c = 3;
console.log(obj.b.c); // 2
// 深度合并
const merged = merge(
{ dialog: { modal: true } },
{ dialog: { draggable: false } }
);
console.log(merged); // { dialog: { modal: true, draggable: false } }
// 移除字段
const safeConfig = omit(
{
routes: [],
defaultViews: {},
homeMetaOverrides: { keepAlive: true }
},
['routes', 'defaultViews']
);
console.log(safeConfig); // { homeMetaOverrides: { keepAlive: true } }
// 格式化货币
const formatted = formatCurrency(1234.56);
console.log(formatted); // "¥1,234.56"主要方法
| 方法 | 说明 |
|------|------|
| uniqueArray(arr) | 数组去重 |
| cloneDeep(obj) | 深拷贝对象 |
| deepClone(obj) | cloneDeep 的兼容别名 |
| merge(target, ...sources) | 深度合并对象 |
| omit(obj, keys) | 返回移除指定字段后的新对象 |
| formatCurrency(amount, locale?, currency?) | 格式化货币 |
依赖
- node-forge: ^1.3.1 - 加密解密功能支持
许可证
GPL-3.0-only
作者
Charles Chan [email protected]
