Skip to content

Commit f1b0dea

Browse files
authored
Merge pull request #12 from YeeZTech/browser
Add browser crypto implementation
2 parents a2169be + d8c0810 commit f1b0dea

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+5155
-83
lines changed

README.md

Lines changed: 119 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -31,55 +31,71 @@ yarn install
3131
yarn test
3232
```
3333

34+
#### 快速开始
35+
36+
```js
37+
import { YPCCrypto } from "@yeez-tech/meta-encryptor";
38+
39+
// YPCCrypto 是单例对象(Node / Browser / SSR 通用)
40+
// 不需要、也不能调用 ()
41+
const crypto = YPCCrypto;
42+
43+
// 生成密钥对
44+
const sKey = crypto.generatePrivateKey();
45+
const pKey = crypto.generatePublicKeyFromPrivateKey(sKey);
46+
47+
// 生成文件名和文件内容
48+
const fileName = crypto.generateFileNameFromPKey(pKey);
49+
const fileContent = crypto.generateFileContentFromSKey(sKey);
50+
51+
// 解密消息(浏览器环境需要 await)
52+
await crypto.decryptMessage(sKey, encryptedMessage);
53+
```
54+
3455
#### API
3556

36-
##### crypto.generatePrivateKey
57+
##### YPCCrypto.generatePrivateKey
3758

3859
生成私钥
3960

4061
```js
41-
import {crypto} from '@yeez-tech/meta-encryptor';
62+
import { YPCCrypto } from "@yeez-tech/meta-encryptor";
4263

64+
const crypto = YPCCrypto;
4365
const sKey = crypto.generatePrivateKey();
44-
45-
console.log('私钥=', sKey);
46-
const pKey = meta.crypto.generatePublicKeyFromPrivateKey(sKey);
47-
useStore().commit(ConfigMutationTypes.SET_ENCRYPTION_CONFIG, {
48-
privateKey: sKey.toString('hex'),
49-
publicKey: pKey.toString('hex')
50-
});
51-
const ypcName = meta.crypto.generateFileNameFromPKey(pKey);
52-
const ypcJson = meta.crypto.generateFileContentFromSKey(sKey);
5366
```
5467

55-
##### crypto.generatePublicKeyFromPrivateKey
68+
##### YPCCrypto.generatePublicKeyFromPrivateKey
5669

5770
通过私钥生成公钥
5871

5972
```js
60-
import {crypto} from '@yeez-tech/meta-encryptor';
73+
import { YPCCrypto } from "@yeez-tech/meta-encryptor";
74+
75+
const crypto = YPCCrypto;
6176
const pKey = crypto.generatePublicKeyFromPrivateKey(sKey);
62-
console.log('公钥钥=', pKey);
6377
```
6478

65-
##### crypto.generateFileNameFromPKey
79+
##### YPCCrypto.generateFileNameFromPKey
6680

6781
通过公钥生成文件名
6882

6983
```js
70-
import {crypto} from '@yeez-tech/meta-encryptor';
71-
const ypcName = crypto.generateFileNameFromPKey(pKey);
72-
console.log('文件名=', ypcName);
84+
import { YPCCrypto } from "@yeez-tech/meta-encryptor";
85+
86+
const crypto = YPCCrypto;
87+
const fileName = crypto.generateFileNameFromPKey(pKey);
7388
```
7489

75-
##### crypto.generateFileContentFromSKey
90+
##### YPCCrypto.generateFileContentFromSKey
7691

7792
通过私钥获取密钥文件内容
7893

7994
```js
80-
import {crypto} from '@yeez-tech/meta-encryptor';
81-
const ypcJson = crypto.generateFileContentFromSKey(sKey);
82-
console.log('文件内容=', ypcJson);
95+
import { YPCCrypto } from "@yeez-tech/meta-encryptor";
96+
97+
const crypto = YPCCrypto;
98+
const fileContent = crypto.generateFileContentFromSKey(sKey);
8399
```
84100

85101
##### Sealer
@@ -103,7 +119,7 @@ rs.pipe(csv())
103119
Unsealer 用来解密流,并且将结果输出到流.
104120
105121
```js
106-
import {Sealer, Unsealer, SealedFileStream} from '@yeez-tech/meta-encryptor';
122+
import { Sealer, Unsealer, SealedFileStream } from "@yeez-tech/meta-encryptor";
107123

108124
/*
109125
let src = "./tsconfig.json"
@@ -119,13 +135,13 @@ await new Promise(resolve=>{
119135
});
120136
*/
121137

122-
let unsealer = new Unsealer({keyPair: key_pair});
138+
let unsealer = new Unsealer({ keyPair: key_pair });
123139
let rrs = new SealedFileStream(dst);
124-
let wws = fs.createWriteStream(src + '.new');
140+
let wws = fs.createWriteStream(src + ".new");
125141

