Skip to content

Commit 33434dc

Browse files
Chore/minor improvements (#6)
* enabled `sourcemap` * feat: improved stream encrypt/decrypt it's now possible to retrieve `crypto.Cipher`/`crypto.Decipher` before data processing. * wip: updating unit tests * tests: updated unit tests * tests: add missing unit tests branch coverage * docs: updated docs
1 parent a80fde1 commit 33434dc

File tree

9 files changed

+247
-74
lines changed

9 files changed

+247
-74
lines changed

README.md

Lines changed: 81 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -299,9 +299,11 @@ Encrypts a `Readable` stream to a `Writable` stream.
299299

300300
<summary>Returns</summary>
301301

302-
Type: `Promise<void>`
302+
Type: `Cph.Stream.Symmetric.EncryptReturnType`
303303

304-
- A new Promise that resolves when the stream encryption is complete.
304+
- An object containing:
305+
- a new instance of `crypto.Cipher` allowing you to add listeners to the `cipher` encryption process.
306+
- the actual `encrypt` callback that must be called and awaited in order to start the encryption process.
305307

306308
</details>
307309

@@ -335,9 +337,11 @@ Decrypts a `Readable` stream to a `Writable` stream.
335337

336338
<summary>Returns</summary>
337339

338-
Type: `Promise<void>`
340+
Type: `Promise<Cph.Stream.Symmetric.DecryptReturnType>`
339341

340-
- A new Promise that resolves when the stream decryption is complete.
342+
- A new Promise that resolves when Key IV extraction completes returning an object containing:
343+
- a new instance of `crypto.Decipher` allowing you to add listeners to the `decipher` decryption process.
344+
- the actual `decrypt` callback that must be called and awaited in order to start the decryption process.
341345

342346
</details>
343347

@@ -372,9 +376,11 @@ Encrypts a stream using hybrid encryption (symmetric + RSA).
372376

373377
<summary>Returns</summary>
374378

375-
Type: `Promise<void>`
379+
Type: `Cph.Stream.Hybrid.EncryptReturnType`
376380

377-
- A new Promise that resolves when hybrid encryption is complete.
381+
- An object containing:
382+
- a new instance of `cipher` allowing you to add listeners to the `cipher` encryption process.
383+
- the actual `encrypt` callback that must be called and awaited to start the encryption process.
378384

379385
</details>
380386

@@ -408,9 +414,11 @@ Decrypts a stream using hybrid decryption (symmetric + RSA).
408414

409415
<summary>Returns</summary>
410416

411-
Type: `Promise<void>`
417+
Type: `Promise<Cph.Stream.Hybrid.DecryptReturnType>`
412418

413-
- A new Promise that resolves when hybrid decryption is complete.
419+
- A new Promise that resolves when Key IV extraction completes returning an object containing:
420+
- a new instance of `crypto.Decipher` allowing you to add listeners to the `decipher` decryption process.
421+
- the actual `decrypt` callback that must be called and awaited in order to start the decryption process.
414422

415423
</details>
416424

@@ -496,6 +504,23 @@ Stream symmetric encryption options.
496504

497505
---
498506

507+
##### `Cph.Stream.Symmetric.EncryptReturnType`
508+
509+
Returnign object from `Cipher.streamEncrypt()` method.
510+
511+
<details>
512+
513+
<summary>Properties</summary>
514+
515+
| Property | Type | Description |
516+
|----------|----------|-------------|
517+
| `cipher` | `crypto.Cipher` | The `crypto.Cipher` instance. |
518+
| `encrypt` | `() => Promise<void>` | The actual `encrypt` callback that must be called and awaited in order to start the encryption process. |
519+
520+
</details>
521+
522+
---
523+
499524
##### `Cph.Stream.Symmetric.DecryptOptions`
500525

501526
Stream symmetric decryption options.
@@ -515,6 +540,23 @@ Stream symmetric decryption options.
515540

516541
---
517542

543+
##### `Cph.Stream.Symmetric.DecryptReturnType`
544+
545+
Returnign object from awaited `Cipher.streamDecrypt()` method.
546+
547+
<details>
548+
549+
<summary>Properties</summary>
550+
551+
| Property | Type | Description |
552+
|----------|----------|-------------|
553+
| `decipher` | `crypto.Decipher` | The `crypto.Decipher` instance. |
554+
| `decrypt` | `() => Promise<void>` | The actual `decrypt` callback that must be called and awaited in order to start the decryption process. |
555+
556+
</details>
557+
558+
---
559+
518560
##### `Cph.Stream.Hybrid.EncryptOptions`
519561

520562
Stream hybrid encryption options.
@@ -523,6 +565,14 @@ Stream hybrid encryption options.
523565

524566
---
525567

568+
##### `Cph.Stream.Hybrid.EncryptReturnType`
569+
570+
Returnign object from `Cipher.hybridEncrypt()` method.
571+
572+
- Alias for [`Cph.Stream.Symmetric.EncryptReturnType`](#cphstreamsymmetricencryptreturntype)
573+
574+
---
575+
526576
##### `Cph.Stream.Hybrid.DecryptOptions`
527577

528578
Stream hybrid decryption options.
@@ -541,6 +591,14 @@ Stream hybrid decryption options.
541591

542592
---
543593

594+
##### `Cph.Stream.Hybrid.DecryptReturnType`
595+
596+
Returnign object from awaited `Cipher.hybridDecrypt()` method.
597+
598+
- Alias for [`Cph.Stream.Symmetric.DecryptReturnType`](#cphstreamsymmetricdecryptreturntype)
599+
600+
---
601+
544602
### Examples
545603

546604
#### Importing the library
@@ -609,6 +667,7 @@ const routeHandler = () => {
609667
} )
610668

611669
Cipher.streamEncrypt( password, { input, output } )
670+
.encrypt()
612671

613672
return (
614673
// encrypted stream
@@ -664,7 +723,9 @@ const routeHandler = () => (
664723
},
665724
} )
666725

667-
Cipher.streamDecrypt( password, { input, output } )
726+
const { decrypt } = await Cipher.streamDecrypt( password, { input, output } )
727+
728+
decrypt()
668729

669730
return (
670731
// decrypted stream
@@ -729,12 +790,13 @@ const output = new Writable( {
729790
}
730791
} )
731792

732-
await Cipher.hybridEncrypt( password, {
793+
const { encrypt } = Cipher.hybridEncrypt( password, {
733794
key : keyPair.publicKey,
734795
padding : crypto.constants.RSA_PKCS1_OAEP_PADDING,
735796
oaepHash : 'SHA-256',
736797
}, { input, output } )
737798

799+
await encrypt()
738800
```
739801

740802
---
@@ -762,7 +824,7 @@ const output = new Writable( {
762824
},
763825
} )
764826

765-
await Cipher.hybridDecrypt(
827+
const { decrypt } = await Cipher.hybridDecrypt(
766828
{
767829
key : keyPair.privateKey,
768830
passphrase: password, // optional passhrase (required if set while generating keypair).
@@ -771,6 +833,8 @@ await Cipher.hybridDecrypt(
771833
}, { input, output, rsaKeyLength }
772834
)
773835

836+
await decrypt()
837+
774838
console.log( Buffer.concat( chunks ).toString() ) // Outputs: 'my top-secret data'
775839
```
776840

@@ -793,6 +857,7 @@ const input = fs.createReadStream( 'my-very-large-top-secret-file.pdf' )
793857
const output = fs.createWriteStream( 'my-very-large-top-secret-file.encrypted' )
794858
// encrypt
795859
await Cipher.streamEncrypt( password, { input, output } )
860+
.encrypt()
796861
```
797862

798863
---
@@ -809,7 +874,8 @@ const input = fs.createReadStream( 'my-very-large-top-secret-file.encrypted' )
809874
// output where decrypted data is written
810875
const output = fs.createWriteStream( 'my-very-large-top-secret-file-decrypted.pdf' )
811876
// decrypt
812-
await Cipher.streamDecrypt( password, { input, output } )
877+
const { decrypt } = await Cipher.streamDecrypt( password, { input, output } )
878+
await decrypt()
813879
```
814880

815881
---
@@ -852,11 +918,12 @@ const input = fs.createReadStream( 'my-very-large-top-secret-file.pdf' )
852918
// output where encrypted data is written
853919
const output = fs.createWriteStream( 'my-very-large-top-secret-file.encrypted' )
854920
// encrypt
855-
await Cipher.hybridEncrypt( password, {
921+
const { encrypt } = Cipher.hybridEncrypt( password, {
856922
key : keyPair.publicKey,
857923
padding : crypto.constants.RSA_PKCS1_OAEP_PADDING,
858924
oaepHash : 'SHA-256',
859925
}, { input, output } )
926+
await encrypt()
860927
```
861928

862929
---
@@ -873,7 +940,7 @@ const input = fs.createReadStream( 'my-very-large-top-secret-file.encrypted' )
873940
// output where decrypted data is written
874941
const output = fs.createWriteStream( 'my-very-large-top-secret-file-decrypted.pdf' )
875942
// decrypt
876-
await Cipher.hybridDecrypt(
943+
const { decrypt } = await Cipher.hybridDecrypt(
877944
{
878945
key : keyPair.privateKey,
879946
passphrase: password, // optional passhrase (required if set while generating keypair).

__tests__/file-hybrid.test.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,14 @@ describe( 'Cipher - In-Memory Stream Hybrid Encryption/Decryption', () => {
5555
// output where encrypted data is written
5656
const output = fs.createWriteStream( encryptedPath )
5757
// encrypt
58-
await Cipher.hybridEncrypt( password, {
58+
const { encrypt } = Cipher.hybridEncrypt( password, {
5959
key : keyPair.publicKey,
6060
padding : crypto.constants.RSA_PKCS1_OAEP_PADDING,
6161
oaepHash : 'SHA-256',
6262
}, { input, output } )
6363

64+
await encrypt()
65+
6466
const encrypted = fs.readFileSync( encryptedPath )
6567

6668
expect( bufferEquals( encrypted, Buffer.from( dataToEncrypt ) ) )
@@ -76,14 +78,16 @@ describe( 'Cipher - In-Memory Stream Hybrid Encryption/Decryption', () => {
7678
// output where decrypted data is written
7779
const output = fs.createWriteStream( decryptedPath )
7880
// decrypt
79-
await Cipher.hybridDecrypt(
81+
const { decrypt } = await Cipher.hybridDecrypt(
8082
{
8183
key : keyPair.privateKey,
8284
passphrase : password,
8385
padding : crypto.constants.RSA_PKCS1_OAEP_PADDING,
8486
oaepHash : 'SHA-256',
8587
}, { input, output, rsaKeyLength: rsaBytes }
8688
)
89+
90+
await decrypt()
8791

8892
const decrypted = fs.readFileSync( decryptedPath )
8993

__tests__/file-symmetric.test.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,9 @@ describe( 'Cipher - File Based Stream Symmetric Encryption/Decryption', () => {
4646
// output where encrypted data is written
4747
const output = fs.createWriteStream( encryptedPath )
4848
// encrypt
49-
await Cipher.streamEncrypt( password, { input, output } )
49+
const { encrypt } = Cipher.streamEncrypt( password, { input, output } )
50+
51+
await encrypt()
5052

5153
const encrypted = fs.readFileSync( encryptedPath )
5254

@@ -61,8 +63,10 @@ describe( 'Cipher - File Based Stream Symmetric Encryption/Decryption', () => {
6163
// output where decrypted data is written
6264
const output = fs.createWriteStream( decryptedPath )
6365
// decrypt
64-
await Cipher.streamDecrypt( password, { input, output } )
66+
const { decrypt } = await Cipher.streamDecrypt( password, { input, output } )
6567

68+
await decrypt()
69+
6670
const decrypted = fs.readFileSync( decryptedPath )
6771

6872
expect( bufferEquals( decrypted, Buffer.from( dataToEncrypt ) ) )

0 commit comments

Comments
 (0)