@@ -31,55 +31,71 @@ yarn install
3131yarn 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;
4365const 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;
6176const 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())
103119Unsealer 用来解密流,并且将结果输出到流.
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/*
109125let 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 });
123139let rrs = new SealedFileStream (dst);
124- let wws = fs .createWriteStream (src + ' .new' );
140+ let wws = fs .createWriteStream (src + " .new" );
125141
126142rrs .pipe (unsealer).pipe (wws);
127143await 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
139155let 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
149201let 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
159211let 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
210262readStream .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
223275someReadStream .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
233285class 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// 保存上下文
254306await 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// 处理传输
273329readStream .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);
278334resumeReadStream .pipe (resumeWriteStream);
279335` ` `
280336
@@ -286,24 +342,24 @@ meta-encryptor 支持将可恢复流与 Unsealer 结合使用,实现加密文
286342
287343` ` ` js
288344import {
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" );
297353await context .loadContext ();
298354
299355// 创建解密管道
300356const readStream = new RecoverableReadStream (encryptedFile, context);
301357const 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});
308364const writeStream = new RecoverableWriteStream (decryptedFile, context);
309365
0 commit comments