126142
rrs.pipe(unsealer).pipe(wws);
127143
await new Promise((resolve) => {
128-
wws.on('finish', () => resolve());
144+
wws.on("finish", () => resolve());
129145
});
130146
```
131147
@@ -134,17 +150,53 @@ await new Promise((resolve) => {
134150
用于判断一个文件是否为一个有效的封装文件,如果为真,返回`true`,否则,返回`false`
135151
136152
```js
137-
import {isSealedFile} from '@yeez-tech/meta-encryptor';
153+
import { isSealedFile } from "@yeez-tech/meta-encryptor";
138154

139155
let r = isSealedFile(path);
140156
```
141157
158+
#### 浏览器环境支持
159+
160+
本库同时支持 Node.js 和浏览器环境,使用方法完全一致。
161+
162+
```js
163+
import { YPCCrypto } from '@yeez-tech/meta-encryptor'
164+
165+
// YPCCrypto 是单例对象(Node / Browser / SSR 通用)
166+
// 不需要、也不能调用 ()
167+
const crypto = YPCCrypto
168+
169+
await crypto.decryptMessage(...)
170+
```
171+
172+
**注意**:
173+
174+
- 库会自动检测运行环境并使用对应的实现(Node.js 或浏览器)
175+
- 由于 Web Crypto API 的限制,浏览器环境中部分方法(如 `decryptMessage``_encryptMessage` 等)是异步的,需要使用 `await` 调用
176+
177+
##### 浏览器端下载功能
178+
179+
在浏览器环境中,可以使用 `downloadUnsealed` 直接下载并解密文件:
180+
181+
```js
182+
import { downloadUnsealed } from "@yeez-tech/meta-encryptor";
183+
184+
await downloadUnsealed({
185+
url: "https://example.com/encrypted file",
186+
privateKeyHex: "YOUR_PRIVATE_KEY_HEX",
187+
filename: "decrypted-file.txt",
188+
progressHandler: (totalItems, processedItems, readBytes, writeBytes) => {
189+
console.log(`Progress: ${processedItems}/${totalItems}`);
190+
},
191+
});
192+
```
193+
142194
##### sealedFileVersion
143195
144196
返回封装文件的版本号。
145197
146198
```js
147-
import {sealedFileVersion} from '@yeez-tech/meta-encryptor';
199+
import { sealedFileVersion } from "@yeez-tech/meta-encryptor";
148200

149201
let r = sealedFileVersion(path);
150202
```
@@ -154,7 +206,7 @@ let r = sealedFileVersion(path);
154206
返回封装文件对应的原始数据的 hash。注意,该函数直接读取的是记录在文件头的 hash,如果文件被篡改,该函数有可能返回错误的 hash,因此,如果有可能,应该在解密之后,对 hash 进行校验。
155207
156208
```js
157-
import {dataHashOfSealedFile} from '@yeez-tech/meta-encryptor';
209+
import { dataHashOfSealedFile } from "@yeez-tech/meta-encryptor";
158210

159211
let r = dataHashOfSealedFile(path);
160212
```
@@ -164,7 +216,7 @@ let r = dataHashOfSealedFile(path);
164216
对数据 hash 进行签名。
165217
166218
```js
167-
import {signedDataHash} from '@yeez-tech/meta-encryptor';
219+
import { signedDataHash } from "@yeez-tech/meta-encryptor";
168220

169221
//keyPair应该是{'private-key':'hex string of private key',
170222
//dataHash应该是一个Buffer,长度为32字节
@@ -176,7 +228,7 @@ let r = signedDataHash(keyPair, dataHash);
176228
生成转发枢私钥的信息。
177229
178230
```js
179-
import {forwardSkey} from '@yeez-tech/meta-encryptor';
231+
import { forwardSkey } from "@yeez-tech/meta-encryptor";
180232

181233
//keyPair应该是{'private-key':'hex string of private key',
182234
//dianPKey应该是一个Buffer,包含了典公钥,
@@ -202,10 +254,10 @@ meta-encryptor 提供了支持断点续传的可恢复流功能,主要包含
202254
用于支持断点续传的读取流,可以从指定位置恢复读取。
203255
204256
```js
205-
import {RecoverableReadStream} from '@yeez-tech/meta-encryptor';
257+
import { RecoverableReadStream } from "@yeez-tech/meta-encryptor";
206258

207-
const context = new PipelineContextInFile('context.dat');
208-
const readStream = new RecoverableReadStream('input.file', context);
259+
const context = new PipelineContextInFile("context.dat");
260+
const readStream = new RecoverableReadStream("input.file", context);
209261

