@alessiofrittoli/crypto-cipher 2.2.0 → 3.0.0-alpha.1

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/dist/index.d.mts CHANGED
@@ -54,7 +54,7 @@ declare class Cipher {
54
54
  /**
55
55
  * Encrypt in-memory data buffer.
56
56
  *
57
- * ⚠️ This is not suitable for large data. Use {@link Cipher.streamEncrypt()} or {@link Cipher.hybridEncrypt()} methods for large data encryption.
57
+ * ⚠️ This is not suitable for large data. Use {@link Cipher.streamEncrypt()} or {@link Cipher.hybridStreamEncrypt()} methods for large data encryption.
58
58
  *
59
59
  * @param data The data to encrypt.
60
60
  * @param secret The secret key used to encrypt the `data`.
@@ -65,7 +65,7 @@ declare class Cipher {
65
65
  /**
66
66
  * Decrypt in-memory data buffer.
67
67
  *
68
- * ⚠️ This is not suitable for large data. Use {@link Cipher.streamDecrypt()} or {@link Cipher.hybridDecrypt()} methods for large data decryption.
68
+ * ⚠️ This is not suitable for large data. Use {@link Cipher.streamDecrypt()} or {@link Cipher.hybridStreamDecrypt()} methods for large data decryption.
69
69
  *
70
70
  * @param data The data to decrypt.
71
71
  * @param secret The secret key used to decrypt the `data`.
@@ -81,7 +81,7 @@ declare class Cipher {
81
81
  * @param secret The secret key used to encrypt the `data`.
82
82
  * @param options Additional options.
83
83
  * @returns An object containing:
84
- * - a new instance of `crypto.Cipher` allowing you to add listeners to the `cipher` encryption process.
84
+ * - a new instance of `crypto.Cipheriv` allowing you to add listeners to the `cipher` encryption process.
85
85
  * - the actual `encrypt` callback that must be called and awaited in order to start the encryption process.
86
86
  */
87
87
  static streamEncrypt(secret: CoerceToUint8ArrayInput, options: Cph.Stream.Symmetric.EncryptOptions): Cph.Stream.Symmetric.EncryptReturnType;
@@ -93,10 +93,33 @@ declare class Cipher {
93
93
  * @param secret The secret key used to encrypt the `data`.
94
94
  * @param options Additional options.
95
95
  * @returns A new Promise that resolves when Key IV extraction completes returning an object containing:
96
- * - a new instance of `crypto.Decipher` allowing you to add listeners to the `decipher` decryption process.
96
+ * - a new instance of `crypto.Decipheriv` allowing you to add listeners to the `decipher` decryption process.
97
97
  * - the actual `decrypt` callback that must be called and awaited in order to start the decryption process.
98
98
  */
99
99
  static streamDecrypt(secret: CoerceToUint8ArrayInput, options: Cph.Stream.Symmetric.DecryptOptions): Promise<Cph.Stream.Symmetric.DecryptReturnType>;
100
+ /**
101
+ * Encrypt data using hybrid encryption.
102
+ *
103
+ * @param data The data to encrypt.
104
+ * @param publicKey The public key.
105
+ * @param options ( Optional ) Additional options.
106
+ *
107
+ * @returns The encrypted result Buffer.
108
+ */
109
+ static hybridEncrypt(data: CoerceToUint8ArrayInput, publicKey: crypto.KeyLike, options?: Cph.Options): Buffer<ArrayBuffer>;
110
+ /**
111
+ * Decrypt data using hybrid decryption.
112
+ *
113
+ * @param data The encrypted data to decrypt.
114
+ * @param privateKey The private key.
115
+ * @param options ( Optional ) Additional options.
116
+ *
117
+ * @returns The encrypted result Buffer.
118
+ */
119
+ static hybridDecrypt(data: CoerceToUint8ArrayInput, privateKey: crypto.KeyLike | {
120
+ key: crypto.KeyLike;
121
+ passphrase?: string;
122
+ }, options?: Cph.Options): Buffer<ArrayBufferLike>;
100
123
  /**
101
124
  * Encrypt a `Readable` to a `Writable` Stream with hybird Encryption.
102
125
  *
@@ -106,10 +129,10 @@ declare class Cipher {
106
129
  * @param publicKey The RSA public key used to encrypt the symmetric key.
107
130
  * @param options Options for the stream encryption.
108
131
  * @returns An object containing:
109
- * - a new instance of `crypto.Cipher` allowing you to add listeners to the `cipher` encryption process.
132
+ * - a new instance of `crypto.Cipheriv` allowing you to add listeners to the `cipher` encryption process.
110
133
  * - the actual `encrypt` callback that must be called and awaited in order to start the encryption process.
111
134
  */
112
- static hybridEncrypt(secret: CoerceToUint8ArrayInput, publicKey: crypto.RsaPublicKey | crypto.RsaPrivateKey | crypto.KeyLike, options: Cph.Stream.Hybrid.EncryptOptions): Cph.Stream.Hybrid.EncryptReturnType;
135
+ static hybridStreamEncrypt(secret: CoerceToUint8ArrayInput, publicKey: crypto.RsaPublicKey | crypto.RsaPrivateKey | crypto.KeyLike, options: Cph.Stream.Hybrid.EncryptOptions): Cph.Stream.Hybrid.EncryptReturnType;
113
136
  /**
114
137
  * Decrypt a `Readable` to a `Writable` Stream with hybrid Decryption.
115
138
  *
@@ -118,10 +141,10 @@ declare class Cipher {
118
141
  * @param privateKey The RSA private key used to decrypt the symmetric key.
119
142
  * @param options Options for the stream decryption.
120
143
  * @returns A new Promise that resolves when Key IV extraction completes returning an object containing:
121
- * - a new instance of `crypto.Decipher` allowing you to add listeners to the `decipher` decryption process.
144
+ * - a new instance of `crypto.Decipheriv` allowing you to add listeners to the `decipher` decryption process.
122
145
  * - the actual `decrypt` callback that must be called and awaited in order to start the decryption process.
123
146
  */
124
- static hybridDecrypt(privateKey: crypto.RsaPrivateKey | crypto.KeyLike, options: Cph.Stream.Hybrid.DecryptOptions): Promise<Cph.Stream.Hybrid.DecryptReturnType>;
147
+ static hybridStreamDecrypt(privateKey: crypto.RsaPrivateKey | crypto.KeyLike, options: Cph.Stream.Hybrid.DecryptOptions): Promise<Cph.Stream.Hybrid.DecryptReturnType>;
125
148
  /**
126
149
  * Handle pipe flow to encrypt a Stream.
127
150
  *
@@ -143,7 +166,7 @@ declare class Cipher {
143
166
  * @param keyLength The encrypted key length in bytes. This is used to properly extract the encrypted Cipher Key and Initialization Vector.
144
167
  * @returns A new Promise that resolve a tuple containing the Cipher Encrypted Symmetric Key and Initialization Vector once fulfilled.
145
168
  */
146
- private static extractKeyIV;
169
+ private static ExtractKeyIVFromStream;
147
170
  /**
148
171
  * Generates a `Scrypt` Symmetric Key and the Initialization Vector with the given options.
149
172
  *
@@ -154,10 +177,10 @@ declare class Cipher {
154
177
  */
155
178
  static newKeyIV<T extends Cph.ResolvedOptions = Cph.ResolvedOptions>(secret: CoerceToUint8ArrayInput, options?: Cph.Options): {
156
179
  options: T;
157
- Key: Buffer<ArrayBufferLike>;
158
- IV: Buffer<ArrayBufferLike>;
159
- AAD: Buffer<ArrayBufferLike> | Uint8Array<ArrayBufferLike>;
160
- salt: Buffer<ArrayBufferLike>;
180
+ Key: NonSharedBuffer;
181
+ IV: NonSharedBuffer;
182
+ AAD: Uint8Array<ArrayBufferLike> | NonSharedBuffer;
183
+ salt: NonSharedBuffer;
161
184
  };
162
185
  /**
163
186
  * Resolves the given `options` with `Cipher` defaults and constraints.
package/dist/index.d.ts CHANGED
@@ -54,7 +54,7 @@ declare class Cipher {
54
54
  /**
55
55
  * Encrypt in-memory data buffer.
56
56
  *
57
- * ⚠️ This is not suitable for large data. Use {@link Cipher.streamEncrypt()} or {@link Cipher.hybridEncrypt()} methods for large data encryption.
57
+ * ⚠️ This is not suitable for large data. Use {@link Cipher.streamEncrypt()} or {@link Cipher.hybridStreamEncrypt()} methods for large data encryption.
58
58
  *
59
59
  * @param data The data to encrypt.
60
60
  * @param secret The secret key used to encrypt the `data`.
@@ -65,7 +65,7 @@ declare class Cipher {
65
65
  /**
66
66
  * Decrypt in-memory data buffer.
67
67
  *
68
- * ⚠️ This is not suitable for large data. Use {@link Cipher.streamDecrypt()} or {@link Cipher.hybridDecrypt()} methods for large data decryption.
68
+ * ⚠️ This is not suitable for large data. Use {@link Cipher.streamDecrypt()} or {@link Cipher.hybridStreamDecrypt()} methods for large data decryption.
69
69
  *
70
70
  * @param data The data to decrypt.
71
71
  * @param secret The secret key used to decrypt the `data`.
@@ -81,7 +81,7 @@ declare class Cipher {
81
81
  * @param secret The secret key used to encrypt the `data`.
82
82
  * @param options Additional options.
83
83
  * @returns An object containing:
84
- * - a new instance of `crypto.Cipher` allowing you to add listeners to the `cipher` encryption process.
84
+ * - a new instance of `crypto.Cipheriv` allowing you to add listeners to the `cipher` encryption process.
85
85
  * - the actual `encrypt` callback that must be called and awaited in order to start the encryption process.
86
86
  */
87
87
  static streamEncrypt(secret: CoerceToUint8ArrayInput, options: Cph.Stream.Symmetric.EncryptOptions): Cph.Stream.Symmetric.EncryptReturnType;
@@ -93,10 +93,33 @@ declare class Cipher {
93
93
  * @param secret The secret key used to encrypt the `data`.
94
94
  * @param options Additional options.
95
95
  * @returns A new Promise that resolves when Key IV extraction completes returning an object containing:
96
- * - a new instance of `crypto.Decipher` allowing you to add listeners to the `decipher` decryption process.
96
+ * - a new instance of `crypto.Decipheriv` allowing you to add listeners to the `decipher` decryption process.
97
97
  * - the actual `decrypt` callback that must be called and awaited in order to start the decryption process.
98
98
  */
99
99
  static streamDecrypt(secret: CoerceToUint8ArrayInput, options: Cph.Stream.Symmetric.DecryptOptions): Promise<Cph.Stream.Symmetric.DecryptReturnType>;
100
+ /**
101
+ * Encrypt data using hybrid encryption.
102
+ *
103
+ * @param data The data to encrypt.
104
+ * @param publicKey The public key.
105
+ * @param options ( Optional ) Additional options.
106
+ *
107
+ * @returns The encrypted result Buffer.
108
+ */
109
+ static hybridEncrypt(data: CoerceToUint8ArrayInput, publicKey: crypto.KeyLike, options?: Cph.Options): Buffer<ArrayBuffer>;
110
+ /**
111
+ * Decrypt data using hybrid decryption.
112
+ *
113
+ * @param data The encrypted data to decrypt.
114
+ * @param privateKey The private key.
115
+ * @param options ( Optional ) Additional options.
116
+ *
117
+ * @returns The encrypted result Buffer.
118
+ */
119
+ static hybridDecrypt(data: CoerceToUint8ArrayInput, privateKey: crypto.KeyLike | {
120
+ key: crypto.KeyLike;
121
+ passphrase?: string;
122
+ }, options?: Cph.Options): Buffer<ArrayBufferLike>;
100
123
  /**
101
124
  * Encrypt a `Readable` to a `Writable` Stream with hybird Encryption.
102
125
  *
@@ -106,10 +129,10 @@ declare class Cipher {
106
129
  * @param publicKey The RSA public key used to encrypt the symmetric key.
107
130
  * @param options Options for the stream encryption.
108
131
  * @returns An object containing:
109
- * - a new instance of `crypto.Cipher` allowing you to add listeners to the `cipher` encryption process.
132
+ * - a new instance of `crypto.Cipheriv` allowing you to add listeners to the `cipher` encryption process.
110
133
  * - the actual `encrypt` callback that must be called and awaited in order to start the encryption process.
111
134
  */
112
- static hybridEncrypt(secret: CoerceToUint8ArrayInput, publicKey: crypto.RsaPublicKey | crypto.RsaPrivateKey | crypto.KeyLike, options: Cph.Stream.Hybrid.EncryptOptions): Cph.Stream.Hybrid.EncryptReturnType;
135
+ static hybridStreamEncrypt(secret: CoerceToUint8ArrayInput, publicKey: crypto.RsaPublicKey | crypto.RsaPrivateKey | crypto.KeyLike, options: Cph.Stream.Hybrid.EncryptOptions): Cph.Stream.Hybrid.EncryptReturnType;
113
136
  /**
114
137
  * Decrypt a `Readable` to a `Writable` Stream with hybrid Decryption.
115
138
  *
@@ -118,10 +141,10 @@ declare class Cipher {
118
141
  * @param privateKey The RSA private key used to decrypt the symmetric key.
119
142
  * @param options Options for the stream decryption.
120
143
  * @returns A new Promise that resolves when Key IV extraction completes returning an object containing:
121
- * - a new instance of `crypto.Decipher` allowing you to add listeners to the `decipher` decryption process.
144
+ * - a new instance of `crypto.Decipheriv` allowing you to add listeners to the `decipher` decryption process.
122
145
  * - the actual `decrypt` callback that must be called and awaited in order to start the decryption process.
123
146
  */
124
- static hybridDecrypt(privateKey: crypto.RsaPrivateKey | crypto.KeyLike, options: Cph.Stream.Hybrid.DecryptOptions): Promise<Cph.Stream.Hybrid.DecryptReturnType>;
147
+ static hybridStreamDecrypt(privateKey: crypto.RsaPrivateKey | crypto.KeyLike, options: Cph.Stream.Hybrid.DecryptOptions): Promise<Cph.Stream.Hybrid.DecryptReturnType>;
125
148
  /**
126
149
  * Handle pipe flow to encrypt a Stream.
127
150
  *
@@ -143,7 +166,7 @@ declare class Cipher {
143
166
  * @param keyLength The encrypted key length in bytes. This is used to properly extract the encrypted Cipher Key and Initialization Vector.
144
167
  * @returns A new Promise that resolve a tuple containing the Cipher Encrypted Symmetric Key and Initialization Vector once fulfilled.
145
168
  */
146
- private static extractKeyIV;
169
+ private static ExtractKeyIVFromStream;
147
170
  /**
148
171
  * Generates a `Scrypt` Symmetric Key and the Initialization Vector with the given options.
149
172
  *
@@ -154,10 +177,10 @@ declare class Cipher {
154
177
  */
155
178
  static newKeyIV<T extends Cph.ResolvedOptions = Cph.ResolvedOptions>(secret: CoerceToUint8ArrayInput, options?: Cph.Options): {
156
179
  options: T;
157
- Key: Buffer<ArrayBufferLike>;
158
- IV: Buffer<ArrayBufferLike>;
159
- AAD: Buffer<ArrayBufferLike> | Uint8Array<ArrayBufferLike>;
160
- salt: Buffer<ArrayBufferLike>;
180
+ Key: NonSharedBuffer;
181
+ IV: NonSharedBuffer;
182
+ AAD: Uint8Array<ArrayBufferLike> | NonSharedBuffer;
183
+ salt: NonSharedBuffer;
161
184
  };
162
185
  /**
163
186
  * Resolves the given `options` with `Cipher` defaults and constraints.
package/dist/index.js CHANGED
@@ -1 +1 @@
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;
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 _conversion = require('@alessiofrittoli/crypto-buffer/conversion');var _coercion = require('@alessiofrittoli/crypto-buffer/coercion');var a=class a{static encrypt(r,e,i={}){let t=_coercion.coerceToUint8Array.call(void 0, r),{salt:s,Key:c,IV:n,AAD:p,options:{algorithm:o,authTag:h}}=a.newKeyIV(e,i);if(a.isCBC(o)){let f=_crypto2.default.createCipheriv(o,c,n),l=Buffer.concat([f.update(t),f.final()]);return Buffer.concat([s,n,l])}let u=_crypto2.default.createCipheriv(o,c,n,{authTagLength:h});a.isCCM(o)&&u.setAAD(p,{plaintextLength:t.length}),(a.isGCM(o)||a.isOCB(o))&&u.setAAD(p);let m=Buffer.concat([u.update(t),u.final()]),d=u.getAuthTag();return Buffer.concat([s,n,p,d,m])}static decrypt(r,e,i={}){let t=_coercion.coerceToUint8Array.call(void 0, r),s=_coercion.coerceToUint8Array.call(void 0, e),c=a.resolveOptions(i),{algorithm:n}=c,p=t.subarray(0,c.salt);t=t.subarray(c.salt);let o=t.subarray(0,c.iv);t=t.subarray(c.iv);let h=_crypto2.default.scryptSync(s,p,c.length);if(a.isCBC(n)){let A=_crypto2.default.createDecipheriv(c.algorithm,h,o);return Buffer.concat([A.update(t),A.final()])}let u=c.aadLength,m=c.authTag,d=t.subarray(0,u);t=t.subarray(u);let f=t.subarray(0,m);t=t.subarray(m);let l=_crypto2.default.createDecipheriv(n,h,o,{authTagLength:m});return a.isCCM(n)&&l.setAAD(d,{plaintextLength:t.length}),(a.isGCM(n)||a.isOCB(n))&&l.setAAD(d),l.setAuthTag(f),Buffer.concat([l.update(t),l.final()])}static streamEncrypt(r,e){e.algorithm||(e.algorithm=a.DEFAULT_ALGORITHM.stream);let{Key:i,IV:t,options:{input:s,output:c,algorithm:n,salt:p,iv:o,authTag:h}}=a.newKeyIV(r,e),u=a.encrypt(Buffer.concat([i,t]),r,{algorithm:n,salt:p,iv:o,authTag:h}),m=_crypto2.default.createCipheriv(n,i,t);return{cipher:m,encrypt:()=>a.stream({cipher:m,input:s,output:c,encryptedKey:u})}}static streamDecrypt(r,e){e.algorithm||(e.algorithm=a.DEFAULT_ALGORITHM.stream);let{input:i,output:t,algorithm:s,salt:c,iv:n,authTag:p,length:o}=a.resolveOptions(e),h=o+n+c+n+p;return a.ExtractKeyIVFromStream(i,h).then(([u,m])=>{if(m.errored)throw m.errored;let d=a.decrypt(u,r,{algorithm:s,salt:c,iv:n,authTag:p}),f=d.subarray(0,o),l=d.subarray(o),A=_crypto2.default.createDecipheriv(s,f,l);return{decipher:A,decrypt:()=>a.decipherStream({decipher:A,input:m,output:t})}})}static hybridEncrypt(r,e,i){let t=_crypto2.default.randomBytes(32),s=_crypto2.default.publicEncrypt({key:e,padding:_crypto2.default.constants.RSA_PKCS1_OAEP_PADDING,oaepHash:"sha256"},t);return Buffer.concat([_conversion.writeUint16BE.call(void 0, s.length),s,a.encrypt(r,t,i)])}static hybridDecrypt(r,e,i){let t=Buffer.from(_coercion.coerceToUint8Array.call(void 0, r)),s=_conversion.readUint16BE.call(void 0, t.subarray(0,2)),c=t.subarray(2,2+s),n=t.subarray(2+s),p=typeof e=="object"&&"key"in e?e.key:e,o=typeof e=="object"&&"passphrase"in e?e.passphrase:void 0,h=_crypto2.default.privateDecrypt({key:p,passphrase:o,padding:_crypto2.default.constants.RSA_PKCS1_OAEP_PADDING,oaepHash:"sha256"},c);return a.decrypt(n,h,i)}static hybridStreamEncrypt(r,e,i){i.algorithm||(i.algorithm=a.DEFAULT_ALGORITHM.stream);let{Key:t,IV:s,options:{algorithm:c,input:n,output:p}}=a.newKeyIV(r,i),o=_crypto2.default.publicEncrypt(e,Buffer.concat([t,s])),h=_crypto2.default.createCipheriv(c,t,s);return{cipher:h,encrypt:()=>a.stream({cipher:h,input:n,output:p,encryptedKey:o})}}static hybridStreamDecrypt(r,e){e.algorithm||(e.algorithm=a.DEFAULT_ALGORITHM.stream);let{input:i,output:t,algorithm:s,length:c,rsaKeyLength:n}=a.resolveOptions(e);return a.ExtractKeyIVFromStream(i,n).then(([p,o])=>{let h=_crypto2.default.privateDecrypt(r,p),u=h.subarray(0,c),m=h.subarray(c),d=_crypto2.default.createDecipheriv(s,u,m);return{decipher:d,decrypt:()=>a.decipherStream({decipher:d,input:o,output:t})}})}static stream(r){let{cipher:e,encryptedKey:i,input:t,output:s}=r;return new Promise((c,n)=>{e.on("error",n),t.on("error",n),s.on("error",n),s.on("finish",c),s.write(i),t.pipe(e).pipe(s)})}static decipherStream(r){let{decipher:e,input:i,output:t}=r;return new Promise((s,c)=>{e.on("error",c),i.on("error",c),t.on("error",c),t.on("finish",s),i.pipe(e).pipe(t)})}static ExtractKeyIVFromStream(r,e){return new Promise((i,t)=>{let s=Buffer.alloc(0),c=0,n=!1,p=new (0, _stream.Transform)({async transform(o,h,u){if(s.length<e&&(s=Buffer.concat([s,o])),c+=o.length,s.length>e){let m=Math.max(0,s.length-e);s=s.subarray(0,e);let d=o.subarray(m*-1);return n||(i([s,this]),n=!0),this.push(d,h),u()}return c>e&&(n||(i([s,this]),n=!0),this.push(o,h)),u()},final(o){return s.length<e?o(new Error("The extracted KeyIV length is less than the expected length.")):o()}});p.on("error",t),r.on("error",o=>{p.destroy(o),t(o)}),r.pipe(p)})}static newKeyIV(r,e={}){let i=_coercion.coerceToUint8Array.call(void 0, r),t=a.resolveOptions(e),s=_crypto2.default.randomBytes(t.salt),c=_crypto2.default.scryptSync(i,s,t.length),n=_crypto2.default.randomBytes(t.iv),p=t.aad||_crypto2.default.scryptSync(c,s,t.aadLength);return{options:t,Key:c,IV:n,AAD:p,salt:s}}static resolveOptions(r={}){let e={...r};e.aad&&(e.aad=_coercion.coerceToUint8Array.call(void 0, e.aad)),e.salt||(e.salt=a.SALT_LENGTH.default),e.authTag||(e.authTag=a.AUTH_TAG_LENGTH.default),e.aadLength=_optionalChain([e, 'access', _ => _.aad, 'optionalAccess', _2 => _2.length])||e.aadLength||a.AAD_LENGTH.default,e.salt=Math.min(Math.max(e.salt,a.SALT_LENGTH.min),a.SALT_LENGTH.max),e.authTag=Math.min(Math.max(e.authTag,a.AUTH_TAG_LENGTH.min),a.AUTH_TAG_LENGTH.max),e.aad||(e.aadLength=Math.min(Math.max(e.aadLength,a.AAD_LENGTH.min),a.AAD_LENGTH.max));let{keyLength:i,algorithm:t}=a.getKeyLength(r.algorithm);return e.algorithm=t,e.length=i,e.iv=a.getIVLength(t,r),e}static getIVLength(r=a.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||a.IV_LENGTH.default,a.IV_LENGTH.min),a.IV_LENGTH.max)}}static getKeyLength(r=a.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"}};a.SALT_LENGTH={min:16,max:64,default:32},a.IV_LENGTH={min:8,max:32,default:16},a.AUTH_TAG_LENGTH={min:4,max:16,default:16},a.AAD_LENGTH={min:16,max:128,default:32},a.DEFAULT_ALGORITHM={buffer:"aes-256-gcm",stream:"aes-256-cbc"},a.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 g=a;exports.Cipher = g;
package/dist/index.mjs CHANGED
@@ -1 +1 @@
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};
1
+ import y from"crypto";import{Transform as C}from"stream";import{writeUint16BE as b,readUint16BE as L}from"@alessiofrittoli/crypto-buffer/conversion";import{coerceToUint8Array as T}from"@alessiofrittoli/crypto-buffer/coercion";var a=class a{static encrypt(r,e,i={}){let t=T(r),{salt:s,Key:c,IV:n,AAD:p,options:{algorithm:o,authTag:h}}=a.newKeyIV(e,i);if(a.isCBC(o)){let f=y.createCipheriv(o,c,n),l=Buffer.concat([f.update(t),f.final()]);return Buffer.concat([s,n,l])}let u=y.createCipheriv(o,c,n,{authTagLength:h});a.isCCM(o)&&u.setAAD(p,{plaintextLength:t.length}),(a.isGCM(o)||a.isOCB(o))&&u.setAAD(p);let m=Buffer.concat([u.update(t),u.final()]),d=u.getAuthTag();return Buffer.concat([s,n,p,d,m])}static decrypt(r,e,i={}){let t=T(r),s=T(e),c=a.resolveOptions(i),{algorithm:n}=c,p=t.subarray(0,c.salt);t=t.subarray(c.salt);let o=t.subarray(0,c.iv);t=t.subarray(c.iv);let h=y.scryptSync(s,p,c.length);if(a.isCBC(n)){let A=y.createDecipheriv(c.algorithm,h,o);return Buffer.concat([A.update(t),A.final()])}let u=c.aadLength,m=c.authTag,d=t.subarray(0,u);t=t.subarray(u);let f=t.subarray(0,m);t=t.subarray(m);let l=y.createDecipheriv(n,h,o,{authTagLength:m});return a.isCCM(n)&&l.setAAD(d,{plaintextLength:t.length}),(a.isGCM(n)||a.isOCB(n))&&l.setAAD(d),l.setAuthTag(f),Buffer.concat([l.update(t),l.final()])}static streamEncrypt(r,e){e.algorithm||(e.algorithm=a.DEFAULT_ALGORITHM.stream);let{Key:i,IV:t,options:{input:s,output:c,algorithm:n,salt:p,iv:o,authTag:h}}=a.newKeyIV(r,e),u=a.encrypt(Buffer.concat([i,t]),r,{algorithm:n,salt:p,iv:o,authTag:h}),m=y.createCipheriv(n,i,t);return{cipher:m,encrypt:()=>a.stream({cipher:m,input:s,output:c,encryptedKey:u})}}static streamDecrypt(r,e){e.algorithm||(e.algorithm=a.DEFAULT_ALGORITHM.stream);let{input:i,output:t,algorithm:s,salt:c,iv:n,authTag:p,length:o}=a.resolveOptions(e),h=o+n+c+n+p;return a.ExtractKeyIVFromStream(i,h).then(([u,m])=>{if(m.errored)throw m.errored;let d=a.decrypt(u,r,{algorithm:s,salt:c,iv:n,authTag:p}),f=d.subarray(0,o),l=d.subarray(o),A=y.createDecipheriv(s,f,l);return{decipher:A,decrypt:()=>a.decipherStream({decipher:A,input:m,output:t})}})}static hybridEncrypt(r,e,i){let t=y.randomBytes(32),s=y.publicEncrypt({key:e,padding:y.constants.RSA_PKCS1_OAEP_PADDING,oaepHash:"sha256"},t);return Buffer.concat([b(s.length),s,a.encrypt(r,t,i)])}static hybridDecrypt(r,e,i){let t=Buffer.from(T(r)),s=L(t.subarray(0,2)),c=t.subarray(2,2+s),n=t.subarray(2+s),p=typeof e=="object"&&"key"in e?e.key:e,o=typeof e=="object"&&"passphrase"in e?e.passphrase:void 0,h=y.privateDecrypt({key:p,passphrase:o,padding:y.constants.RSA_PKCS1_OAEP_PADDING,oaepHash:"sha256"},c);return a.decrypt(n,h,i)}static hybridStreamEncrypt(r,e,i){i.algorithm||(i.algorithm=a.DEFAULT_ALGORITHM.stream);let{Key:t,IV:s,options:{algorithm:c,input:n,output:p}}=a.newKeyIV(r,i),o=y.publicEncrypt(e,Buffer.concat([t,s])),h=y.createCipheriv(c,t,s);return{cipher:h,encrypt:()=>a.stream({cipher:h,input:n,output:p,encryptedKey:o})}}static hybridStreamDecrypt(r,e){e.algorithm||(e.algorithm=a.DEFAULT_ALGORITHM.stream);let{input:i,output:t,algorithm:s,length:c,rsaKeyLength:n}=a.resolveOptions(e);return a.ExtractKeyIVFromStream(i,n).then(([p,o])=>{let h=y.privateDecrypt(r,p),u=h.subarray(0,c),m=h.subarray(c),d=y.createDecipheriv(s,u,m);return{decipher:d,decrypt:()=>a.decipherStream({decipher:d,input:o,output:t})}})}static stream(r){let{cipher:e,encryptedKey:i,input:t,output:s}=r;return new Promise((c,n)=>{e.on("error",n),t.on("error",n),s.on("error",n),s.on("finish",c),s.write(i),t.pipe(e).pipe(s)})}static decipherStream(r){let{decipher:e,input:i,output:t}=r;return new Promise((s,c)=>{e.on("error",c),i.on("error",c),t.on("error",c),t.on("finish",s),i.pipe(e).pipe(t)})}static ExtractKeyIVFromStream(r,e){return new Promise((i,t)=>{let s=Buffer.alloc(0),c=0,n=!1,p=new C({async transform(o,h,u){if(s.length<e&&(s=Buffer.concat([s,o])),c+=o.length,s.length>e){let m=Math.max(0,s.length-e);s=s.subarray(0,e);let d=o.subarray(m*-1);return n||(i([s,this]),n=!0),this.push(d,h),u()}return c>e&&(n||(i([s,this]),n=!0),this.push(o,h)),u()},final(o){return s.length<e?o(new Error("The extracted KeyIV length is less than the expected length.")):o()}});p.on("error",t),r.on("error",o=>{p.destroy(o),t(o)}),r.pipe(p)})}static newKeyIV(r,e={}){let i=T(r),t=a.resolveOptions(e),s=y.randomBytes(t.salt),c=y.scryptSync(i,s,t.length),n=y.randomBytes(t.iv),p=t.aad||y.scryptSync(c,s,t.aadLength);return{options:t,Key:c,IV:n,AAD:p,salt:s}}static resolveOptions(r={}){let e={...r};e.aad&&(e.aad=T(e.aad)),e.salt||(e.salt=a.SALT_LENGTH.default),e.authTag||(e.authTag=a.AUTH_TAG_LENGTH.default),e.aadLength=e.aad?.length||e.aadLength||a.AAD_LENGTH.default,e.salt=Math.min(Math.max(e.salt,a.SALT_LENGTH.min),a.SALT_LENGTH.max),e.authTag=Math.min(Math.max(e.authTag,a.AUTH_TAG_LENGTH.min),a.AUTH_TAG_LENGTH.max),e.aad||(e.aadLength=Math.min(Math.max(e.aadLength,a.AAD_LENGTH.min),a.AAD_LENGTH.max));let{keyLength:i,algorithm:t}=a.getKeyLength(r.algorithm);return e.algorithm=t,e.length=i,e.iv=a.getIVLength(t,r),e}static getIVLength(r=a.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||a.IV_LENGTH.default,a.IV_LENGTH.min),a.IV_LENGTH.max)}}static getKeyLength(r=a.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"}};a.SALT_LENGTH={min:16,max:64,default:32},a.IV_LENGTH={min:8,max:32,default:16},a.AUTH_TAG_LENGTH={min:4,max:16,default:16},a.AAD_LENGTH={min:16,max:128,default:32},a.DEFAULT_ALGORITHM={buffer:"aes-256-gcm",stream:"aes-256-cbc"},a.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 g=a;export{g as Cipher};
package/dist/types.d.mts CHANGED
@@ -55,8 +55,8 @@ declare namespace Cph {
55
55
  * Returnign object from `Cipher.encryptStream()` method.
56
56
  */
57
57
  interface EncryptReturnType {
58
- /** The `crypto.Cipher` instance. */
59
- cipher: crypto.Cipher;
58
+ /** The `crypto.Cipheriv` instance. */
59
+ cipher: crypto.Cipheriv;
60
60
  /** The actual `encrypt` callback that must be called and awaited in order to start the encryption process. */
61
61
  encrypt: () => Promise<void>;
62
62
  }
@@ -74,8 +74,8 @@ declare namespace Cph {
74
74
  */
75
75
  type DecryptResolvedOptions = (Cph.ResolvedOptions & Required<Cph.Stream.Symmetric.DecryptOptions>);
76
76
  interface DecryptReturnType {
77
- /** The `crypto.Decipher` instance. */
78
- decipher: crypto.Decipher;
77
+ /** The `crypto.Decipheriv` instance. */
78
+ decipher: crypto.Decipheriv;
79
79
  /** The actual `decrypt` callback that must be called and awaited in order to start the decryption process. */
80
80
  decrypt: () => Promise<void>;
81
81
  }
package/dist/types.d.ts CHANGED
@@ -55,8 +55,8 @@ declare namespace Cph {
55
55
  * Returnign object from `Cipher.encryptStream()` method.
56
56
  */
57
57
  interface EncryptReturnType {
58
- /** The `crypto.Cipher` instance. */
59
- cipher: crypto.Cipher;
58
+ /** The `crypto.Cipheriv` instance. */
59
+ cipher: crypto.Cipheriv;
60
60
  /** The actual `encrypt` callback that must be called and awaited in order to start the encryption process. */
61
61
  encrypt: () => Promise<void>;
62
62
  }
@@ -74,8 +74,8 @@ declare namespace Cph {
74
74
  */
75
75
  type DecryptResolvedOptions = (Cph.ResolvedOptions & Required<Cph.Stream.Symmetric.DecryptOptions>);
76
76
  interface DecryptReturnType {
77
- /** The `crypto.Decipher` instance. */
78
- decipher: crypto.Decipher;
77
+ /** The `crypto.Decipheriv` instance. */
78
+ decipher: crypto.Decipheriv;
79
79
  /** The actual `decrypt` callback that must be called and awaited in order to start the decryption process. */
80
80
  decrypt: () => Promise<void>;
81
81
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alessiofrittoli/crypto-cipher",
3
- "version": "2.2.0",
3
+ "version": "3.0.0-alpha.1",
4
4
  "description": "Node.js Cipher cryptograph utility library",
5
5
  "author": {
6
6
  "name": "Alessio Frittoli",
@@ -40,14 +40,22 @@
40
40
  ],
41
41
  "exports": {
42
42
  ".": {
43
- "types": "./dist/index.d.ts",
44
- "import": "./dist/index.mjs",
45
- "require": "./dist/index.js"
43
+ "import": {
44
+ "types": "./dist/index.d.mts",
45
+ "default": "./dist/index.mjs"
46
+ },
47
+ "require": {
48
+ "types": "./dist/index.d.ts",
49
+ "default": "./dist/index.js"
50
+ }
46
51
  },
47
52
  "./types": {
48
- "types": "./dist/types.d.ts",
49
- "import": "./dist/types.mjs",
50
- "require": "./dist/types.js"
53
+ "import": {
54
+ "types": "./dist/types.d.mts"
55
+ },
56
+ "require": {
57
+ "types": "./dist/types.d.ts"
58
+ }
51
59
  }
52
60
  },
53
61
  "sideEffects": false,
@@ -55,11 +63,11 @@
55
63
  "//1a": "*********************************************************************",
56
64
  "//1b": "******************** DEV - BUILD - LINT - RELEASE *******************",
57
65
  "//1c": "*********************************************************************",
58
- "dev": "NODE_ENV=development tsup --watch",
59
- "build:prod": "NODE_ENV=production tsup",
66
+ "dev": "cross-env NODE_ENV=development tsup --watch",
67
+ "build:prod": "cross-env NODE_ENV=production tsup",
60
68
  "build": "pnpm lint && pnpm test:ci && pnpm build:prod",
61
69
  "lint": "eslint",
62
- "release": "node scripts/publish.js --verbose --npm",
70
+ "release": "node scripts/release.js --verbose --npm",
63
71
  "//2a": "*********************************************************************",
64
72
  "//2b": "***************************** UNIT TESTS ****************************",
65
73
  "//2c": "*********************************************************************",
@@ -84,29 +92,25 @@
84
92
  "test:misc": "pnpm test:watch misc.test.ts"
85
93
  },
86
94
  "devDependencies": {
87
- "@alessiofrittoli/node-scripts": "^2.5.0",
88
- "@eslint/js": "^9.26.0",
89
- "@jest/globals": "^29.7.0",
90
- "@types/jest": "^29.5.14",
91
- "@types/node": "^22.15.3",
92
- "concurrently": "^9.1.2",
93
- "dotenv": "^16.5.0",
94
- "eslint": "^9.26.0",
95
- "globals": "^16.0.0",
95
+ "@alessiofrittoli/node-scripts": "^3.3.0",
96
+ "@eslint/js": "^9.39.2",
97
+ "@jest/globals": "^30.2.0",
98
+ "@types/jest": "^30.0.0",
99
+ "@types/node": "^25.0.2",
100
+ "concurrently": "^9.2.1",
101
+ "cross-env": "^10.1.0",
102
+ "dotenv": "^17.2.3",
103
+ "eslint": "^9.39.2",
104
+ "globals": "^16.5.0",
96
105
  "http-server": "^14.1.1",
97
- "jest": "^29.7.0",
98
- "ts-jest": "^29.3.2",
106
+ "jest": "^30.2.0",
107
+ "ts-jest": "^29.4.6",
99
108
  "ts-node": "^10.9.2",
100
- "tsup": "^8.4.0",
101
- "typescript": "^5.8.3",
102
- "typescript-eslint": "^8.31.1"
109
+ "tsup": "^8.5.1",
110
+ "typescript": "^5.9.3",
111
+ "typescript-eslint": "^8.49.0"
103
112
  },
104
113
  "dependencies": {
105
- "@alessiofrittoli/crypto-buffer": "^3.6.0"
106
- },
107
- "pnpm": {
108
- "onlyBuiltDependencies": [
109
- "esbuild"
110
- ]
114
+ "@alessiofrittoli/crypto-buffer": "^3.9.0"
111
115
  }
112
116
  }