@alviere/core 0.18.0 → 0.18.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.mjs CHANGED
@@ -149,10 +149,10 @@ class Cryptor {
149
149
  *
150
150
  */
151
151
  constructor(logger) {
152
+ this.nonce = new Uint8Array(12);
152
153
  this.logger = logger;
153
154
  this.rsa_key = getRSA_PUB_KEY();
154
155
  this.key = this.generate_key();
155
- this.nonce = this.generate_nonce();
156
156
  this.logger?.debug("🔐 Cryptor initialized");
157
157
  }
158
158
  /**
@@ -161,27 +161,21 @@ class Cryptor {
161
161
  * @returns
162
162
  */
163
163
  async encrypt(data) {
164
+ this.nonce = this.generate_nonce();
164
165
  const encoded = new TextEncoder().encode(JSON.stringify(data));
165
166
  const alg = { name: "AES-GCM", iv: this.nonce };
166
167
  const crypto_key = await this.import_key();
167
168
  const cipher = await crypto.subtle.encrypt(alg, crypto_key, encoded);
168
- const cipherStr = this.bufferToStr(cipher);
169
- const cipherB64 = btoa(cipherStr);
170
- const keyStr = this.bufferToStr(this.key);
171
- const keyB64 = btoa(keyStr);
172
- const nonceStr = this.bufferToStr(this.nonce);
173
- const nonceB64 = btoa(nonceStr);
169
+ const cipherB64 = btoa(this.bufferToStr(new Uint8Array(cipher)));
170
+ const keyB64 = btoa(this.bufferToStr(this.key));
171
+ const nonceB64 = btoa(this.bufferToStr(this.nonce));
174
172
  this.logger?.debug(`🔐 RSA_KEY: ${this.rsa_key.substring(0, 50)}...`);
175
173
  const publicKey = pki.publicKeyFromPem(this.rsa_key);
176
- const keyNonceCipher = publicKey.encrypt(keyB64 + "::" + nonceB64);
177
- const keyNonceB64 = btoa(keyNonceCipher);
174
+ const keyNonceB64 = btoa(publicKey.encrypt(keyB64 + "::" + nonceB64));
178
175
  const encrypted_request = {
179
176
  p: cipherB64,
180
- // base64(AES-GCM(<sdk_envelope>))
181
177
  k: keyNonceB64,
182
- // base64(RSA(base64(SDK_KEY)::base64(<sdk_nonce>)))
183
178
  i: getCERTIFICATE_ID()
184
- // certificate id - configurable via environment variable
185
179
  };
186
180
  this.logger?.debug(
187
181
  `🔐 ENCRYPTED_REQUEST: p=${encrypted_request.p.substring(0, 20)}..., i=${encrypted_request.i}`
@@ -227,25 +221,12 @@ class Cryptor {
227
221
  "decrypt"
228
222
  ]);
229
223
  }
230
- /**
231
- *
232
- * @param data
233
- * @returns
234
- */
235
- async rsa_encrypt(data) {
236
- this.logger?.debug(`🔐 RSA_ENCRYPT: Data to encrypt: ${data.substring(0, 50)}...`);
237
- this.logger?.debug(`🔐 RSA_KEY: ${this.rsa_key.substring(0, 50)}...`);
238
- const publicKey = pki.publicKeyFromPem(this.rsa_key);
239
- return btoa(publicKey.encrypt(data));
240
- }
241
224
  /**
242
225
  *
243
226
  * @param buf
244
227
  */
245
228
  bufferToStr(buf) {
246
- const cb = Array.from(new Uint8Array(buf));
247
- const cs = cb.map((byte) => String.fromCharCode(byte)).join("");
248
- return cs;
229
+ return Array.from(buf).map((byte) => String.fromCharCode(byte)).join("");
249
230
  }
250
231
  }
251
232
  class Validator {