210262
readStream.pipe(someWriteStream);
211263
```
@@ -215,10 +267,10 @@ readStream.pipe(someWriteStream);
215267
用于支持断点续传的写入流,可以从指定位置恢复写入。
216268
217269
```js
218-
import {RecoverableWriteStream} from '@yeez-tech/meta-encryptor';
270+
import { RecoverableWriteStream } from "@yeez-tech/meta-encryptor";
219271

220-
const context = new PipelineContextInFile('context.dat');
221-
const writeStream = new RecoverableWriteStream('output.file', context);
272+
const context = new PipelineContextInFile("context.dat");
273+
const writeStream = new RecoverableWriteStream("output.file", context);
222274

223275
someReadStream.pipe(writeStream);
224276
```
@@ -228,16 +280,16 @@ someReadStream.pipe(writeStream);
228280
用于管理断点续传过程中的上下文信息的基类。
229281
230282
```js
231-
import {PipelineContext} from '@yeez-tech/meta-encryptor';
283+
import { PipelineContext } from "@yeez-tech/meta-encryptor";
232284

233285
class MyContext extends PipelineContext {
234-
saveContext() {
235-
// 实现保存上下文的逻辑
236-
}
286+
saveContext() {
287+
// 实现保存上下文的逻辑
288+
}
237289

238-
loadContext() {
239-
// 实现加载上下文的逻辑
240-
}
290+
loadContext() {
291+
// 实现加载上下文的逻辑
292+
}
241293
}
242294
```
243295
@@ -246,9 +298,9 @@ class MyContext extends PipelineContext {
246298
基于文件存储的上下文管理实现,支持二进制数据。
247299
248300
```js
249-
import {PipelineContextInFile} from '@yeez-tech/meta-encryptor';
301+
import { PipelineContextInFile } from "@yeez-tech/meta-encryptor";
250302

251-
const context = new PipelineContextInFile('context.dat');
303+
const context = new PipelineContextInFile("context.dat");
252304

253305
// 保存上下文
254306
await context.saveContext();
@@ -260,21 +312,25 @@ await context.loadContext();
260312
使用示例:
261313
262314
```js
263-
import {RecoverableReadStream, RecoverableWriteStream, PipelineContextInFile} from '@yeez-tech/meta-encryptor';
315+
import {
316+
RecoverableReadStream,
317+
RecoverableWriteStream,
318+
PipelineContextInFile,
319+
} from "@yeez-tech/meta-encryptor";
264320

265321
// 创建上下文管理器
266-
const context = new PipelineContextInFile('transfer.context');
322+
const context = new PipelineContextInFile("transfer.context");
267323

268324
// 创建可恢复的读写流
269-
const readStream = new RecoverableReadStream('source.file', context);
270-
const writeStream = new RecoverableWriteStream('target.file', context);
325+
const readStream = new RecoverableReadStream("source.file", context);
326+
const writeStream = new RecoverableWriteStream("target.file", context);
271327

272328
// 处理传输
273329
readStream.pipe(writeStream);
274330

275331
// 如果传输中断,可以使用相同的上下文重新创建流来继续传输
276-
const resumeReadStream = new RecoverableReadStream('source.file', context);
277-
const resumeWriteStream = new RecoverableWriteStream('target.file', context);
332+
const resumeReadStream = new RecoverableReadStream("source.file", context);
333+
const resumeWriteStream = new RecoverableWriteStream("target.file", context);
278334
resumeReadStream.pipe(resumeWriteStream);
279335
```
280336
@@ -286,24 +342,24 @@ meta-encryptor 支持将可恢复流与 Unsealer 结合使用,实现加密文
286342
287343
```js
288344
import {
289-
RecoverableReadStream,
290-
RecoverableWriteStream,
291-
PipelineContextInFile,
292-
Unsealer
293-
} from '@yeez-tech/meta-encryptor';
345+
RecoverableReadStream,
346+
RecoverableWriteStream,
347+
PipelineContextInFile,
348+
Unsealer,
349+
} from "@yeez-tech/meta-encryptor";
294350

295351
// 创建上下文管理器
296-
const context = new PipelineContextInFile('context.dat');
352+
const context = new PipelineContextInFile("context.dat");
297353
await context.loadContext();
298354

299355
// 创建解密管道
300356
const readStream = new RecoverableReadStream(encryptedFile, context);
301357
const unsealer = new Unsealer({
302-
keyPair,
303-
context,
304-
progressHandler: (totalItem, readItem, bytes, writeBytes) => {
305-
console.log(`Progress: ${(bytes / (1024 * 1024)).toFixed(2)}MB`);
306-
}
358+
keyPair,
359+
context,
360+
progressHandler: (totalItem, readItem, bytes, writeBytes) => {
361+
console.log(`Progress: ${(bytes / (1024 * 1024)).toFixed(2)}MB`);
362+
},
307363
});
308364
const writeStream = new RecoverableWriteStream(decryptedFile, context);
309365

0 commit comments

Comments
 (0)