@kaadon.com/peertopeer
v0.0.2
Published
WebAssembly-based peer-to-peer cryptographic library with encryption/decryption capabilities for secure communication
Maintainers
Readme
@kaadon.com/peertopeer
基于 WebAssembly 的高性能点对点加密通信库,为点对点应用程序提供端到端加密功能和公钥密码学支持。
特性
- 🔐 端到端加密 - 基于公钥密码学的安全通信
- ⚡ WebAssembly 性能 - 高效的密码学运算
- 🌐 跨平台支持 - 支持 Node.js、浏览器、各种框架
- 📦 零依赖 - 纯 WebAssembly 实现
- 🔑 密钥交换 - 安全的消息加密/解密
- 🛡️ 内存安全 - 基于 Rust 的 WebAssembly 后端
php后端
php后端可以结合 @kaadon.com/peertopeer使用,实现点对点加密通信
通过 Composer 安装:
composer require kaadon/peertopeer安装
npm install @kaadon.com/peertopeer快速开始
import init, {
get_public_key,
set_remote_pubkey,
encrypt_message,
decrypt_message,
encrypt_with_remote_pubkey,
decrypt_with_remote_pubkey
} from '@kaadon.com/peertopeer';
// 初始化 WebAssembly 模块
await init();
// 生成公钥
const myPublicKey = get_public_key();
console.log('我的公钥:', myPublicKey);
// 使用远程公钥加密消息
const remotePublicKey = 'base64-encoded-remote-public-key';
const message = '你好,安全世界!';
const encrypted = encrypt_with_remote_pubkey(remotePublicKey, message);
console.log('加密结果:', encrypted);
// 解密消息
const decrypted = decrypt_with_remote_pubkey(
remotePublicKey,
encrypted.iv,
encrypted.ciphertext
);
console.log('解密结果:', decrypted);使用示例
基础点对点通信
import init, { get_public_key, encrypt_with_remote_pubkey, decrypt_with_remote_pubkey } from '@kaadon.com/peertopeer';
// 初始化 WASM
await init();
// 节点 A 生成公钥
const peerAPublicKey = get_public_key();
// 节点 B 生成公钥
const peerBPublicKey = get_public_key();
// 节点 A 为节点 B 加密消息
const message = '来自 A 的秘密消息';
const encrypted = encrypt_with_remote_pubkey(peerBPublicKey, message);
// 节点 B 解密来自节点 A 的消息
const decrypted = decrypt_with_remote_pubkey(
peerAPublicKey,
encrypted.iv,
encrypted.ciphertext
);
console.log('原始消息:', message);
console.log('解密消息:', decrypted); // 应该与原始消息匹配会话模式通信
import init, { get_public_key, set_remote_pubkey, encrypt_message, decrypt_message } from '@kaadon.com/peertopeer';
await init();
// 获取您的公钥
const myPublicKey = get_public_key();
// 设置远程节点的公钥用于会话
const remotePeerKey = 'base64-remote-public-key';
set_remote_pubkey(remotePeerKey);
// 现在您可以在此会话中加密/解密消息
const message = '来自安全会话的问候!';
const encrypted = encrypt_message(message);
// 解密接收到的消息
const decrypted = decrypt_message(encrypted.iv, encrypted.ciphertext);Vue.js 集成
<template>
<div>
<p>我的公钥: {{ publicKey }}</p>
<input v-model="message" placeholder="输入消息" />
<button @click="encryptMessage">加密</button>
<p v-if="encrypted">加密结果: {{ encrypted }}</p>
</div>
</template>
<script>
import init, { get_public_key, encrypt_with_remote_pubkey } from '@kaadon.com/peertopeer';
export default {
data() {
return {
publicKey: '',
message: '',
encrypted: null
};
},
async mounted() {
await init();
this.publicKey = get_public_key();
},
methods: {
encryptMessage() {
const remoteKey = 'remote-peer-public-key';
this.encrypted = encrypt_with_remote_pubkey(remoteKey, this.message);
}
}
};
</script>React 集成
import React, { useState, useEffect } from 'react';
import init, { get_public_key, encrypt_with_remote_pubkey } from '@kaadon.com/peertopeer';
function SecureMessaging() {
const [publicKey, setPublicKey] = useState('');
const [message, setMessage] = useState('');
const [encrypted, setEncrypted] = useState(null);
useEffect(() => {
const initWasm = async () => {
await init();
setPublicKey(get_public_key());
};
initWasm();
}, []);
const handleEncrypt = () => {
const remoteKey = 'remote-peer-public-key';
const result = encrypt_with_remote_pubkey(remoteKey, message);
setEncrypted(result);
};
return (
<div>
<p>我的公钥: {publicKey}</p>
<input
value={message}
onChange={(e) => setMessage(e.target.value)}
placeholder="输入消息"
/>
<button onClick={handleEncrypt}>加密</button>
{encrypted && <p>加密结果: {JSON.stringify(encrypted)}</p>}
</div>
);
}API 参考
初始化
init(module?: InitInput): Promise<InitOutput>
初始化 WebAssembly 模块,必须在使用任何其他函数之前调用。
await init(); // 使用默认 WASM 文件初始化
// 或
await init({ module_or_path: '/path/to/peer_to_peer_bg.wasm' });密钥管理
get_public_key(): string
生成并返回您的公钥(base64 编码字符串)。
const publicKey = get_public_key();set_remote_pubkey(remote_pub_b64: string): void
为基于会话的加密设置远程节点的公钥。
set_remote_pubkey('base64-encoded-remote-public-key');加密/解密
encrypt_message(plaintext: string): EncryptedMessage
使用之前设置的远程公钥加密消息。
const encrypted = encrypt_message('Hello World');
// 返回: { iv: string, ciphertext: string }decrypt_message(iv_b64: string, ciphertext_b64: string): string
使用您的私钥和发送方的公钥解密消息。
const decrypted = decrypt_message(encrypted.iv, encrypted.ciphertext);encrypt_with_remote_pubkey(remote_pub_b64: string, plaintext: string): EncryptedMessage
使用指定的远程公钥加密消息(一次性加密)。
const encrypted = encrypt_with_remote_pubkey(
'base64-remote-public-key',
'秘密消息'
);decrypt_with_remote_pubkey(remote_pub_b64: string, iv_b64: string, ciphertext_b64: string): string
使用指定的远程公钥解密消息(一次性解密)。
const decrypted = decrypt_with_remote_pubkey(
'base64-remote-public-key',
encrypted.iv,
encrypted.ciphertext
);TypeScript 支持
此库包含完整的 TypeScript 类型定义:
interface EncryptedMessage {
iv: string;
ciphertext: string;
}
declare function get_public_key(): string;
declare function set_remote_pubkey(remote_pub_b64: string): void;
declare function encrypt_message(plaintext: string): EncryptedMessage;
declare function decrypt_message(iv_b64: string, ciphertext_b64: string): string;
declare function encrypt_with_remote_pubkey(remote_pub_b64: string, plaintext: string): EncryptedMessage;
declare function decrypt_with_remote_pubkey(remote_pub_b64: string, iv_b64: string, ciphertext_b64: string): string;浏览器兼容性
- ✅ Chrome 57+
- ✅ Firefox 52+
- ✅ Safari 11+
- ✅ Edge 16+
- ❌ Internet Explorer(不支持 WebAssembly)
Node.js 兼容性
- ✅ Node.js 14.0.0+
- ✅ ES 模块和 CommonJS 支持
构建工具支持
Vite
// vite.config.js
export default {
server: {
fs: {
allow: ['..']
}
}
};Webpack
// webpack.config.js
module.exports = {
experiments: {
asyncWebAssembly: true
}
};Next.js
// next.config.js
module.exports = {
webpack: (config) => {
config.experiments = { asyncWebAssembly: true };
return config;
}
};安全说明
- 🔒 所有密码学操作都在 WebAssembly 中执行,增强安全性
- 🛡️ 使用安全的随机数生成器生成密钥
- ⚠️ 使用前务必验证公钥的有效性
- 🔄 对于长期通信,请考虑实施密钥轮换
- 📡 本库仅处理加密/解密 - 请单独实现安全的密钥交换
性能
- 加密: ~0.1ms 每条消息(典型值)
- 解密: ~0.1ms 每条消息(典型值)
- 密钥生成: ~1ms 每个密钥对
- 内存使用: ~128KB WASM 模块 + 最小运行时开销
错误处理
try {
await init();
const publicKey = get_public_key();
const encrypted = encrypt_message('test');
} catch (error) {
console.error('加密失败:', error);
// 处理初始化或加密错误
}贡献
欢迎贡献!请阅读我们的贡献指南并向我们的 GitHub 仓库提交拉取请求。
许可证
本项目基于 MIT 许可证 - 详见 LICENSE 文件。
支持
由 kaadon.com ❤️ 制作
