@alessiofrittoli/crypto-cipher 2.0.0 → 2.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +41 -38
- package/dist/index.d.mts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -2
- package/dist/index.mjs +1 -2
- package/dist/types.d.mts +5 -2
- package/dist/types.d.ts +5 -2
- package/dist/types.js +1 -2
- package/dist/types.mjs +0 -2
- package/package.json +40 -19
- package/dist/index.js.map +0 -1
- package/dist/index.mjs.map +0 -1
- package/dist/types.js.map +0 -1
- package/dist/types.mjs.map +0 -1
package/README.md
CHANGED
|
@@ -1,15 +1,22 @@
|
|
|
1
1
|
# Crypto Cipher 🔐
|
|
2
2
|
|
|
3
|
-
[![NPM Latest Version][version-badge]][npm-url] [![Coverage Status][coverage-badge]][coverage-url] [![NPM Monthly Downloads][downloads-badge]][npm-url] [![Dependencies][deps-badge]][deps-url]
|
|
3
|
+
[![NPM Latest Version][version-badge]][npm-url] [![Coverage Status][coverage-badge]][coverage-url] [![Socket Status][socket-badge]][socket-url] [![NPM Monthly Downloads][downloads-badge]][npm-url] [![Dependencies][deps-badge]][deps-url]
|
|
4
|
+
|
|
5
|
+
[![GitHub Sponsor][sponsor-badge]][sponsor-url]
|
|
4
6
|
|
|
5
7
|
[version-badge]: https://img.shields.io/npm/v/%40alessiofrittoli%2Fcrypto-cipher
|
|
6
8
|
[npm-url]: https://npmjs.org/package/%40alessiofrittoli%2Fcrypto-cipher
|
|
7
9
|
[coverage-badge]: https://coveralls.io/repos/github/alessiofrittoli/crypto-cipher/badge.svg
|
|
8
10
|
[coverage-url]: https://coveralls.io/github/alessiofrittoli/crypto-cipher
|
|
11
|
+
[socket-badge]: https://socket.dev/api/badge/npm/package/@alessiofrittoli/crypto-cipher
|
|
12
|
+
[socket-url]: https://socket.dev/npm/package/@alessiofrittoli/crypto-cipher/overview
|
|
9
13
|
[downloads-badge]: https://img.shields.io/npm/dm/%40alessiofrittoli%2Fcrypto-cipher.svg
|
|
10
14
|
[deps-badge]: https://img.shields.io/librariesio/release/npm/%40alessiofrittoli%2Fcrypto-cipher
|
|
11
15
|
[deps-url]: https://libraries.io/npm/%40alessiofrittoli%2Fcrypto-cipher
|
|
12
16
|
|
|
17
|
+
[sponsor-badge]: https://img.shields.io/static/v1?label=Fund%20this%20package&message=%E2%9D%A4&logo=GitHub&color=%23DB61A2
|
|
18
|
+
[sponsor-url]: https://github.com/sponsors/alessiofrittoli
|
|
19
|
+
|
|
13
20
|
## Node.js Cipher cryptograph utility library
|
|
14
21
|
|
|
15
22
|
### Table of Contents
|
|
@@ -994,16 +1001,7 @@ pnpm test:watch
|
|
|
994
1001
|
pnpm test:ci
|
|
995
1002
|
```
|
|
996
1003
|
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
```bash
|
|
1000
|
-
pnpm test:buffer-in-memory
|
|
1001
|
-
pnpm test:file-symmetric
|
|
1002
|
-
pnpm test:file-hybrid
|
|
1003
|
-
pnpm test:stream-symmetric
|
|
1004
|
-
pnpm test:stream-hybrid
|
|
1005
|
-
pnpm test:misc
|
|
1006
|
-
```
|
|
1004
|
+
- See [`package.json`](./package.json) file scripts for more info.
|
|
1007
1005
|
|
|
1008
1006
|
Run tests with coverage.
|
|
1009
1007
|
|
|
@@ -1019,9 +1017,14 @@ test:coverage:serve
|
|
|
1019
1017
|
|
|
1020
1018
|
### Contributing
|
|
1021
1019
|
|
|
1022
|
-
Contributions are truly welcome
|
|
1020
|
+
Contributions are truly welcome!
|
|
1021
|
+
|
|
1023
1022
|
Please refer to the [Contributing Doc](./CONTRIBUTING.md) for more information on how to start contributing to this project.
|
|
1024
1023
|
|
|
1024
|
+
Help keep this project up to date with [GitHub Sponsor][sponsor-url].
|
|
1025
|
+
|
|
1026
|
+
[![GitHub Sponsor][sponsor-badge]][sponsor-url]
|
|
1027
|
+
|
|
1025
1028
|
---
|
|
1026
1029
|
|
|
1027
1030
|
### Security
|
|
@@ -1031,30 +1034,30 @@ If you believe you have found a security vulnerability, we encourage you to **_r
|
|
|
1031
1034
|
### Made with ☕
|
|
1032
1035
|
|
|
1033
1036
|
<table style='display:flex;gap:20px;'>
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1037
|
+
<tbody>
|
|
1038
|
+
<tr>
|
|
1039
|
+
<td>
|
|
1040
|
+
<img alt="avatar" src='https://avatars.githubusercontent.com/u/35973186' style='width:60px;border-radius:50%;object-fit:contain;'>
|
|
1041
|
+
</td>
|
|
1042
|
+
<td>
|
|
1043
|
+
<table style='display:flex;gap:2px;flex-direction:column;'>
|
|
1044
|
+
<tbody>
|
|
1045
|
+
<tr>
|
|
1046
|
+
<td>
|
|
1047
|
+
<a href='https://github.com/alessiofrittoli' target='_blank' rel='noopener'>Alessio Frittoli</a>
|
|
1048
|
+
</td>
|
|
1049
|
+
</tr>
|
|
1050
|
+
<tr>
|
|
1051
|
+
<td>
|
|
1052
|
+
<small>
|
|
1053
|
+
<a href='https://alessiofrittoli.it' target='_blank' rel='noopener'>https://alessiofrittoli.it</a> |
|
|
1054
|
+
<a href='mailto:info@alessiofrittoli.it' target='_blank' rel='noopener'>info@alessiofrittoli.it</a>
|
|
1055
|
+
</small>
|
|
1056
|
+
</td>
|
|
1057
|
+
</tr>
|
|
1058
|
+
</tbody>
|
|
1059
|
+
</table>
|
|
1060
|
+
</td>
|
|
1061
|
+
</tr>
|
|
1062
|
+
</tbody>
|
|
1060
1063
|
</table>
|
package/dist/index.d.mts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import crypto from 'crypto';
|
|
2
2
|
import { CoerceToUint8ArrayInput } from '@alessiofrittoli/crypto-buffer/coercion';
|
|
3
3
|
import { Cph } from './types.mjs';
|
|
4
|
-
import '@alessiofrittoli/crypto-buffer';
|
|
5
4
|
import 'stream';
|
|
5
|
+
import '@alessiofrittoli/crypto-buffer';
|
|
6
6
|
|
|
7
7
|
/**
|
|
8
8
|
* Utility class for AES encryption and decryption following [NIST SP 800-38D](https://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38d.pdf) standard reccomendations.
|
package/dist/index.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import crypto from 'crypto';
|
|
2
2
|
import { CoerceToUint8ArrayInput } from '@alessiofrittoli/crypto-buffer/coercion';
|
|
3
3
|
import { Cph } from './types.js';
|
|
4
|
-
import '@alessiofrittoli/crypto-buffer';
|
|
5
4
|
import 'stream';
|
|
5
|
+
import '@alessiofrittoli/crypto-buffer';
|
|
6
6
|
|
|
7
7
|
/**
|
|
8
8
|
* Utility class for AES encryption and decryption following [NIST SP 800-38D](https://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38d.pdf) standard reccomendations.
|
package/dist/index.js
CHANGED
|
@@ -1,2 +1 @@
|
|
|
1
|
-
|
|
2
|
-
//# sourceMappingURL=index.js.map
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }var _crypto = require('crypto'); var _crypto2 = _interopRequireDefault(_crypto);var _stream = require('stream');var _coercion = require('@alessiofrittoli/crypto-buffer/coercion');var t=class t{static encrypt(r,e,o={}){let a=_coercion.coerceToUint8Array.call(void 0, r),{salt:c,Key:s,IV:n,AAD:p,options:{algorithm:i,authTag:h}}=t.newKeyIV(e,o);if(t.isCBC(i)){let T=_crypto2.default.createCipheriv(i,s,n),d=Buffer.concat([T.update(a),T.final()]);return Buffer.concat([c,n,d])}let y=_crypto2.default.createCipheriv(i,s,n,{authTagLength:h});t.isCCM(i)&&y.setAAD(p,{plaintextLength:a.length}),(t.isGCM(i)||t.isOCB(i))&&y.setAAD(p);let u=Buffer.concat([y.update(a),y.final()]),l=y.getAuthTag();return Buffer.concat([c,n,p,l,u])}static decrypt(r,e,o={}){let a=_coercion.coerceToUint8Array.call(void 0, r),c=_coercion.coerceToUint8Array.call(void 0, e),s=t.resolveOptions(o),{algorithm:n}=s,p=a.subarray(0,s.salt);a=a.subarray(s.salt);let i=a.subarray(0,s.iv);a=a.subarray(s.iv);let h=_crypto2.default.scryptSync(c,p,s.length);if(t.isCBC(n)){let f=_crypto2.default.createDecipheriv(s.algorithm,h,i);return Buffer.concat([f.update(a),f.final()])}let y=s.aadLength,u=s.authTag,l=a.subarray(0,y);a=a.subarray(y);let T=a.subarray(0,u);a=a.subarray(u);let d=_crypto2.default.createDecipheriv(n,h,i,{authTagLength:u});return t.isCCM(n)&&d.setAAD(l,{plaintextLength:a.length}),(t.isGCM(n)||t.isOCB(n))&&d.setAAD(l),d.setAuthTag(T),Buffer.concat([d.update(a),d.final()])}static streamEncrypt(r,e){e.algorithm||(e.algorithm=t.DEFAULT_ALGORITHM.stream);let{Key:o,IV:a,options:{input:c,output:s,algorithm:n,salt:p,iv:i,authTag:h}}=t.newKeyIV(r,e),y=t.encrypt(Buffer.concat([o,a]),r,{algorithm:n,salt:p,iv:i,authTag:h}),u=_crypto2.default.createCipheriv(n,o,a);return{cipher:u,encrypt:()=>t.stream({cipher:u,input:c,output:s,encryptedKey:y})}}static streamDecrypt(r,e){e.algorithm||(e.algorithm=t.DEFAULT_ALGORITHM.stream);let{input:o,output:a,algorithm:c,salt:s,iv:n,authTag:p,length:i}=t.resolveOptions(e),h=i+n+s+n+p;return t.extractKeyIV(o,h).then(([y,u])=>{if(u.errored)throw u.errored;let l=t.decrypt(y,r,{algorithm:c,salt:s,iv:n,authTag:p}),T=l.subarray(0,i),d=l.subarray(i),f=_crypto2.default.createDecipheriv(c,T,d);return{decipher:f,decrypt:()=>t.decipherStream({decipher:f,input:u,output:a})}})}static hybridEncrypt(r,e,o){o.algorithm||(o.algorithm=t.DEFAULT_ALGORITHM.stream);let{Key:a,IV:c,options:{algorithm:s,input:n,output:p}}=t.newKeyIV(r,o),i=_crypto2.default.publicEncrypt(e,Buffer.concat([a,c])),h=_crypto2.default.createCipheriv(s,a,c);return{cipher:h,encrypt:()=>t.stream({cipher:h,input:n,output:p,encryptedKey:i})}}static hybridDecrypt(r,e){e.algorithm||(e.algorithm=t.DEFAULT_ALGORITHM.stream);let{input:o,output:a,algorithm:c,length:s,rsaKeyLength:n}=t.resolveOptions(e);return t.extractKeyIV(o,n).then(([p,i])=>{let h=_crypto2.default.privateDecrypt(r,p),y=h.subarray(0,s),u=h.subarray(s),l=_crypto2.default.createDecipheriv(c,y,u);return{decipher:l,decrypt:()=>t.decipherStream({decipher:l,input:i,output:a})}})}static stream(r){let{cipher:e,encryptedKey:o,input:a,output:c}=r;return new Promise((s,n)=>{e.on("error",n),a.on("error",n),c.on("error",n),c.on("finish",s),c.write(o),a.pipe(e).pipe(c)})}static decipherStream(r){let{decipher:e,input:o,output:a}=r;return new Promise((c,s)=>{e.on("error",s),o.on("error",s),a.on("error",s),a.on("finish",c),o.pipe(e).pipe(a)})}static extractKeyIV(r,e){return new Promise((o,a)=>{let c=Buffer.alloc(0),s=0,n=!1,p=new (0, _stream.Transform)({async transform(i,h,y){if(c.length<e&&(c=Buffer.concat([c,i])),s+=i.length,c.length>e){let u=Math.max(0,c.length-e);c=c.subarray(0,e);let l=i.subarray(u*-1);return n||(o([c,this]),n=!0),this.push(l,h),y()}return s>e&&(n||(o([c,this]),n=!0),this.push(i,h)),y()},final(i){return c.length<e?i(new Error("The extracted KeyIV length is less than the expected length.")):i()}});p.on("error",a),r.on("error",i=>{p.destroy(i),a(i)}),r.pipe(p)})}static newKeyIV(r,e={}){let o=_coercion.coerceToUint8Array.call(void 0, r),a=t.resolveOptions(e),c=_crypto2.default.randomBytes(a.salt),s=_crypto2.default.scryptSync(o,c,a.length),n=_crypto2.default.randomBytes(a.iv),p=a.aad||_crypto2.default.scryptSync(s,c,a.aadLength);return{options:a,Key:s,IV:n,AAD:p,salt:c}}static resolveOptions(r={}){let e={...r};e.aad&&(e.aad=_coercion.coerceToUint8Array.call(void 0, e.aad)),e.salt||(e.salt=t.SALT_LENGTH.default),e.authTag||(e.authTag=t.AUTH_TAG_LENGTH.default),e.aadLength=_optionalChain([e, 'access', _ => _.aad, 'optionalAccess', _2 => _2.length])||e.aadLength||t.AAD_LENGTH.default,e.salt=Math.min(Math.max(e.salt,t.SALT_LENGTH.min),t.SALT_LENGTH.max),e.authTag=Math.min(Math.max(e.authTag,t.AUTH_TAG_LENGTH.min),t.AUTH_TAG_LENGTH.max),e.aad||(e.aadLength=Math.min(Math.max(e.aadLength,t.AAD_LENGTH.min),t.AAD_LENGTH.max));let{keyLength:o,algorithm:a}=t.getKeyLength(r.algorithm);return e.algorithm=a,e.length=o,e.iv=t.getIVLength(a,r),e}static getIVLength(r=t.DEFAULT_ALGORITHM.buffer,e={}){switch(r){case"aes-128-ccm":case"aes-128-ocb":case"aes-192-ccm":case"aes-192-ocb":case"aes-256-ccm":case"aes-256-ocb":return 8;case"chacha20-poly1305":return 12;default:return Math.min(Math.max(e.iv||t.IV_LENGTH.default,t.IV_LENGTH.min),t.IV_LENGTH.max)}}static getKeyLength(r=t.DEFAULT_ALGORITHM.buffer){switch(r){case"chacha20-poly1305":return{algorithm:r,keyLength:256/8};case"aes-128-cbc":case"aes-128-ccm":case"aes-128-gcm":case"aes-128-ocb":return{algorithm:r,keyLength:128/8};case"aes-192-cbc":case"aes-192-ccm":case"aes-192-gcm":case"aes-192-ocb":return{algorithm:r,keyLength:192/8};case"aes-256-cbc":case"aes-256-ccm":case"aes-256-gcm":case"aes-256-ocb":default:return{algorithm:r,keyLength:256/8}}}static isGCM(r){return r==="aes-128-gcm"||r==="aes-192-gcm"||r==="aes-256-gcm"}static isCCM(r){return r==="aes-128-ccm"||r==="aes-192-ccm"||r==="aes-256-ccm"||r==="chacha20-poly1305"}static isOCB(r){return r==="aes-128-ocb"||r==="aes-192-ocb"||r==="aes-256-ocb"}static isCBC(r){return r==="aes-128-cbc"||r==="aes-192-cbc"||r==="aes-256-cbc"}};t.SALT_LENGTH={min:16,max:64,default:32},t.IV_LENGTH={min:8,max:32,default:16},t.AUTH_TAG_LENGTH={min:4,max:16,default:16},t.AAD_LENGTH={min:16,max:128,default:32},t.DEFAULT_ALGORITHM={buffer:"aes-256-gcm",stream:"aes-256-cbc"},t.ALGORITHMS=["aes-128-cbc","aes-128-ccm","aes-128-gcm","aes-128-ocb","aes-192-cbc","aes-192-ccm","aes-192-gcm","aes-192-ocb","aes-256-cbc","aes-256-ccm","aes-256-gcm","aes-256-ocb","chacha20-poly1305"];var A=t;exports.Cipher = A;
|
package/dist/index.mjs
CHANGED
|
@@ -1,2 +1 @@
|
|
|
1
|
-
import m from
|
|
2
|
-
//# sourceMappingURL=index.mjs.map
|
|
1
|
+
import m from"crypto";import{Transform as C}from"stream";import{coerceToUint8Array as g}from"@alessiofrittoli/crypto-buffer/coercion";var t=class t{static encrypt(r,e,o={}){let a=g(r),{salt:c,Key:s,IV:n,AAD:p,options:{algorithm:i,authTag:h}}=t.newKeyIV(e,o);if(t.isCBC(i)){let T=m.createCipheriv(i,s,n),d=Buffer.concat([T.update(a),T.final()]);return Buffer.concat([c,n,d])}let y=m.createCipheriv(i,s,n,{authTagLength:h});t.isCCM(i)&&y.setAAD(p,{plaintextLength:a.length}),(t.isGCM(i)||t.isOCB(i))&&y.setAAD(p);let u=Buffer.concat([y.update(a),y.final()]),l=y.getAuthTag();return Buffer.concat([c,n,p,l,u])}static decrypt(r,e,o={}){let a=g(r),c=g(e),s=t.resolveOptions(o),{algorithm:n}=s,p=a.subarray(0,s.salt);a=a.subarray(s.salt);let i=a.subarray(0,s.iv);a=a.subarray(s.iv);let h=m.scryptSync(c,p,s.length);if(t.isCBC(n)){let f=m.createDecipheriv(s.algorithm,h,i);return Buffer.concat([f.update(a),f.final()])}let y=s.aadLength,u=s.authTag,l=a.subarray(0,y);a=a.subarray(y);let T=a.subarray(0,u);a=a.subarray(u);let d=m.createDecipheriv(n,h,i,{authTagLength:u});return t.isCCM(n)&&d.setAAD(l,{plaintextLength:a.length}),(t.isGCM(n)||t.isOCB(n))&&d.setAAD(l),d.setAuthTag(T),Buffer.concat([d.update(a),d.final()])}static streamEncrypt(r,e){e.algorithm||(e.algorithm=t.DEFAULT_ALGORITHM.stream);let{Key:o,IV:a,options:{input:c,output:s,algorithm:n,salt:p,iv:i,authTag:h}}=t.newKeyIV(r,e),y=t.encrypt(Buffer.concat([o,a]),r,{algorithm:n,salt:p,iv:i,authTag:h}),u=m.createCipheriv(n,o,a);return{cipher:u,encrypt:()=>t.stream({cipher:u,input:c,output:s,encryptedKey:y})}}static streamDecrypt(r,e){e.algorithm||(e.algorithm=t.DEFAULT_ALGORITHM.stream);let{input:o,output:a,algorithm:c,salt:s,iv:n,authTag:p,length:i}=t.resolveOptions(e),h=i+n+s+n+p;return t.extractKeyIV(o,h).then(([y,u])=>{if(u.errored)throw u.errored;let l=t.decrypt(y,r,{algorithm:c,salt:s,iv:n,authTag:p}),T=l.subarray(0,i),d=l.subarray(i),f=m.createDecipheriv(c,T,d);return{decipher:f,decrypt:()=>t.decipherStream({decipher:f,input:u,output:a})}})}static hybridEncrypt(r,e,o){o.algorithm||(o.algorithm=t.DEFAULT_ALGORITHM.stream);let{Key:a,IV:c,options:{algorithm:s,input:n,output:p}}=t.newKeyIV(r,o),i=m.publicEncrypt(e,Buffer.concat([a,c])),h=m.createCipheriv(s,a,c);return{cipher:h,encrypt:()=>t.stream({cipher:h,input:n,output:p,encryptedKey:i})}}static hybridDecrypt(r,e){e.algorithm||(e.algorithm=t.DEFAULT_ALGORITHM.stream);let{input:o,output:a,algorithm:c,length:s,rsaKeyLength:n}=t.resolveOptions(e);return t.extractKeyIV(o,n).then(([p,i])=>{let h=m.privateDecrypt(r,p),y=h.subarray(0,s),u=h.subarray(s),l=m.createDecipheriv(c,y,u);return{decipher:l,decrypt:()=>t.decipherStream({decipher:l,input:i,output:a})}})}static stream(r){let{cipher:e,encryptedKey:o,input:a,output:c}=r;return new Promise((s,n)=>{e.on("error",n),a.on("error",n),c.on("error",n),c.on("finish",s),c.write(o),a.pipe(e).pipe(c)})}static decipherStream(r){let{decipher:e,input:o,output:a}=r;return new Promise((c,s)=>{e.on("error",s),o.on("error",s),a.on("error",s),a.on("finish",c),o.pipe(e).pipe(a)})}static extractKeyIV(r,e){return new Promise((o,a)=>{let c=Buffer.alloc(0),s=0,n=!1,p=new C({async transform(i,h,y){if(c.length<e&&(c=Buffer.concat([c,i])),s+=i.length,c.length>e){let u=Math.max(0,c.length-e);c=c.subarray(0,e);let l=i.subarray(u*-1);return n||(o([c,this]),n=!0),this.push(l,h),y()}return s>e&&(n||(o([c,this]),n=!0),this.push(i,h)),y()},final(i){return c.length<e?i(new Error("The extracted KeyIV length is less than the expected length.")):i()}});p.on("error",a),r.on("error",i=>{p.destroy(i),a(i)}),r.pipe(p)})}static newKeyIV(r,e={}){let o=g(r),a=t.resolveOptions(e),c=m.randomBytes(a.salt),s=m.scryptSync(o,c,a.length),n=m.randomBytes(a.iv),p=a.aad||m.scryptSync(s,c,a.aadLength);return{options:a,Key:s,IV:n,AAD:p,salt:c}}static resolveOptions(r={}){let e={...r};e.aad&&(e.aad=g(e.aad)),e.salt||(e.salt=t.SALT_LENGTH.default),e.authTag||(e.authTag=t.AUTH_TAG_LENGTH.default),e.aadLength=e.aad?.length||e.aadLength||t.AAD_LENGTH.default,e.salt=Math.min(Math.max(e.salt,t.SALT_LENGTH.min),t.SALT_LENGTH.max),e.authTag=Math.min(Math.max(e.authTag,t.AUTH_TAG_LENGTH.min),t.AUTH_TAG_LENGTH.max),e.aad||(e.aadLength=Math.min(Math.max(e.aadLength,t.AAD_LENGTH.min),t.AAD_LENGTH.max));let{keyLength:o,algorithm:a}=t.getKeyLength(r.algorithm);return e.algorithm=a,e.length=o,e.iv=t.getIVLength(a,r),e}static getIVLength(r=t.DEFAULT_ALGORITHM.buffer,e={}){switch(r){case"aes-128-ccm":case"aes-128-ocb":case"aes-192-ccm":case"aes-192-ocb":case"aes-256-ccm":case"aes-256-ocb":return 8;case"chacha20-poly1305":return 12;default:return Math.min(Math.max(e.iv||t.IV_LENGTH.default,t.IV_LENGTH.min),t.IV_LENGTH.max)}}static getKeyLength(r=t.DEFAULT_ALGORITHM.buffer){switch(r){case"chacha20-poly1305":return{algorithm:r,keyLength:256/8};case"aes-128-cbc":case"aes-128-ccm":case"aes-128-gcm":case"aes-128-ocb":return{algorithm:r,keyLength:128/8};case"aes-192-cbc":case"aes-192-ccm":case"aes-192-gcm":case"aes-192-ocb":return{algorithm:r,keyLength:192/8};case"aes-256-cbc":case"aes-256-ccm":case"aes-256-gcm":case"aes-256-ocb":default:return{algorithm:r,keyLength:256/8}}}static isGCM(r){return r==="aes-128-gcm"||r==="aes-192-gcm"||r==="aes-256-gcm"}static isCCM(r){return r==="aes-128-ccm"||r==="aes-192-ccm"||r==="aes-256-ccm"||r==="chacha20-poly1305"}static isOCB(r){return r==="aes-128-ocb"||r==="aes-192-ocb"||r==="aes-256-ocb"}static isCBC(r){return r==="aes-128-cbc"||r==="aes-192-cbc"||r==="aes-256-cbc"}};t.SALT_LENGTH={min:16,max:64,default:32},t.IV_LENGTH={min:8,max:32,default:16},t.AUTH_TAG_LENGTH={min:4,max:16,default:16},t.AAD_LENGTH={min:16,max:128,default:32},t.DEFAULT_ALGORITHM={buffer:"aes-256-gcm",stream:"aes-256-cbc"},t.ALGORITHMS=["aes-128-cbc","aes-128-ccm","aes-128-gcm","aes-128-ocb","aes-192-cbc","aes-192-ccm","aes-192-gcm","aes-192-ocb","aes-256-cbc","aes-256-ccm","aes-256-gcm","aes-256-ocb","chacha20-poly1305"];var A=t;export{A as Cipher};
|
package/dist/types.d.mts
CHANGED
|
@@ -1,12 +1,15 @@
|
|
|
1
|
-
import { CoerceToUint8ArrayInput } from '@alessiofrittoli/crypto-buffer';
|
|
2
1
|
import crypto from 'crypto';
|
|
3
2
|
import { Readable, Writable } from 'stream';
|
|
3
|
+
import { CoerceToUint8ArrayInput } from '@alessiofrittoli/crypto-buffer';
|
|
4
4
|
|
|
5
|
+
/**
|
|
6
|
+
* Cipher types.
|
|
7
|
+
*/
|
|
5
8
|
declare namespace Cph {
|
|
6
9
|
/** Cipher CBC algorithm types. */
|
|
7
10
|
type CBCTypes = 'aes-128-cbc' | 'aes-192-cbc' | 'aes-256-cbc';
|
|
8
11
|
/** Cipher supported algorithm types. */
|
|
9
|
-
type AesAlgorithm = (crypto.CipherCCMTypes | crypto.CipherGCMTypes | crypto.CipherOCBTypes | Cph.CBCTypes);
|
|
12
|
+
type AesAlgorithm = (crypto.CipherCCMTypes | crypto.CipherGCMTypes | crypto.CipherOCBTypes | Cph.CBCTypes | crypto.CipherChaCha20Poly1305Types);
|
|
10
13
|
/**
|
|
11
14
|
* Common Cipher options.
|
|
12
15
|
* @template T The AES algorithm type.
|
package/dist/types.d.ts
CHANGED
|
@@ -1,12 +1,15 @@
|
|
|
1
|
-
import { CoerceToUint8ArrayInput } from '@alessiofrittoli/crypto-buffer';
|
|
2
1
|
import crypto from 'crypto';
|
|
3
2
|
import { Readable, Writable } from 'stream';
|
|
3
|
+
import { CoerceToUint8ArrayInput } from '@alessiofrittoli/crypto-buffer';
|
|
4
4
|
|
|
5
|
+
/**
|
|
6
|
+
* Cipher types.
|
|
7
|
+
*/
|
|
5
8
|
declare namespace Cph {
|
|
6
9
|
/** Cipher CBC algorithm types. */
|
|
7
10
|
type CBCTypes = 'aes-128-cbc' | 'aes-192-cbc' | 'aes-256-cbc';
|
|
8
11
|
/** Cipher supported algorithm types. */
|
|
9
|
-
type AesAlgorithm = (crypto.CipherCCMTypes | crypto.CipherGCMTypes | crypto.CipherOCBTypes | Cph.CBCTypes);
|
|
12
|
+
type AesAlgorithm = (crypto.CipherCCMTypes | crypto.CipherGCMTypes | crypto.CipherOCBTypes | Cph.CBCTypes | crypto.CipherChaCha20Poly1305Types);
|
|
10
13
|
/**
|
|
11
14
|
* Common Cipher options.
|
|
12
15
|
* @template T The AES algorithm type.
|
package/dist/types.js
CHANGED
|
@@ -1,2 +1 @@
|
|
|
1
|
-
|
|
2
|
-
//# sourceMappingURL=types.js.map
|
|
1
|
+
"use strict";
|
package/dist/types.mjs
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@alessiofrittoli/crypto-cipher",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.2.0",
|
|
4
4
|
"description": "Node.js Cipher cryptograph utility library",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "Alessio Frittoli",
|
|
@@ -8,6 +8,12 @@
|
|
|
8
8
|
"url": "https://alessiofrittoli.it"
|
|
9
9
|
},
|
|
10
10
|
"license": "MIT",
|
|
11
|
+
"funding": [
|
|
12
|
+
{
|
|
13
|
+
"type": "github",
|
|
14
|
+
"url": "https://github.com/sponsors/alessiofrittoli"
|
|
15
|
+
}
|
|
16
|
+
],
|
|
11
17
|
"keywords": [
|
|
12
18
|
"nodejs-crypto",
|
|
13
19
|
"cryptography",
|
|
@@ -46,19 +52,30 @@
|
|
|
46
52
|
},
|
|
47
53
|
"sideEffects": false,
|
|
48
54
|
"scripts": {
|
|
49
|
-
"
|
|
50
|
-
"
|
|
55
|
+
"//1a": "*********************************************************************",
|
|
56
|
+
"//1b": "******************** DEV - BUILD - LINT - RELEASE *******************",
|
|
57
|
+
"//1c": "*********************************************************************",
|
|
58
|
+
"dev": "NODE_ENV=development tsup --watch",
|
|
59
|
+
"build:prod": "NODE_ENV=production tsup",
|
|
60
|
+
"build": "pnpm lint && pnpm test:ci && pnpm build:prod",
|
|
51
61
|
"lint": "eslint",
|
|
52
62
|
"release": "node scripts/publish.js --verbose --npm",
|
|
53
|
-
"
|
|
63
|
+
"//2a": "*********************************************************************",
|
|
64
|
+
"//2b": "***************************** UNIT TESTS ****************************",
|
|
65
|
+
"//2c": "*********************************************************************",
|
|
66
|
+
"test": "jest --verbose",
|
|
54
67
|
"test:watch": "jest --watchAll --verbose",
|
|
55
68
|
"test:ci": "jest --ci --verbose",
|
|
69
|
+
"//3a": "*********************************************************************",
|
|
70
|
+
"//3b": "************************ UNIT TESTS COVERAGE ************************",
|
|
71
|
+
"//3c": "*********************************************************************",
|
|
56
72
|
"test:coverage": "pnpm test:watch --coverage",
|
|
57
|
-
"test:ci
|
|
73
|
+
"test:coverage:ci": "pnpm test:ci --coverage",
|
|
58
74
|
"test:serve-coverage": "http-server ./coverage/lcov-report --gzip true -p 0 -o --silent",
|
|
59
75
|
"test:coverage:serve": "concurrently --prefix none --kill-others \"pnpm test:coverage\" \"pnpm test:serve-coverage\"",
|
|
60
|
-
"
|
|
61
|
-
"
|
|
76
|
+
"//4a": "*********************************************************************",
|
|
77
|
+
"//4b": "************************ TARGETED UNIT TESTS ************************",
|
|
78
|
+
"//4c": "*********************************************************************",
|
|
62
79
|
"test:buffer-in-memory": "pnpm test:watch buffer-in-memory.test.ts",
|
|
63
80
|
"test:file-symmetric": "pnpm test:watch file-symmetric.test.ts",
|
|
64
81
|
"test:file-hybrid": "pnpm test:watch file-hybrid.test.ts",
|
|
@@ -67,25 +84,29 @@
|
|
|
67
84
|
"test:misc": "pnpm test:watch misc.test.ts"
|
|
68
85
|
},
|
|
69
86
|
"devDependencies": {
|
|
70
|
-
"@alessiofrittoli/node-scripts": "^2.
|
|
71
|
-
"@eslint/js": "^9.
|
|
87
|
+
"@alessiofrittoli/node-scripts": "^2.5.0",
|
|
88
|
+
"@eslint/js": "^9.26.0",
|
|
72
89
|
"@jest/globals": "^29.7.0",
|
|
73
90
|
"@types/jest": "^29.5.14",
|
|
74
|
-
"@types/node": "^22.
|
|
91
|
+
"@types/node": "^22.15.3",
|
|
75
92
|
"concurrently": "^9.1.2",
|
|
76
|
-
"dotenv": "^16.
|
|
77
|
-
"eslint": "^9.
|
|
78
|
-
"globals": "^
|
|
93
|
+
"dotenv": "^16.5.0",
|
|
94
|
+
"eslint": "^9.26.0",
|
|
95
|
+
"globals": "^16.0.0",
|
|
79
96
|
"http-server": "^14.1.1",
|
|
80
97
|
"jest": "^29.7.0",
|
|
81
|
-
"jest
|
|
82
|
-
"ts-jest": "^29.2.5",
|
|
98
|
+
"ts-jest": "^29.3.2",
|
|
83
99
|
"ts-node": "^10.9.2",
|
|
84
|
-
"tsup": "^8.
|
|
85
|
-
"typescript": "^5.
|
|
86
|
-
"typescript-eslint": "^8.
|
|
100
|
+
"tsup": "^8.4.0",
|
|
101
|
+
"typescript": "^5.8.3",
|
|
102
|
+
"typescript-eslint": "^8.31.1"
|
|
87
103
|
},
|
|
88
104
|
"dependencies": {
|
|
89
|
-
"@alessiofrittoli/crypto-buffer": "^3.
|
|
105
|
+
"@alessiofrittoli/crypto-buffer": "^3.6.0"
|
|
106
|
+
},
|
|
107
|
+
"pnpm": {
|
|
108
|
+
"onlyBuiltDependencies": [
|
|
109
|
+
"esbuild"
|
|
110
|
+
]
|
|
90
111
|
}
|
|
91
112
|
}
|
package/dist/index.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"names":["_Cipher","data","secret","options","_data","coerceToUint8Array","salt","Key","IV","AAD","algorithm","authTagLength","cipher","crypto","encrypted","authTag","_secret","_options","decipher","AADLength","input","output","iv","encryptedKey","length","keyIvLength","encryptedKeyIV","KeyIV","publicKey","privateKey","rsaKeyLength","resolve","reject","keyLength","bytesRead","resolved","transform","Transform","chunk","encoding","callback","bytesLoss","subchunk","error","Cipher"],"mappings":"0OAmCO,IAAMA,CAAN,CAAA,MAAMA,CACb,CAuEC,OAAO,QACNC,CACAC,CAAAA,CAAAA,CACAC,CAAwB,CAAA,GAEzB,CACC,IAAMC,EAAQC,2BAAoBJ,CAAAA,CAAK,EAEjC,CACL,IAAA,CAAAK,CAAM,CAAA,GAAA,CAAAC,EAAK,EAAAC,CAAAA,CAAAA,CAAI,IAAAC,CACf,CAAA,OAAA,CAAS,CAAE,SAAAC,CAAAA,CAAAA,CAAW,OAASC,CAAAA,CAAc,CAC9C,CAAIX,CAAAA,CAAAA,CAAO,QAAUE,CAAAA,CAAAA,CAAQC,CAAQ,CAErC,CAAA,GAAKH,CAAO,CAAA,KAAA,CAAOU,CAAU,CAAI,CAAA,CAChC,IAAME,CAASC,CAAAA,kBAAAA,CAAO,eAAgBH,CAAWH,CAAAA,CAAAA,CAAKC,CAAG,CAAA,CACnDM,EAAY,MAAO,CAAA,MAAA,CAAQ,CAAEF,CAAO,CAAA,MAAA,CAAQR,CAAM,CAAGQ,CAAAA,CAAAA,CAAO,KAAM,EAAE,CAAE,CAE5E,CAAA,OAAO,OAAO,MAAQ,CAAA,CAAEN,EAAME,CAAIM,CAAAA,CAAU,CAAE,CAC/C,CAGA,IAAMF,CAAAA,CACLC,kBAAO,CAAA,cAAA,CAENH,EAAWH,CAAKC,CAAAA,CAAAA,CAAI,CAAE,aAAA,CAAAG,CAAc,CACrC,CAAA,CAIIX,EAAO,KAAOU,CAAAA,CAAU,GAE5BE,CAAO,CAAA,MAAA,CAAQH,CAAK,CAAA,CAAE,gBAAiBL,CAAM,CAAA,MAAO,CAAE,CAGlDJ,CAAAA,CAAAA,CAAAA,CAAO,MAAOU,CAAU,CAAA,EAAKV,CAA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CAAA,CACnC,OAAQ,aACR,CAAA,MAAA,CAAQ,aACT,CAAA,CAhDYA,EAsDI,UAAiC,CAAA,CAChD,cAAe,aAAe,CAAA,aAAA,CAAe,cAC7C,aAAe,CAAA,aAAA,CAAe,aAAe,CAAA,aAAA,CAC7C,cAAe,aAAe,CAAA,aAAA,CAAe,cAC7C,mBACD,CAAA,KA3DY4C,CAAN5C,CAAAA","file":"index.js","sourcesContent":["import crypto from 'crypto'\nimport { Transform } from 'stream'\nimport type { Readable, Writable } from 'stream'\nimport { coerceToUint8Array, type CoerceToUint8ArrayInput } from '@alessiofrittoli/crypto-buffer/coercion'\n\nimport type { Cph } from './types'\n\n\n\n/**\n * INTERNAL USE ONLY\n */\ninterface StreamEncryptOptions\n{\n\tcipher\t\t: crypto.Cipher\n\tencryptedKey: Buffer\n\tinput\t\t: Readable\n\toutput\t\t: Writable\n}\n\n\n/**\n * INTERNAL USE ONLY\n */\ninterface StreamDecryptOptions\n{\n\tdecipher\t: crypto.Decipher\n\tinput\t\t: Readable\n\toutput\t\t: Writable\n}\n\n\n/**\n * Utility class for AES encryption and decryption following [NIST SP 800-38D](https://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38d.pdf) standard reccomendations.\n */\nexport class Cipher\n{\n\t/**\n\t * Cipher default `salt` lengths.\n\t */\n\tstatic readonly SALT_LENGTH = {\n\t\tmin\t\t: 16,\n\t\tmax\t\t: 64,\n\t\tdefault\t: 32,\n\t} as const\n\n\n\t/**\n\t * Cipher default `IV` lengths.\n\t */\n\tstatic readonly IV_LENGTH = {\n\t\tmin\t\t: 8,\n\t\tmax\t\t: 32,\n\t\tdefault\t: 16,\n\t} as const\n\n\n\t/**\n\t * Cipher default `Auth Tag` lengths.\n\t */\n\tstatic readonly AUTH_TAG_LENGTH = {\n\t\tmin\t\t: 4,\n\t\tmax\t\t: 16,\n\t\tdefault\t: 16,\n\t} as const\n\t\n\t\n\t/**\n\t * Cipher default `Additional Authenticated Data` lengths.\n\t */\n\tstatic readonly AAD_LENGTH = {\n\t\tmin\t\t: 16,\n\t\tmax\t\t: 128,\n\t\tdefault\t: 32,\n\t} as const\n\t\n\n\t/**\n\t * Default AES algorithms used based on functionality.\n\t */\n\tstatic readonly DEFAULT_ALGORITHM = {\n\t\tbuffer: 'aes-256-gcm',\n\t\tstream: 'aes-256-cbc',\n\t} as const\n\n\n\t/**\n\t * Supported AES algorithms.\n\t */\n\tstatic readonly ALGORITHMS: Cph.AesAlgorithm[] = [\n\t\t'aes-128-cbc', 'aes-128-ccm', 'aes-128-gcm', 'aes-128-ocb',\n\t\t'aes-192-cbc', 'aes-192-ccm', 'aes-192-gcm', 'aes-192-ocb',\n\t\t'aes-256-cbc', 'aes-256-ccm', 'aes-256-gcm', 'aes-256-ocb',\n\t\t'chacha20-poly1305'\n\t] as const\n\n\n\t/**\n\t * Encrypt in-memory data buffer.\n\t *\n\t * ⚠️ This is not suitable for large data. Use {@link Cipher.streamEncrypt()} or {@link Cipher.hybridEncrypt()} methods for large data encryption.\n\t *\n\t * @param\tdata\tThe data to encrypt.\n\t * @param\tsecret\tThe secret key used to encrypt the `data`.\n\t * @param\toptions\t( Optional ) Additional options.\n\t * @returns\tThe encrypted result Buffer.\n\t */\n\tstatic encrypt(\n\t\tdata\t: CoerceToUint8ArrayInput,\n\t\tsecret\t: CoerceToUint8ArrayInput,\n\t\toptions\t: Cph.Options = {},\n\t): Buffer\n\t{\n\t\tconst _data = coerceToUint8Array( data )\n\n\t\tconst {\n\t\t\tsalt, Key, IV, AAD,\n\t\t\toptions: { algorithm, authTag: authTagLength },\n\t\t} = Cipher.newKeyIV( secret, options )\n\n\t\tif ( Cipher.isCBC( algorithm ) ) {\n\t\t\tconst cipher\t= crypto.createCipheriv( algorithm, Key, IV )\n\t\t\tconst encrypted\t= Buffer.concat( [ cipher.update( _data ), cipher.final() ] )\n\n\t\t\treturn Buffer.concat( [ salt, IV, encrypted ] )\n\t\t}\n\n\n\t\tconst cipher = (\n\t\t\tcrypto.createCipheriv(\n\t\t\t\t// @ts-expect-error `crypto.createCipheriv` cannot infer the correct overload based on the given union type algorithm.\n\t\t\t\talgorithm, Key, IV, { authTagLength }\n\t\t\t)\n\t\t)\n\t\t\n\t\t\n\t\tif ( Cipher.isCCM( algorithm ) ) {\n\t\t\t// AES-CCM requires `plaintextLength`\n\t\t\tcipher.setAAD( AAD, { plaintextLength: _data.length } )\n\t\t}\n\n\t\tif ( Cipher.isGCM( algorithm ) || Cipher.isOCB( algorithm ) ) {\n\t\t\t// AES-GCM and AES-OCB doesn't require `plaintextLength`\n\t\t\tcipher.setAAD( AAD )\n\t\t}\n\n\t\tconst encrypted\t= Buffer.concat( [ cipher.update( _data ), cipher.final() ] )\n\t\tconst authTag\t= cipher.getAuthTag()\n\n\t\treturn Buffer.concat( [ salt, IV, AAD, authTag, encrypted ] )\n\t}\n\n\n\t/**\n\t * Decrypt in-memory data buffer.\n\t *\n\t * ⚠️ This is not suitable for large data. Use {@link Cipher.streamDecrypt()} or {@link Cipher.hybridDecrypt()} methods for large data decryption.\n\t *\n\t * @param\tdata\tThe data to decrypt.\n\t * @param\tsecret\tThe secret key used to decrypt the `data`.\n\t * @param\toptions\t( Optional ) Additional options. Must be the same used while encrypting data.\n\t * @returns\tThe decrypted data Buffer.\n\t */\n\tstatic decrypt(\n\t\tdata\t: CoerceToUint8ArrayInput,\n\t\tsecret\t: CoerceToUint8ArrayInput,\n\t\toptions\t: Cph.Options = {},\n\t): Buffer\n\t{\n\t\tlet _data\t\t\t= coerceToUint8Array( data )\n\t\tconst _secret\t\t= coerceToUint8Array( secret )\n\t\tconst _options\t\t= Cipher.resolveOptions( options )\n\t\tconst { algorithm }\t= _options\n\n\t\tconst salt\t\t= _data.subarray( 0, _options.salt )\n\t\t_data\t\t\t= _data.subarray( _options.salt )\n\t\tconst IV\t\t= _data.subarray( 0, _options.iv )\n\t\t_data\t\t\t= _data.subarray( _options.iv )\n\t\tconst Key\t\t= crypto.scryptSync( _secret, salt, _options.length )\n\n\t\tif ( Cipher.isCBC( algorithm ) ) {\n\t\t\tconst decipher = (\n\t\t\t\tcrypto.createDecipheriv( _options.algorithm, Key, IV )\n\t\t\t)\n\n\t\t\treturn (\n\t\t\t\tBuffer.concat( [ decipher.update( _data ), decipher.final() ] )\n\t\t\t)\n\t\t}\n\n\t\tconst AADLength\t\t= _options.aadLength\n\t\tconst authTagLength\t= _options.authTag\n\t\tconst AAD\t\t\t= _data.subarray( 0, AADLength )\n\t\t_data\t\t\t\t= _data.subarray( AADLength )\n\t\tconst authTag\t\t= _data.subarray( 0, authTagLength )\n\t\t_data\t\t\t\t= _data.subarray( authTagLength )\n\n\n\t\tconst decipher = (\n\t\t\tcrypto.createDecipheriv(\n\t\t\t\t// @ts-expect-error `crypto.createDecipheriv` cannot infer the correct overload based on the given union type algorithm.\n\t\t\t\talgorithm, Key, IV, { authTagLength }\n\t\t\t)\n\t\t)\n\n\t\tif ( Cipher.isCCM( algorithm ) ) {\n\t\t\t// AES-CCM requires `plaintextLength`\n\t\t\tdecipher.setAAD( AAD, { plaintextLength: _data.length } )\n\t\t}\n\n\t\tif ( Cipher.isGCM( algorithm ) || Cipher.isOCB( algorithm ) ) {\n\t\t\t// AES-GCM and AES-OCB doesn't require `plaintextLength`\n\t\t\tdecipher.setAAD( AAD )\n\t\t}\n\n\t\tdecipher.setAuthTag( authTag )\n\t\n\t\treturn (\n\t\t\tBuffer.concat( [ decipher.update( _data ), decipher.final() ] )\n\t\t)\n\n\t}\n\n\n\t/**\n\t * Encrypt a `Readable` to a `Writable` Stream.\n\t * \n\t * The `Readable` Stream could be 'in-memory buffer' or 'file' based.\n\t * \n\t * @param\tsecret\tThe secret key used to encrypt the `data`.\n\t * @param\toptions Additional options.\n\t * @returns An object containing:\n\t * \t- a new instance of `crypto.Cipher` allowing you to add listeners to the `cipher` encryption process.\n\t * \t- the actual `encrypt` callback that must be called and awaited in order to start the encryption process.\n\t */\n\tstatic streamEncrypt(\n\t\tsecret\t: CoerceToUint8ArrayInput,\n\t\toptions\t: Cph.Stream.Symmetric.EncryptOptions,\n\t): Cph.Stream.Symmetric.EncryptReturnType\n\t{\n\t\toptions.algorithm ||= Cipher.DEFAULT_ALGORITHM.stream\n\n\t\tconst {\n\t\t\tKey, IV, options: { input, output, algorithm, salt, iv, authTag },\n\t\t} = Cipher.newKeyIV<Cph.Stream.Symmetric.EncryptResolvedOptions>( secret, options )\n\n\t\tconst encryptedKey = (\n\t\t\tCipher.encrypt( Buffer.concat( [ Key, IV ] ), secret, { algorithm, salt, iv, authTag } )\n\t\t)\n\n\t\tconst cipher = crypto.createCipheriv( algorithm, Key, IV )\n\t\tconst encrypt = () => (\n\t\t\tCipher.stream( {\n\t\t\t\tcipher, input, output, encryptedKey\n\t\t\t} )\n\t\t)\n\n\t\treturn { cipher, encrypt }\n\t}\n\n\n\t/**\n\t * Decrypt a `Readable` to a `Writable` Stream.\n\t * \n\t * The `Readable` Stream could be 'in-memory buffer' or 'file' based.\n\t * \n\t * @param\tsecret\tThe secret key used to encrypt the `data`.\n\t * @param\toptions Additional options.\n\t * @returns A new Promise that resolves when Key IV extraction completes returning an object containing:\n\t * \t- a new instance of `crypto.Decipher` allowing you to add listeners to the `decipher` decryption process.\n\t * \t- the actual `decrypt` callback that must be called and awaited in order to start the decryption process.\n\t */\n\tstatic streamDecrypt(\n\t\tsecret\t: CoerceToUint8ArrayInput,\n\t\toptions\t: Cph.Stream.Symmetric.DecryptOptions,\n\t): Promise<Cph.Stream.Symmetric.DecryptReturnType>\n\t{\n\t\toptions.algorithm ||= Cipher.DEFAULT_ALGORITHM.stream\n\n\t\tconst {\n\t\t\tinput, output, algorithm, salt, iv, authTag, length\n\t\t} = Cipher.resolveOptions<Cph.Stream.Symmetric.DecryptResolvedOptions>( options )\n\n\t\tconst keyIvLength = (\n\t\t\tlength + iv\n\t\t\t+ salt + iv + authTag\n\t\t)\n\n\t\treturn (\n\t\t\tCipher.extractKeyIV( input, keyIvLength )\n\t\t\t\t.then( ( [ encryptedKeyIV, input ] ) => {\n\t\t\t\t\t/**\n\t\t\t\t\t * Check if input has error and re-throw if so.\n\t\t\t\t\t * This is required since `.on( 'error' )` listeners attached in\n\t\t\t\t\t * `Cipher.decipherStream()` get attached too late (error event already emitted).\n\t\t\t\t\t */\n\t\t\t\t\tif ( input.errored ) throw input.errored\n\t\t\t\t\t\n\t\t\t\t\tconst KeyIV = (\n\t\t\t\t\t\tCipher.decrypt( encryptedKeyIV, secret, { algorithm, salt, iv, authTag } )\n\t\t\t\t\t)\n\n\t\t\t\t\tconst Key\t\t= KeyIV.subarray( 0, length )\n\t\t\t\t\tconst IV\t\t= KeyIV.subarray( length )\n\t\t\t\t\tconst decipher\t= crypto.createDecipheriv( algorithm, Key, IV )\n\t\n\t\t\t\t\tconst decrypt = () => (\n\t\t\t\t\t\tCipher.decipherStream(\n\t\t\t\t\t\t\t{ decipher, input, output }\n\t\t\t\t\t\t)\n\t\t\t\t\t)\n\t\t\t\n\t\t\t\t\treturn { decipher, decrypt }\n\t\t\t\t} )\n\t\t)\n\t}\n\n\n\t/**\n\t * Encrypt a `Readable` to a `Writable` Stream with hybird Encryption.\n\t *\n\t * The `Readable` Stream could be 'in-memory buffer' or 'file' based.\n\t *\n\t * @param\tsecret\t\tThe secret key used to encrypt the stream.\n\t * @param\tpublicKey\tThe RSA public key used to encrypt the symmetric key.\n\t * @param\toptions\t\tOptions for the stream encryption.\n\t * @returns An object containing:\n\t * \t- a new instance of `crypto.Cipher` allowing you to add listeners to the `cipher` encryption process.\n\t * \t- the actual `encrypt` callback that must be called and awaited in order to start the encryption process.\n\t */\n\tstatic hybridEncrypt(\n\t\tsecret\t\t: CoerceToUint8ArrayInput,\n\t\tpublicKey\t: crypto.RsaPublicKey | crypto.RsaPrivateKey | crypto.KeyLike,\n\t\toptions\t\t: Cph.Stream.Hybrid.EncryptOptions,\n\t): Cph.Stream.Hybrid.EncryptReturnType\n\t{\n\t\toptions.algorithm ||= Cipher.DEFAULT_ALGORITHM.stream\n\n\t\tconst {\n\t\t\tKey, IV,\n\t\t\toptions: { algorithm, input, output },\n\t\t} = Cipher.newKeyIV<Cph.Stream.Hybrid.EncryptResolvedOptions>( secret, options )\n\n\t\tconst encryptedKey = (\n\t\t\tcrypto.publicEncrypt( publicKey, Buffer.concat( [ Key, IV ] ) )\n\t\t)\n\n\t\tconst cipher = crypto.createCipheriv( algorithm, Key, IV )\n\t\tconst encrypt = () => (\n\t\t\tCipher.stream( {\n\t\t\t\tcipher, input, output, encryptedKey\n\t\t\t} )\n\t\t)\n\n\t\treturn { cipher, encrypt }\n\t}\n\n\n\t/**\n\t * Decrypt a `Readable` to a `Writable` Stream with hybrid Decryption.\n\t * \n\t * The `Readable` Stream could be 'in-memory buffer' or 'file' based.\n\t *\n\t * @param\tprivateKey\tThe RSA private key used to decrypt the symmetric key.\n\t * @param\toptions\t\tOptions for the stream decryption.\n\t * @returns A new Promise that resolves when Key IV extraction completes returning an object containing:\n\t * \t- a new instance of `crypto.Decipher` allowing you to add listeners to the `decipher` decryption process.\n\t * \t- the actual `decrypt` callback that must be called and awaited in order to start the decryption process.\n\t */\n\tstatic hybridDecrypt(\n\t\tprivateKey\t: crypto.RsaPrivateKey | crypto.KeyLike,\n\t\toptions\t\t: Cph.Stream.Hybrid.DecryptOptions,\n\t): Promise<Cph.Stream.Hybrid.DecryptReturnType>\n\t{\n\t\toptions.algorithm ||= Cipher.DEFAULT_ALGORITHM.stream\n\n\t\tconst {\n\t\t\tinput, output, algorithm, length, rsaKeyLength,\n\t\t} = Cipher.resolveOptions<Cph.Stream.Hybrid.DecryptResolvedOptions>( options )\n\n\t\treturn (\n\t\t\tCipher.extractKeyIV( input, rsaKeyLength )\n\t\t\t\t.then( ( [ encryptedKeyIV, input ] ) => {\n\t\t\t\t\tconst KeyIV\t\t= crypto.privateDecrypt( privateKey, encryptedKeyIV )\n\t\t\t\t\tconst Key\t\t= KeyIV.subarray( 0, length )\n\t\t\t\t\tconst IV\t\t= KeyIV.subarray( length )\n\t\t\t\t\tconst decipher\t= crypto.createDecipheriv( algorithm, Key, IV )\n\t\n\t\t\t\t\tconst decrypt = () => (\n\t\t\t\t\t\tCipher.decipherStream(\n\t\t\t\t\t\t\t{ decipher, input, output }\n\t\t\t\t\t\t)\n\t\t\t\t\t)\n\t\t\t\n\t\t\t\t\treturn { decipher, decrypt }\n\t\t\t\t} )\n\t\t)\n\t}\n\n\n\t/**\n\t * Handle pipe flow to encrypt a Stream.\n\t * \n\t * @param\toptions Required parameters.\n\t * @returns A new Promise that resolves `void` once stream is completed.\n\t */\n\tprivate static stream( options: StreamEncryptOptions )\n\t{\n\t\tconst {\n\t\t\tcipher, encryptedKey,\n\t\t\tinput, output\n\t\t} = options\n\n\t\treturn (\n\t\t\tnew Promise<void>( ( resolve, reject ) => {\n\t\t\t\tcipher.on( 'error', reject )\n\t\t\t\tinput.on( 'error', reject )\n\t\t\t\toutput.on( 'error', reject )\n\t\t\t\toutput.on( 'finish', resolve )\n\t\t\n\t\t\t\toutput.write( encryptedKey )\n\t\t\t\tinput.pipe( cipher ).pipe( output )\n\t\t\t} )\n\t\t)\n\t}\n\n\n\t/**\n\t * Handle pipe flow to decrypt a Stream.\n\t * \n\t * @param\toptions Required parameters.\n\t * @returns A new Promise that resolves `void` once stream is completed.\n\t */\n\tprivate static decipherStream( options: StreamDecryptOptions )\n\t{\n\t\tconst {\n\t\t\tdecipher, input, output,\n\t\t} = options\n\n\t\treturn (\n\t\t\tnew Promise<void>( ( resolve, reject ) => {\n\t\t\t\tdecipher.on( 'error', reject )\n\t\t\t\tinput.on( 'error', reject )\n\t\t\t\toutput.on( 'error', reject )\n\t\t\t\toutput.on( 'finish', resolve )\n\n\t\t\t\tinput.pipe( decipher ).pipe( output )\n\t\t\t} )\n\t\t)\n\t}\n\n\n\t/**\n\t * Extract the Cipher Encrypted Symmetric Key and Initialization Vector from an encrypted `Readable` Stream.\n\t *\n\t * @param\tinput\t\tThe `Readable` Stream.\n\t * @param\tkeyLength\tThe encrypted key length in bytes. This is used to properly extract the encrypted Cipher Key and Initialization Vector.\n\t * @returns\tA new Promise that resolve a tuple containing the Cipher Encrypted Symmetric Key and Initialization Vector once fulfilled.\n\t */\n\tprivate static extractKeyIV(\n\t\tinput\t\t: Readable,\n\t\tkeyLength\t: number,\n\t)\n\t{\n\t\treturn (\n\t\t\tnew Promise<[ KeyIV: Buffer, input: Transform ]>( ( resolve, reject ) => {\n\t\t\t\tlet KeyIV\t\t= Buffer.alloc( 0 )\n\t\t\t\tlet bytesRead\t= 0\n\t\t\t\tlet resolved\t= false\n\n\t\t\t\tconst transform = new Transform( {\n\t\t\t\t\tasync transform( chunk: Buffer, encoding, callback )\n\t\t\t\t\t{\n\n\t\t\t\t\t\tif ( KeyIV.length < keyLength ) {\n\t\t\t\t\t\t\tKeyIV = Buffer.concat( [ KeyIV, chunk ] )\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tbytesRead += chunk.length\n\n\t\t\t\t\t\t/**\n\t\t\t\t\t\t * `KeyIV` length may exceed the `keyLength`.\n\t\t\t\t\t\t * This may occurs when the received chunk length is not a multiple of the `keyLength`.\n\t\t\t\t\t\t *\n\t\t\t\t\t\t * In that case the chunk will contain mixed content:\n\t\t\t\t\t\t * - end of the `KeyIV`.\n\t\t\t\t\t\t * - begin of the actual encrypted data.\n\t\t\t\t\t\t *\n\t\t\t\t\t\t * For a proper data handling we need to:\n\t\t\t\t\t\t * - cut off the `KeyIV` to `keyLength` bytes.\n\t\t\t\t\t\t * - recover and push the byte loss to the next piped destination.\n\t\t\t\t\t\t */\n\t\t\t\t\t\tif ( KeyIV.length > keyLength ) {\n\t\t\t\t\t\t\tconst bytesLoss\t= Math.max( 0, KeyIV.length - keyLength )\n\t\t\t\t\t\t\tKeyIV\t\t\t= KeyIV.subarray( 0, keyLength )\n\t\t\t\t\t\t\tconst subchunk\t= chunk.subarray( bytesLoss * -1 )\n\n\t\t\t\t\t\t\tif ( ! resolved ) {\n\t\t\t\t\t\t\t\tresolve( [ KeyIV, this ] )\n\t\t\t\t\t\t\t\tresolved = true\n\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\t/**\n\t\t\t\t\t\t\t * Push to the next piped destination.\n\t\t\t\t\t\t\t */\t\t\t\t\t\t\t\n\t\t\t\t\t\t\tthis.push( subchunk, encoding )\n\n\t\t\t\t\t\t\treturn callback()\n\t\t\t\t\t\t}\n\n\n\t\t\t\t\t\t/**\n\t\t\t\t\t\t * `KeyIV` has been read.\n\t\t\t\t\t\t * The Received chunk can now be pushed to the next piped destination.\n\t\t\t\t\t\t */\n\t\t\t\t\t\tif ( bytesRead > keyLength ) {\n\t\t\t\t\t\t\tif ( ! resolved ) {\n\t\t\t\t\t\t\t\tresolve( [ KeyIV, this ] )\n\t\t\t\t\t\t\t\tresolved = true\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t/**\n\t\t\t\t\t\t\t * Push to the next piped destination.\n\t\t\t\t\t\t\t */\n\t\t\t\t\t\t\tthis.push( chunk, encoding )\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\treturn callback()\n\t\t\t\t\t},\n\t\t\t\t\tfinal( callback )\n\t\t\t\t\t{\n\t\t\t\t\t\tif ( KeyIV.length < keyLength ) {\n\t\t\t\t\t\t\treturn callback(\n\t\t\t\t\t\t\t\tnew Error( 'The extracted KeyIV length is less than the expected length.' )\n\t\t\t\t\t\t\t)\n\t\t\t\t\t\t}\n\t\t\t\t\t\treturn callback()\n\t\t\t\t\t},\n\t\t\t\t} )\n\n\t\t\t\ttransform.on( 'error', reject )\n\t\t\t\tinput.on( 'error', error => {\n\t\t\t\t\ttransform.destroy( error )\n\t\t\t\t\treject( error )\n\t\t\t\t} )\n\t\t\t\tinput.pipe( transform )\n\t\t\t} )\n\t\t)\n\t}\n\n\n\t/**\n\t * Generates a `Scrypt` Symmetric Key and the Initialization Vector with the given options.\n\t * \n\t * @param\tsecret\tThe secret key.\n\t * @param\toptions\t( Optional ) Additional options.\n\t * \n\t * @returns An object with the generated `Key`, `IV`, `salt` and resolved `options`. \n\t */\n\tstatic newKeyIV<T extends Cph.ResolvedOptions = Cph.ResolvedOptions>(\n\t\tsecret\t: CoerceToUint8ArrayInput,\n\t\toptions\t: Cph.Options = {},\n\t)\n\t{\n\t\tconst _secret\t= coerceToUint8Array( secret )\n\t\tconst _options\t= Cipher.resolveOptions<T>( options )\n\t\tconst salt\t\t= crypto.randomBytes( _options.salt )\n\t\tconst Key\t\t= crypto.scryptSync( _secret, salt, _options.length )\n\t\tconst IV\t\t= crypto.randomBytes( _options.iv )\n\t\tconst AAD\t\t= _options.aad || crypto.scryptSync( Key, salt, _options.aadLength )\n\n\t\treturn { options: _options, Key, IV, AAD, salt }\n\t}\n\n\n\t/**\n\t * Resolves the given `options` with `Cipher` defaults and constraints.\n\t * \n\t * @param options ( Optional ) Additional options.\n\t * @returns The given `options` with `Cipher` defaults and constraints.\n\t */\n\tstatic resolveOptions<\n\t\tT extends Cph.ResolvedOptions = Cph.ResolvedOptions\n\t>( options: Cph.Options = {} ): T\n\t{\n\t\tconst _options = { ...options } as T\n\n\t\tif ( _options.aad ) {\n\t\t\t_options.aad = coerceToUint8Array( _options.aad )\n\t\t}\n\n\t\t_options.salt\t\t||= Cipher.SALT_LENGTH.default\n\t\t_options.authTag\t||= Cipher.AUTH_TAG_LENGTH.default\n\t\t_options.aadLength\t= _options.aad?.length || _options.aadLength || Cipher.AAD_LENGTH.default\n\t\t_options.salt\t\t= Math.min( Math.max( _options.salt, Cipher.SALT_LENGTH.min ), Cipher.SALT_LENGTH.max )\n\t\t_options.authTag\t= Math.min( Math.max( _options.authTag, Cipher.AUTH_TAG_LENGTH.min ), Cipher.AUTH_TAG_LENGTH.max )\n\t\tif ( ! _options.aad ) {\n\t\t\t_options.aadLength\t= Math.min( Math.max( _options.aadLength, Cipher.AAD_LENGTH.min ), Cipher.AAD_LENGTH.max )\n\t\t}\n\n\t\tconst { keyLength, algorithm } = Cipher.getKeyLength( options.algorithm )\n\n\t\t_options.algorithm\t= algorithm\n\t\t_options.length\t\t= keyLength\n\t\t_options.iv\t\t\t= Cipher.getIVLength( algorithm, options )\n\n\t\treturn _options\n\t}\n\n\n\t/**\n\t * Get the Initialization Vector length based on the given algorithm.\n\t * \n\t * @param\talgorithm The algorithm in use.\n\t * @param\toptions ( Optional ) Additional options. \n\t * @returns\tThe Initialization Vector length based on the given algorithm\n\t */\n\tprivate static getIVLength(\n\t\talgorithm\t: Cph.Options[ 'algorithm' ] = Cipher.DEFAULT_ALGORITHM.buffer,\n\t\toptions\t\t: Cph.Options = {},\n\t)\n\t{\n\t\tswitch ( algorithm ) {\n\t\t\tcase 'aes-128-ccm':\n\t\t\tcase 'aes-128-ocb':\n\t\t\tcase 'aes-192-ccm':\n\t\t\tcase 'aes-192-ocb':\n\t\t\tcase 'aes-256-ccm':\n\t\t\tcase 'aes-256-ocb':\n\t\t\t\treturn 8\n\t\t\tcase 'chacha20-poly1305':\n\t\t\t\treturn 12\n\t\t\tdefault:\n\t\t\t\treturn (\n\t\t\t\t\tMath.min(\n\t\t\t\t\t\tMath.max(\n\t\t\t\t\t\t\toptions.iv || Cipher.IV_LENGTH.default,\n\t\t\t\t\t\t\tCipher.IV_LENGTH.min\n\t\t\t\t\t\t), Cipher.IV_LENGTH.max\n\t\t\t\t\t)\n\t\t\t\t)\n\t\t}\n\t}\n\n\n\t/**\n\t * Ensure correct key length based on the given algorithm.\n\t *\n\t * @param\talgorithm The AES algorithm name.\n\t * @returns\tAn object with validated `algorithm` and `keyLength`.\n\t */\n\tprivate static getKeyLength(\n\t\talgorithm: Cph.Options[ 'algorithm' ] = Cipher.DEFAULT_ALGORITHM.buffer\n\t)\n\t{\n\t\tswitch ( algorithm ) {\n\t\t\tcase 'chacha20-poly1305':\n\t\t\t\treturn {\n\t\t\t\t\talgorithm: algorithm,\n\t\t\t\t\tkeyLength: 256 / 8,\n\t\t\t\t} as const\n\t\t\tcase 'aes-128-cbc':\n\t\t\tcase 'aes-128-ccm':\n\t\t\tcase 'aes-128-gcm':\n\t\t\tcase 'aes-128-ocb':\n\t\t\t\treturn {\n\t\t\t\t\talgorithm: algorithm,\n\t\t\t\t\tkeyLength: 128 / 8, // AES-128 needs a 128 bit (16 bytes)\n\t\t\t\t} as const\n\t\t\tcase 'aes-192-cbc':\n\t\t\tcase 'aes-192-ccm':\n\t\t\tcase 'aes-192-gcm':\n\t\t\tcase 'aes-192-ocb':\n\t\t\t\treturn {\n\t\t\t\t\talgorithm: algorithm,\n\t\t\t\t\tkeyLength: 192 / 8, // AES-192 needs a 192 bit (24 bytes)\n\t\t\t\t} as const\n\t\t\tcase 'aes-256-cbc':\n\t\t\tcase 'aes-256-ccm':\n\t\t\tcase 'aes-256-gcm':\n\t\t\tcase 'aes-256-ocb':\n\t\t\tdefault:\n\t\t\t\treturn {\n\t\t\t\t\talgorithm: algorithm,\n\t\t\t\t\tkeyLength: 256 / 8, // AES-256 needs a 256 bit (32 bytes)\n\t\t\t\t} as const\n\t\t}\n\t}\n\n\n\t/**\n\t * Check if the given algorithm is a Cipher AES-GCM algorithm.\n\t * \n\t * @param algorithm The AES Algorithm to check.\n\t * @returns `true` if the given algorithm is a Cipher AES-GCM algorithm. `false` otherwise.\n\t */\n\tstatic isGCM( algorithm: Cph.AesAlgorithm ): algorithm is crypto.CipherGCMTypes\n\t{\n\t\treturn (\n\t\t\talgorithm === 'aes-128-gcm' ||\n\t\t\talgorithm === 'aes-192-gcm' ||\n\t\t\talgorithm === 'aes-256-gcm'\n\t\t)\n\t}\n\n\n\t/**\n\t * Check if the given algorithm is a Cipher AES-CCM algorithm.\n\t * \n\t * @param algorithm The AES Algorithm to check.\n\t * @returns `true` if the given algorithm is a Cipher AES-CCM algorithm. `false` otherwise.\n\t */\n\tstatic isCCM( algorithm: Cph.AesAlgorithm ): algorithm is crypto.CipherCCMTypes\n\t{\n\t\treturn (\n\t\t\talgorithm === 'aes-128-ccm' ||\n\t\t\talgorithm === 'aes-192-ccm' ||\n\t\t\talgorithm === 'aes-256-ccm' ||\n\t\t\talgorithm === 'chacha20-poly1305'\n\t\t)\n\t}\n\n\n\t/**\n\t * Check if the given algorithm is a Cipher AES-OCB algorithm.\n\t * \n\t * @param algorithm The AES Algorithm to check.\n\t * @returns `true` if the given algorithm is a Cipher AES-OCB algorithm. `false` otherwise.\n\t */\n\tstatic isOCB( algorithm: Cph.AesAlgorithm ): algorithm is crypto.CipherOCBTypes\n\t{\n\t\treturn (\n\t\t\talgorithm === 'aes-128-ocb' ||\n\t\t\talgorithm === 'aes-192-ocb' ||\n\t\t\talgorithm === 'aes-256-ocb'\n\t\t)\n\t}\n\n\n\t/**\n\t * Check if the given algorithm is a Cipher AES-CBC algorithm.\n\t * \n\t * @param algorithm The AES Algorithm to check.\n\t * @returns `true` if the given algorithm is a Cipher AES-CBC algorithm. `false` otherwise.\n\t */\n\tstatic isCBC( algorithm: Cph.AesAlgorithm ): algorithm is Cph.CBCTypes\n\t{\n\t\treturn (\n\t\t\talgorithm === 'aes-128-cbc' ||\n\t\t\talgorithm === 'aes-192-cbc' ||\n\t\t\talgorithm === 'aes-256-cbc'\n\t\t)\n\t}\n}"]}
|
package/dist/index.mjs.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"names":["_Cipher","data","secret","options","_data","coerceToUint8Array","salt","Key","IV","AAD","algorithm","authTagLength","cipher","crypto","encrypted","authTag","_secret","_options","decipher","AADLength","input","output","iv","encryptedKey","length","keyIvLength","encryptedKeyIV","KeyIV","publicKey","privateKey","rsaKeyLength","resolve","reject","keyLength","bytesRead","resolved","transform","Transform","chunk","encoding","callback","bytesLoss","subchunk","error","Cipher"],"mappings":"8HAmCO,IAAMA,CAAN,CAAA,MAAMA,CACb,CAuEC,OAAO,QACNC,CACAC,CAAAA,CAAAA,CACAC,CAAwB,CAAA,GAEzB,CACC,IAAMC,EAAQC,kBAAoBJ,CAAAA,CAAK,EAEjC,CACL,IAAA,CAAAK,CAAM,CAAA,GAAA,CAAAC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d,CAAA,CACC,OACCA,CAAc,GAAA,aAAA,EACdA,IAAc,aACdA,EAAAA,CAAAA,GAAc,aAEhB,CASA,OAAO,KAAOA,CAAAA,CAAAA,CACd,CACC,OACCA,CAAAA,GAAc,eACdA,CAAc,GAAA,aAAA,EACdA,CAAc,GAAA,aAAA,EACdA,IAAc,mBAEhB,CASA,OAAO,KAAOA,CAAAA,CAAAA,CACd,CACC,OACCA,CAAAA,GAAc,aACdA,EAAAA,CAAAA,GAAc,eACdA,CAAc,GAAA,aAEhB,CASA,OAAO,MAAOA,CACd,CAAA,CACC,OACCA,CAAAA,GAAc,eACdA,CAAc,GAAA,aAAA,EACdA,IAAc,aAEhB,CACD,EA7sBaV,CAKI,CAAA,WAAA,CAAc,CAC7B,GAAA,CAAO,GACP,GAAO,CAAA,EAAA,CACP,QAAU,EACX,CAAA,CATYA,EAeI,SAAY,CAAA,CAC3B,GAAO,CAAA,CAAA,CACP,IAAO,EACP,CAAA,OAAA,CAAU,EACX,CAnBYA,CAAAA,CAAAA,CAyBI,gBAAkB,CACjC,GAAA,CAAO,CACP,CAAA,GAAA,CAAO,GACP,OAAU,CAAA,EACX,EA7BYA,CAmCI,CAAA,UAAA,CAAa,CAC5B,GAAO,CAAA,EAAA,CACP,GAAO,CAAA,GAAA,CACP,QAAU,EACX,CAAA,CAvCYA,EA6CI,iBAAoB,CAAA,CACnC,OAAQ,aACR,CAAA,MAAA,CAAQ,aACT,CAAA,CAhDYA,EAsDI,UAAiC,CAAA,CAChD,cAAe,aAAe,CAAA,aAAA,CAAe,cAC7C,aAAe,CAAA,aAAA,CAAe,aAAe,CAAA,aAAA,CAC7C,cAAe,aAAe,CAAA,aAAA,CAAe,cAC7C,mBACD,CAAA,KA3DY4C,CAAN5C,CAAAA","file":"index.mjs","sourcesContent":["import crypto from 'crypto'\nimport { Transform } from 'stream'\nimport type { Readable, Writable } from 'stream'\nimport { coerceToUint8Array, type CoerceToUint8ArrayInput } from '@alessiofrittoli/crypto-buffer/coercion'\n\nimport type { Cph } from './types'\n\n\n\n/**\n * INTERNAL USE ONLY\n */\ninterface StreamEncryptOptions\n{\n\tcipher\t\t: crypto.Cipher\n\tencryptedKey: Buffer\n\tinput\t\t: Readable\n\toutput\t\t: Writable\n}\n\n\n/**\n * INTERNAL USE ONLY\n */\ninterface StreamDecryptOptions\n{\n\tdecipher\t: crypto.Decipher\n\tinput\t\t: Readable\n\toutput\t\t: Writable\n}\n\n\n/**\n * Utility class for AES encryption and decryption following [NIST SP 800-38D](https://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38d.pdf) standard reccomendations.\n */\nexport class Cipher\n{\n\t/**\n\t * Cipher default `salt` lengths.\n\t */\n\tstatic readonly SALT_LENGTH = {\n\t\tmin\t\t: 16,\n\t\tmax\t\t: 64,\n\t\tdefault\t: 32,\n\t} as const\n\n\n\t/**\n\t * Cipher default `IV` lengths.\n\t */\n\tstatic readonly IV_LENGTH = {\n\t\tmin\t\t: 8,\n\t\tmax\t\t: 32,\n\t\tdefault\t: 16,\n\t} as const\n\n\n\t/**\n\t * Cipher default `Auth Tag` lengths.\n\t */\n\tstatic readonly AUTH_TAG_LENGTH = {\n\t\tmin\t\t: 4,\n\t\tmax\t\t: 16,\n\t\tdefault\t: 16,\n\t} as const\n\t\n\t\n\t/**\n\t * Cipher default `Additional Authenticated Data` lengths.\n\t */\n\tstatic readonly AAD_LENGTH = {\n\t\tmin\t\t: 16,\n\t\tmax\t\t: 128,\n\t\tdefault\t: 32,\n\t} as const\n\t\n\n\t/**\n\t * Default AES algorithms used based on functionality.\n\t */\n\tstatic readonly DEFAULT_ALGORITHM = {\n\t\tbuffer: 'aes-256-gcm',\n\t\tstream: 'aes-256-cbc',\n\t} as const\n\n\n\t/**\n\t * Supported AES algorithms.\n\t */\n\tstatic readonly ALGORITHMS: Cph.AesAlgorithm[] = [\n\t\t'aes-128-cbc', 'aes-128-ccm', 'aes-128-gcm', 'aes-128-ocb',\n\t\t'aes-192-cbc', 'aes-192-ccm', 'aes-192-gcm', 'aes-192-ocb',\n\t\t'aes-256-cbc', 'aes-256-ccm', 'aes-256-gcm', 'aes-256-ocb',\n\t\t'chacha20-poly1305'\n\t] as const\n\n\n\t/**\n\t * Encrypt in-memory data buffer.\n\t *\n\t * ⚠️ This is not suitable for large data. Use {@link Cipher.streamEncrypt()} or {@link Cipher.hybridEncrypt()} methods for large data encryption.\n\t *\n\t * @param\tdata\tThe data to encrypt.\n\t * @param\tsecret\tThe secret key used to encrypt the `data`.\n\t * @param\toptions\t( Optional ) Additional options.\n\t * @returns\tThe encrypted result Buffer.\n\t */\n\tstatic encrypt(\n\t\tdata\t: CoerceToUint8ArrayInput,\n\t\tsecret\t: CoerceToUint8ArrayInput,\n\t\toptions\t: Cph.Options = {},\n\t): Buffer\n\t{\n\t\tconst _data = coerceToUint8Array( data )\n\n\t\tconst {\n\t\t\tsalt, Key, IV, AAD,\n\t\t\toptions: { algorithm, authTag: authTagLength },\n\t\t} = Cipher.newKeyIV( secret, options )\n\n\t\tif ( Cipher.isCBC( algorithm ) ) {\n\t\t\tconst cipher\t= crypto.createCipheriv( algorithm, Key, IV )\n\t\t\tconst encrypted\t= Buffer.concat( [ cipher.update( _data ), cipher.final() ] )\n\n\t\t\treturn Buffer.concat( [ salt, IV, encrypted ] )\n\t\t}\n\n\n\t\tconst cipher = (\n\t\t\tcrypto.createCipheriv(\n\t\t\t\t// @ts-expect-error `crypto.createCipheriv` cannot infer the correct overload based on the given union type algorithm.\n\t\t\t\talgorithm, Key, IV, { authTagLength }\n\t\t\t)\n\t\t)\n\t\t\n\t\t\n\t\tif ( Cipher.isCCM( algorithm ) ) {\n\t\t\t// AES-CCM requires `plaintextLength`\n\t\t\tcipher.setAAD( AAD, { plaintextLength: _data.length } )\n\t\t}\n\n\t\tif ( Cipher.isGCM( algorithm ) || Cipher.isOCB( algorithm ) ) {\n\t\t\t// AES-GCM and AES-OCB doesn't require `plaintextLength`\n\t\t\tcipher.setAAD( AAD )\n\t\t}\n\n\t\tconst encrypted\t= Buffer.concat( [ cipher.update( _data ), cipher.final() ] )\n\t\tconst authTag\t= cipher.getAuthTag()\n\n\t\treturn Buffer.concat( [ salt, IV, AAD, authTag, encrypted ] )\n\t}\n\n\n\t/**\n\t * Decrypt in-memory data buffer.\n\t *\n\t * ⚠️ This is not suitable for large data. Use {@link Cipher.streamDecrypt()} or {@link Cipher.hybridDecrypt()} methods for large data decryption.\n\t *\n\t * @param\tdata\tThe data to decrypt.\n\t * @param\tsecret\tThe secret key used to decrypt the `data`.\n\t * @param\toptions\t( Optional ) Additional options. Must be the same used while encrypting data.\n\t * @returns\tThe decrypted data Buffer.\n\t */\n\tstatic decrypt(\n\t\tdata\t: CoerceToUint8ArrayInput,\n\t\tsecret\t: CoerceToUint8ArrayInput,\n\t\toptions\t: Cph.Options = {},\n\t): Buffer\n\t{\n\t\tlet _data\t\t\t= coerceToUint8Array( data )\n\t\tconst _secret\t\t= coerceToUint8Array( secret )\n\t\tconst _options\t\t= Cipher.resolveOptions( options )\n\t\tconst { algorithm }\t= _options\n\n\t\tconst salt\t\t= _data.subarray( 0, _options.salt )\n\t\t_data\t\t\t= _data.subarray( _options.salt )\n\t\tconst IV\t\t= _data.subarray( 0, _options.iv )\n\t\t_data\t\t\t= _data.subarray( _options.iv )\n\t\tconst Key\t\t= crypto.scryptSync( _secret, salt, _options.length )\n\n\t\tif ( Cipher.isCBC( algorithm ) ) {\n\t\t\tconst decipher = (\n\t\t\t\tcrypto.createDecipheriv( _options.algorithm, Key, IV )\n\t\t\t)\n\n\t\t\treturn (\n\t\t\t\tBuffer.concat( [ decipher.update( _data ), decipher.final() ] )\n\t\t\t)\n\t\t}\n\n\t\tconst AADLength\t\t= _options.aadLength\n\t\tconst authTagLength\t= _options.authTag\n\t\tconst AAD\t\t\t= _data.subarray( 0, AADLength )\n\t\t_data\t\t\t\t= _data.subarray( AADLength )\n\t\tconst authTag\t\t= _data.subarray( 0, authTagLength )\n\t\t_data\t\t\t\t= _data.subarray( authTagLength )\n\n\n\t\tconst decipher = (\n\t\t\tcrypto.createDecipheriv(\n\t\t\t\t// @ts-expect-error `crypto.createDecipheriv` cannot infer the correct overload based on the given union type algorithm.\n\t\t\t\talgorithm, Key, IV, { authTagLength }\n\t\t\t)\n\t\t)\n\n\t\tif ( Cipher.isCCM( algorithm ) ) {\n\t\t\t// AES-CCM requires `plaintextLength`\n\t\t\tdecipher.setAAD( AAD, { plaintextLength: _data.length } )\n\t\t}\n\n\t\tif ( Cipher.isGCM( algorithm ) || Cipher.isOCB( algorithm ) ) {\n\t\t\t// AES-GCM and AES-OCB doesn't require `plaintextLength`\n\t\t\tdecipher.setAAD( AAD )\n\t\t}\n\n\t\tdecipher.setAuthTag( authTag )\n\t\n\t\treturn (\n\t\t\tBuffer.concat( [ decipher.update( _data ), decipher.final() ] )\n\t\t)\n\n\t}\n\n\n\t/**\n\t * Encrypt a `Readable` to a `Writable` Stream.\n\t * \n\t * The `Readable` Stream could be 'in-memory buffer' or 'file' based.\n\t * \n\t * @param\tsecret\tThe secret key used to encrypt the `data`.\n\t * @param\toptions Additional options.\n\t * @returns An object containing:\n\t * \t- a new instance of `crypto.Cipher` allowing you to add listeners to the `cipher` encryption process.\n\t * \t- the actual `encrypt` callback that must be called and awaited in order to start the encryption process.\n\t */\n\tstatic streamEncrypt(\n\t\tsecret\t: CoerceToUint8ArrayInput,\n\t\toptions\t: Cph.Stream.Symmetric.EncryptOptions,\n\t): Cph.Stream.Symmetric.EncryptReturnType\n\t{\n\t\toptions.algorithm ||= Cipher.DEFAULT_ALGORITHM.stream\n\n\t\tconst {\n\t\t\tKey, IV, options: { input, output, algorithm, salt, iv, authTag },\n\t\t} = Cipher.newKeyIV<Cph.Stream.Symmetric.EncryptResolvedOptions>( secret, options )\n\n\t\tconst encryptedKey = (\n\t\t\tCipher.encrypt( Buffer.concat( [ Key, IV ] ), secret, { algorithm, salt, iv, authTag } )\n\t\t)\n\n\t\tconst cipher = crypto.createCipheriv( algorithm, Key, IV )\n\t\tconst encrypt = () => (\n\t\t\tCipher.stream( {\n\t\t\t\tcipher, input, output, encryptedKey\n\t\t\t} )\n\t\t)\n\n\t\treturn { cipher, encrypt }\n\t}\n\n\n\t/**\n\t * Decrypt a `Readable` to a `Writable` Stream.\n\t * \n\t * The `Readable` Stream could be 'in-memory buffer' or 'file' based.\n\t * \n\t * @param\tsecret\tThe secret key used to encrypt the `data`.\n\t * @param\toptions Additional options.\n\t * @returns A new Promise that resolves when Key IV extraction completes returning an object containing:\n\t * \t- a new instance of `crypto.Decipher` allowing you to add listeners to the `decipher` decryption process.\n\t * \t- the actual `decrypt` callback that must be called and awaited in order to start the decryption process.\n\t */\n\tstatic streamDecrypt(\n\t\tsecret\t: CoerceToUint8ArrayInput,\n\t\toptions\t: Cph.Stream.Symmetric.DecryptOptions,\n\t): Promise<Cph.Stream.Symmetric.DecryptReturnType>\n\t{\n\t\toptions.algorithm ||= Cipher.DEFAULT_ALGORITHM.stream\n\n\t\tconst {\n\t\t\tinput, output, algorithm, salt, iv, authTag, length\n\t\t} = Cipher.resolveOptions<Cph.Stream.Symmetric.DecryptResolvedOptions>( options )\n\n\t\tconst keyIvLength = (\n\t\t\tlength + iv\n\t\t\t+ salt + iv + authTag\n\t\t)\n\n\t\treturn (\n\t\t\tCipher.extractKeyIV( input, keyIvLength )\n\t\t\t\t.then( ( [ encryptedKeyIV, input ] ) => {\n\t\t\t\t\t/**\n\t\t\t\t\t * Check if input has error and re-throw if so.\n\t\t\t\t\t * This is required since `.on( 'error' )` listeners attached in\n\t\t\t\t\t * `Cipher.decipherStream()` get attached too late (error event already emitted).\n\t\t\t\t\t */\n\t\t\t\t\tif ( input.errored ) throw input.errored\n\t\t\t\t\t\n\t\t\t\t\tconst KeyIV = (\n\t\t\t\t\t\tCipher.decrypt( encryptedKeyIV, secret, { algorithm, salt, iv, authTag } )\n\t\t\t\t\t)\n\n\t\t\t\t\tconst Key\t\t= KeyIV.subarray( 0, length )\n\t\t\t\t\tconst IV\t\t= KeyIV.subarray( length )\n\t\t\t\t\tconst decipher\t= crypto.createDecipheriv( algorithm, Key, IV )\n\t\n\t\t\t\t\tconst decrypt = () => (\n\t\t\t\t\t\tCipher.decipherStream(\n\t\t\t\t\t\t\t{ decipher, input, output }\n\t\t\t\t\t\t)\n\t\t\t\t\t)\n\t\t\t\n\t\t\t\t\treturn { decipher, decrypt }\n\t\t\t\t} )\n\t\t)\n\t}\n\n\n\t/**\n\t * Encrypt a `Readable` to a `Writable` Stream with hybird Encryption.\n\t *\n\t * The `Readable` Stream could be 'in-memory buffer' or 'file' based.\n\t *\n\t * @param\tsecret\t\tThe secret key used to encrypt the stream.\n\t * @param\tpublicKey\tThe RSA public key used to encrypt the symmetric key.\n\t * @param\toptions\t\tOptions for the stream encryption.\n\t * @returns An object containing:\n\t * \t- a new instance of `crypto.Cipher` allowing you to add listeners to the `cipher` encryption process.\n\t * \t- the actual `encrypt` callback that must be called and awaited in order to start the encryption process.\n\t */\n\tstatic hybridEncrypt(\n\t\tsecret\t\t: CoerceToUint8ArrayInput,\n\t\tpublicKey\t: crypto.RsaPublicKey | crypto.RsaPrivateKey | crypto.KeyLike,\n\t\toptions\t\t: Cph.Stream.Hybrid.EncryptOptions,\n\t): Cph.Stream.Hybrid.EncryptReturnType\n\t{\n\t\toptions.algorithm ||= Cipher.DEFAULT_ALGORITHM.stream\n\n\t\tconst {\n\t\t\tKey, IV,\n\t\t\toptions: { algorithm, input, output },\n\t\t} = Cipher.newKeyIV<Cph.Stream.Hybrid.EncryptResolvedOptions>( secret, options )\n\n\t\tconst encryptedKey = (\n\t\t\tcrypto.publicEncrypt( publicKey, Buffer.concat( [ Key, IV ] ) )\n\t\t)\n\n\t\tconst cipher = crypto.createCipheriv( algorithm, Key, IV )\n\t\tconst encrypt = () => (\n\t\t\tCipher.stream( {\n\t\t\t\tcipher, input, output, encryptedKey\n\t\t\t} )\n\t\t)\n\n\t\treturn { cipher, encrypt }\n\t}\n\n\n\t/**\n\t * Decrypt a `Readable` to a `Writable` Stream with hybrid Decryption.\n\t * \n\t * The `Readable` Stream could be 'in-memory buffer' or 'file' based.\n\t *\n\t * @param\tprivateKey\tThe RSA private key used to decrypt the symmetric key.\n\t * @param\toptions\t\tOptions for the stream decryption.\n\t * @returns A new Promise that resolves when Key IV extraction completes returning an object containing:\n\t * \t- a new instance of `crypto.Decipher` allowing you to add listeners to the `decipher` decryption process.\n\t * \t- the actual `decrypt` callback that must be called and awaited in order to start the decryption process.\n\t */\n\tstatic hybridDecrypt(\n\t\tprivateKey\t: crypto.RsaPrivateKey | crypto.KeyLike,\n\t\toptions\t\t: Cph.Stream.Hybrid.DecryptOptions,\n\t): Promise<Cph.Stream.Hybrid.DecryptReturnType>\n\t{\n\t\toptions.algorithm ||= Cipher.DEFAULT_ALGORITHM.stream\n\n\t\tconst {\n\t\t\tinput, output, algorithm, length, rsaKeyLength,\n\t\t} = Cipher.resolveOptions<Cph.Stream.Hybrid.DecryptResolvedOptions>( options )\n\n\t\treturn (\n\t\t\tCipher.extractKeyIV( input, rsaKeyLength )\n\t\t\t\t.then( ( [ encryptedKeyIV, input ] ) => {\n\t\t\t\t\tconst KeyIV\t\t= crypto.privateDecrypt( privateKey, encryptedKeyIV )\n\t\t\t\t\tconst Key\t\t= KeyIV.subarray( 0, length )\n\t\t\t\t\tconst IV\t\t= KeyIV.subarray( length )\n\t\t\t\t\tconst decipher\t= crypto.createDecipheriv( algorithm, Key, IV )\n\t\n\t\t\t\t\tconst decrypt = () => (\n\t\t\t\t\t\tCipher.decipherStream(\n\t\t\t\t\t\t\t{ decipher, input, output }\n\t\t\t\t\t\t)\n\t\t\t\t\t)\n\t\t\t\n\t\t\t\t\treturn { decipher, decrypt }\n\t\t\t\t} )\n\t\t)\n\t}\n\n\n\t/**\n\t * Handle pipe flow to encrypt a Stream.\n\t * \n\t * @param\toptions Required parameters.\n\t * @returns A new Promise that resolves `void` once stream is completed.\n\t */\n\tprivate static stream( options: StreamEncryptOptions )\n\t{\n\t\tconst {\n\t\t\tcipher, encryptedKey,\n\t\t\tinput, output\n\t\t} = options\n\n\t\treturn (\n\t\t\tnew Promise<void>( ( resolve, reject ) => {\n\t\t\t\tcipher.on( 'error', reject )\n\t\t\t\tinput.on( 'error', reject )\n\t\t\t\toutput.on( 'error', reject )\n\t\t\t\toutput.on( 'finish', resolve )\n\t\t\n\t\t\t\toutput.write( encryptedKey )\n\t\t\t\tinput.pipe( cipher ).pipe( output )\n\t\t\t} )\n\t\t)\n\t}\n\n\n\t/**\n\t * Handle pipe flow to decrypt a Stream.\n\t * \n\t * @param\toptions Required parameters.\n\t * @returns A new Promise that resolves `void` once stream is completed.\n\t */\n\tprivate static decipherStream( options: StreamDecryptOptions )\n\t{\n\t\tconst {\n\t\t\tdecipher, input, output,\n\t\t} = options\n\n\t\treturn (\n\t\t\tnew Promise<void>( ( resolve, reject ) => {\n\t\t\t\tdecipher.on( 'error', reject )\n\t\t\t\tinput.on( 'error', reject )\n\t\t\t\toutput.on( 'error', reject )\n\t\t\t\toutput.on( 'finish', resolve )\n\n\t\t\t\tinput.pipe( decipher ).pipe( output )\n\t\t\t} )\n\t\t)\n\t}\n\n\n\t/**\n\t * Extract the Cipher Encrypted Symmetric Key and Initialization Vector from an encrypted `Readable` Stream.\n\t *\n\t * @param\tinput\t\tThe `Readable` Stream.\n\t * @param\tkeyLength\tThe encrypted key length in bytes. This is used to properly extract the encrypted Cipher Key and Initialization Vector.\n\t * @returns\tA new Promise that resolve a tuple containing the Cipher Encrypted Symmetric Key and Initialization Vector once fulfilled.\n\t */\n\tprivate static extractKeyIV(\n\t\tinput\t\t: Readable,\n\t\tkeyLength\t: number,\n\t)\n\t{\n\t\treturn (\n\t\t\tnew Promise<[ KeyIV: Buffer, input: Transform ]>( ( resolve, reject ) => {\n\t\t\t\tlet KeyIV\t\t= Buffer.alloc( 0 )\n\t\t\t\tlet bytesRead\t= 0\n\t\t\t\tlet resolved\t= false\n\n\t\t\t\tconst transform = new Transform( {\n\t\t\t\t\tasync transform( chunk: Buffer, encoding, callback )\n\t\t\t\t\t{\n\n\t\t\t\t\t\tif ( KeyIV.length < keyLength ) {\n\t\t\t\t\t\t\tKeyIV = Buffer.concat( [ KeyIV, chunk ] )\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tbytesRead += chunk.length\n\n\t\t\t\t\t\t/**\n\t\t\t\t\t\t * `KeyIV` length may exceed the `keyLength`.\n\t\t\t\t\t\t * This may occurs when the received chunk length is not a multiple of the `keyLength`.\n\t\t\t\t\t\t *\n\t\t\t\t\t\t * In that case the chunk will contain mixed content:\n\t\t\t\t\t\t * - end of the `KeyIV`.\n\t\t\t\t\t\t * - begin of the actual encrypted data.\n\t\t\t\t\t\t *\n\t\t\t\t\t\t * For a proper data handling we need to:\n\t\t\t\t\t\t * - cut off the `KeyIV` to `keyLength` bytes.\n\t\t\t\t\t\t * - recover and push the byte loss to the next piped destination.\n\t\t\t\t\t\t */\n\t\t\t\t\t\tif ( KeyIV.length > keyLength ) {\n\t\t\t\t\t\t\tconst bytesLoss\t= Math.max( 0, KeyIV.length - keyLength )\n\t\t\t\t\t\t\tKeyIV\t\t\t= KeyIV.subarray( 0, keyLength )\n\t\t\t\t\t\t\tconst subchunk\t= chunk.subarray( bytesLoss * -1 )\n\n\t\t\t\t\t\t\tif ( ! resolved ) {\n\t\t\t\t\t\t\t\tresolve( [ KeyIV, this ] )\n\t\t\t\t\t\t\t\tresolved = true\n\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\t/**\n\t\t\t\t\t\t\t * Push to the next piped destination.\n\t\t\t\t\t\t\t */\t\t\t\t\t\t\t\n\t\t\t\t\t\t\tthis.push( subchunk, encoding )\n\n\t\t\t\t\t\t\treturn callback()\n\t\t\t\t\t\t}\n\n\n\t\t\t\t\t\t/**\n\t\t\t\t\t\t * `KeyIV` has been read.\n\t\t\t\t\t\t * The Received chunk can now be pushed to the next piped destination.\n\t\t\t\t\t\t */\n\t\t\t\t\t\tif ( bytesRead > keyLength ) {\n\t\t\t\t\t\t\tif ( ! resolved ) {\n\t\t\t\t\t\t\t\tresolve( [ KeyIV, this ] )\n\t\t\t\t\t\t\t\tresolved = true\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t/**\n\t\t\t\t\t\t\t * Push to the next piped destination.\n\t\t\t\t\t\t\t */\n\t\t\t\t\t\t\tthis.push( chunk, encoding )\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\treturn callback()\n\t\t\t\t\t},\n\t\t\t\t\tfinal( callback )\n\t\t\t\t\t{\n\t\t\t\t\t\tif ( KeyIV.length < keyLength ) {\n\t\t\t\t\t\t\treturn callback(\n\t\t\t\t\t\t\t\tnew Error( 'The extracted KeyIV length is less than the expected length.' )\n\t\t\t\t\t\t\t)\n\t\t\t\t\t\t}\n\t\t\t\t\t\treturn callback()\n\t\t\t\t\t},\n\t\t\t\t} )\n\n\t\t\t\ttransform.on( 'error', reject )\n\t\t\t\tinput.on( 'error', error => {\n\t\t\t\t\ttransform.destroy( error )\n\t\t\t\t\treject( error )\n\t\t\t\t} )\n\t\t\t\tinput.pipe( transform )\n\t\t\t} )\n\t\t)\n\t}\n\n\n\t/**\n\t * Generates a `Scrypt` Symmetric Key and the Initialization Vector with the given options.\n\t * \n\t * @param\tsecret\tThe secret key.\n\t * @param\toptions\t( Optional ) Additional options.\n\t * \n\t * @returns An object with the generated `Key`, `IV`, `salt` and resolved `options`. \n\t */\n\tstatic newKeyIV<T extends Cph.ResolvedOptions = Cph.ResolvedOptions>(\n\t\tsecret\t: CoerceToUint8ArrayInput,\n\t\toptions\t: Cph.Options = {},\n\t)\n\t{\n\t\tconst _secret\t= coerceToUint8Array( secret )\n\t\tconst _options\t= Cipher.resolveOptions<T>( options )\n\t\tconst salt\t\t= crypto.randomBytes( _options.salt )\n\t\tconst Key\t\t= crypto.scryptSync( _secret, salt, _options.length )\n\t\tconst IV\t\t= crypto.randomBytes( _options.iv )\n\t\tconst AAD\t\t= _options.aad || crypto.scryptSync( Key, salt, _options.aadLength )\n\n\t\treturn { options: _options, Key, IV, AAD, salt }\n\t}\n\n\n\t/**\n\t * Resolves the given `options` with `Cipher` defaults and constraints.\n\t * \n\t * @param options ( Optional ) Additional options.\n\t * @returns The given `options` with `Cipher` defaults and constraints.\n\t */\n\tstatic resolveOptions<\n\t\tT extends Cph.ResolvedOptions = Cph.ResolvedOptions\n\t>( options: Cph.Options = {} ): T\n\t{\n\t\tconst _options = { ...options } as T\n\n\t\tif ( _options.aad ) {\n\t\t\t_options.aad = coerceToUint8Array( _options.aad )\n\t\t}\n\n\t\t_options.salt\t\t||= Cipher.SALT_LENGTH.default\n\t\t_options.authTag\t||= Cipher.AUTH_TAG_LENGTH.default\n\t\t_options.aadLength\t= _options.aad?.length || _options.aadLength || Cipher.AAD_LENGTH.default\n\t\t_options.salt\t\t= Math.min( Math.max( _options.salt, Cipher.SALT_LENGTH.min ), Cipher.SALT_LENGTH.max )\n\t\t_options.authTag\t= Math.min( Math.max( _options.authTag, Cipher.AUTH_TAG_LENGTH.min ), Cipher.AUTH_TAG_LENGTH.max )\n\t\tif ( ! _options.aad ) {\n\t\t\t_options.aadLength\t= Math.min( Math.max( _options.aadLength, Cipher.AAD_LENGTH.min ), Cipher.AAD_LENGTH.max )\n\t\t}\n\n\t\tconst { keyLength, algorithm } = Cipher.getKeyLength( options.algorithm )\n\n\t\t_options.algorithm\t= algorithm\n\t\t_options.length\t\t= keyLength\n\t\t_options.iv\t\t\t= Cipher.getIVLength( algorithm, options )\n\n\t\treturn _options\n\t}\n\n\n\t/**\n\t * Get the Initialization Vector length based on the given algorithm.\n\t * \n\t * @param\talgorithm The algorithm in use.\n\t * @param\toptions ( Optional ) Additional options. \n\t * @returns\tThe Initialization Vector length based on the given algorithm\n\t */\n\tprivate static getIVLength(\n\t\talgorithm\t: Cph.Options[ 'algorithm' ] = Cipher.DEFAULT_ALGORITHM.buffer,\n\t\toptions\t\t: Cph.Options = {},\n\t)\n\t{\n\t\tswitch ( algorithm ) {\n\t\t\tcase 'aes-128-ccm':\n\t\t\tcase 'aes-128-ocb':\n\t\t\tcase 'aes-192-ccm':\n\t\t\tcase 'aes-192-ocb':\n\t\t\tcase 'aes-256-ccm':\n\t\t\tcase 'aes-256-ocb':\n\t\t\t\treturn 8\n\t\t\tcase 'chacha20-poly1305':\n\t\t\t\treturn 12\n\t\t\tdefault:\n\t\t\t\treturn (\n\t\t\t\t\tMath.min(\n\t\t\t\t\t\tMath.max(\n\t\t\t\t\t\t\toptions.iv || Cipher.IV_LENGTH.default,\n\t\t\t\t\t\t\tCipher.IV_LENGTH.min\n\t\t\t\t\t\t), Cipher.IV_LENGTH.max\n\t\t\t\t\t)\n\t\t\t\t)\n\t\t}\n\t}\n\n\n\t/**\n\t * Ensure correct key length based on the given algorithm.\n\t *\n\t * @param\talgorithm The AES algorithm name.\n\t * @returns\tAn object with validated `algorithm` and `keyLength`.\n\t */\n\tprivate static getKeyLength(\n\t\talgorithm: Cph.Options[ 'algorithm' ] = Cipher.DEFAULT_ALGORITHM.buffer\n\t)\n\t{\n\t\tswitch ( algorithm ) {\n\t\t\tcase 'chacha20-poly1305':\n\t\t\t\treturn {\n\t\t\t\t\talgorithm: algorithm,\n\t\t\t\t\tkeyLength: 256 / 8,\n\t\t\t\t} as const\n\t\t\tcase 'aes-128-cbc':\n\t\t\tcase 'aes-128-ccm':\n\t\t\tcase 'aes-128-gcm':\n\t\t\tcase 'aes-128-ocb':\n\t\t\t\treturn {\n\t\t\t\t\talgorithm: algorithm,\n\t\t\t\t\tkeyLength: 128 / 8, // AES-128 needs a 128 bit (16 bytes)\n\t\t\t\t} as const\n\t\t\tcase 'aes-192-cbc':\n\t\t\tcase 'aes-192-ccm':\n\t\t\tcase 'aes-192-gcm':\n\t\t\tcase 'aes-192-ocb':\n\t\t\t\treturn {\n\t\t\t\t\talgorithm: algorithm,\n\t\t\t\t\tkeyLength: 192 / 8, // AES-192 needs a 192 bit (24 bytes)\n\t\t\t\t} as const\n\t\t\tcase 'aes-256-cbc':\n\t\t\tcase 'aes-256-ccm':\n\t\t\tcase 'aes-256-gcm':\n\t\t\tcase 'aes-256-ocb':\n\t\t\tdefault:\n\t\t\t\treturn {\n\t\t\t\t\talgorithm: algorithm,\n\t\t\t\t\tkeyLength: 256 / 8, // AES-256 needs a 256 bit (32 bytes)\n\t\t\t\t} as const\n\t\t}\n\t}\n\n\n\t/**\n\t * Check if the given algorithm is a Cipher AES-GCM algorithm.\n\t * \n\t * @param algorithm The AES Algorithm to check.\n\t * @returns `true` if the given algorithm is a Cipher AES-GCM algorithm. `false` otherwise.\n\t */\n\tstatic isGCM( algorithm: Cph.AesAlgorithm ): algorithm is crypto.CipherGCMTypes\n\t{\n\t\treturn (\n\t\t\talgorithm === 'aes-128-gcm' ||\n\t\t\talgorithm === 'aes-192-gcm' ||\n\t\t\talgorithm === 'aes-256-gcm'\n\t\t)\n\t}\n\n\n\t/**\n\t * Check if the given algorithm is a Cipher AES-CCM algorithm.\n\t * \n\t * @param algorithm The AES Algorithm to check.\n\t * @returns `true` if the given algorithm is a Cipher AES-CCM algorithm. `false` otherwise.\n\t */\n\tstatic isCCM( algorithm: Cph.AesAlgorithm ): algorithm is crypto.CipherCCMTypes\n\t{\n\t\treturn (\n\t\t\talgorithm === 'aes-128-ccm' ||\n\t\t\talgorithm === 'aes-192-ccm' ||\n\t\t\talgorithm === 'aes-256-ccm' ||\n\t\t\talgorithm === 'chacha20-poly1305'\n\t\t)\n\t}\n\n\n\t/**\n\t * Check if the given algorithm is a Cipher AES-OCB algorithm.\n\t * \n\t * @param algorithm The AES Algorithm to check.\n\t * @returns `true` if the given algorithm is a Cipher AES-OCB algorithm. `false` otherwise.\n\t */\n\tstatic isOCB( algorithm: Cph.AesAlgorithm ): algorithm is crypto.CipherOCBTypes\n\t{\n\t\treturn (\n\t\t\talgorithm === 'aes-128-ocb' ||\n\t\t\talgorithm === 'aes-192-ocb' ||\n\t\t\talgorithm === 'aes-256-ocb'\n\t\t)\n\t}\n\n\n\t/**\n\t * Check if the given algorithm is a Cipher AES-CBC algorithm.\n\t * \n\t * @param algorithm The AES Algorithm to check.\n\t * @returns `true` if the given algorithm is a Cipher AES-CBC algorithm. `false` otherwise.\n\t */\n\tstatic isCBC( algorithm: Cph.AesAlgorithm ): algorithm is Cph.CBCTypes\n\t{\n\t\treturn (\n\t\t\talgorithm === 'aes-128-cbc' ||\n\t\t\talgorithm === 'aes-192-cbc' ||\n\t\t\talgorithm === 'aes-256-cbc'\n\t\t)\n\t}\n}"]}
|
package/dist/types.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":[],"names":[],"mappings":"","file":"types.js"}
|
package/dist/types.mjs.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":[],"names":[],"mappings":"","file":"types.mjs"}
|