test-entity-library-asm 3.9.6 → 3.9.7

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.
@@ -4,48 +4,63 @@ exports.JsonEncryptionTransformer = void 0;
4
4
  const crypto = require("crypto");
5
5
  const ENCRYPTION_KEY = process.env.SECRET_ENCRYPTION_KEY || "clave-32-bytes-segura";
6
6
  const ALGORITHM = "aes-256-cbc";
7
- // Crear KeyObject con exactamente 32 bytes para AES-256
8
- const keyObject = crypto.createSecretKey(new Uint8Array(crypto.createHash("sha256").update(ENCRYPTION_KEY).digest()));
9
- function decryptValue(encrypted) {
10
- if (!encrypted.includes(":"))
11
- return encrypted;
12
- const [ivHex, content] = encrypted.split(":");
13
- const iv = Buffer.from(ivHex, "hex");
14
- // Pasar IV como Uint8Array para que TypeScript no dé error
15
- const decipher = crypto.createDecipheriv(ALGORITHM, keyObject, new Uint8Array(iv));
16
- let decrypted = decipher.update(content, "hex", "utf8");
17
- decrypted += decipher.final("utf8");
18
- return decrypted;
7
+ // KeyObject de 32 bytes (evita líos de tipos con CipherKey)
8
+ const KEY = crypto.createSecretKey(crypto.createHash("sha256").update(String(ENCRYPTION_KEY)).digest());
9
+ function looksLikeCBC(encoded) {
10
+ // formato: iv:cipherHex
11
+ return /^[0-9a-fA-F]+:[0-9a-fA-F]+$/.test(encoded);
12
+ }
13
+ function decryptValue(value) {
14
+ if (typeof value !== "string" || !looksLikeCBC(value))
15
+ return value;
16
+ var parts = value.split(":");
17
+ var ivHex = parts[0];
18
+ var contentHex = parts[1];
19
+ var iv = Buffer.from(ivHex, "hex");
20
+ var decipher = crypto.createDecipheriv(ALGORITHM, KEY, iv);
21
+ var dec = Buffer.concat([
22
+ decipher.update(Buffer.from(contentHex, "hex")),
23
+ decipher.final(),
24
+ ]);
25
+ return dec.toString("utf8");
19
26
  }
20
27
  function encryptValue(plain) {
21
- const iv = crypto.randomBytes(16);
22
- // Igual aquí: IV como Uint8Array
23
- const cipher = crypto.createCipheriv(ALGORITHM, keyObject, new Uint8Array(iv));
24
- let encrypted = cipher.update(plain, "utf8", "hex");
25
- encrypted += cipher.final("hex");
26
- return iv.toString("hex") + ":" + encrypted;
28
+ if (plain === null || plain === undefined)
29
+ return null;
30
+ var text = String(plain);
31
+ var iv = crypto.randomBytes(16);
32
+ var cipher = crypto.createCipheriv(ALGORITHM, KEY, iv);
33
+ var enc = Buffer.concat([cipher.update(text, "utf8"), cipher.final()]);
34
+ return iv.toString("hex") + ":" + enc.toString("hex");
27
35
  }
28
36
  exports.JsonEncryptionTransformer = {
29
37
  to(value) {
30
38
  if (typeof value !== "object" || value === null)
31
39
  return JSON.stringify({});
32
- const encryptedJson = {};
33
- for (const key in value) {
34
- if (key.startsWith("encrypted_")) {
35
- encryptedJson[key] = encryptValue(value[key]);
40
+ var out = {};
41
+ for (var k in value) {
42
+ var v = value[k];
43
+ // No usamos startsWith (no tipado en ES5)
44
+ if (k.length >= 10 && k.substr(0, 10) === "encrypted_") {
45
+ out[k] = encryptValue(v);
36
46
  }
37
47
  else {
38
- encryptedJson[key] = value[key];
48
+ out[k] = v;
39
49
  }
40
50
  }
41
- return JSON.stringify(encryptedJson);
51
+ return JSON.stringify(out);
42
52
  },
43
53
  from(dbValue) {
44
54
  try {
45
- const parsed = JSON.parse(dbValue);
46
- for (const key in parsed) {
47
- if (key.startsWith("encrypted_")) {
48
- parsed[key] = decryptValue(parsed[key]);
55
+ var parsed = JSON.parse(dbValue);
56
+ for (var k in parsed) {
57
+ if (k.length >= 10 && k.substr(0, 10) === "encrypted_") {
58
+ try {
59
+ parsed[k] = decryptValue(parsed[k]);
60
+ }
61
+ catch {
62
+ // si falla, deja el valor original
63
+ }
49
64
  }
50
65
  }
51
66
  return parsed;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "test-entity-library-asm",
3
- "version": "3.9.6",
3
+ "version": "3.9.7",
4
4
  "description": "Entidades de ejemplo para una base de datos",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -20,7 +20,7 @@
20
20
  },
21
21
  "devDependencies": {
22
22
  "@types/express": "^4.17.21",
23
- "@types/node": "^20.12.7",
23
+ "@types/node": "^20.19.10",
24
24
  "rimraf": "^6.0.1",
25
25
  "typescript": "^5.4.5"
26
26
  },
@@ -5,66 +5,69 @@ const ENCRYPTION_KEY =
5
5
  process.env.SECRET_ENCRYPTION_KEY || "clave-32-bytes-segura";
6
6
  const ALGORITHM = "aes-256-cbc";
7
7
 
8
- // Crear KeyObject con exactamente 32 bytes para AES-256
9
- const keyObject = crypto.createSecretKey(
10
- new Uint8Array(crypto.createHash("sha256").update(ENCRYPTION_KEY).digest())
8
+ // KeyObject de 32 bytes (evita líos de tipos con CipherKey)
9
+ const KEY = crypto.createSecretKey(
10
+ crypto.createHash("sha256").update(String(ENCRYPTION_KEY)).digest()
11
11
  );
12
12
 
13
- function decryptValue(encrypted: string): string {
14
- if (!encrypted.includes(":")) return encrypted;
13
+ function looksLikeCBC(encoded: string) {
14
+ // formato: iv:cipherHex
15
+ return /^[0-9a-fA-F]+:[0-9a-fA-F]+$/.test(encoded);
16
+ }
15
17
 
16
- const [ivHex, content] = encrypted.split(":");
17
- const iv = Buffer.from(ivHex, "hex");
18
+ function decryptValue(value: any): any {
19
+ if (typeof value !== "string" || !looksLikeCBC(value)) return value;
18
20
 
19
- // Pasar IV como Uint8Array para que TypeScript no dé error
20
- const decipher = crypto.createDecipheriv(
21
- ALGORITHM,
22
- keyObject,
23
- new Uint8Array(iv)
24
- );
25
- let decrypted = decipher.update(content, "hex", "utf8");
26
- decrypted += decipher.final("utf8");
27
- return decrypted;
28
- }
21
+ var parts = value.split(":");
22
+ var ivHex = parts[0];
23
+ var contentHex = parts[1];
29
24
 
30
- function encryptValue(plain: string): string {
31
- const iv = crypto.randomBytes(16);
25
+ var iv = Buffer.from(ivHex, "hex");
26
+ var decipher = crypto.createDecipheriv(ALGORITHM, KEY, iv);
27
+ var dec = Buffer.concat([
28
+ decipher.update(Buffer.from(contentHex, "hex")),
29
+ decipher.final(),
30
+ ]);
31
+ return dec.toString("utf8");
32
+ }
32
33
 
33
- // Igual aquí: IV como Uint8Array
34
- const cipher = crypto.createCipheriv(
35
- ALGORITHM,
36
- keyObject,
37
- new Uint8Array(iv)
38
- );
39
- let encrypted = cipher.update(plain, "utf8", "hex");
40
- encrypted += cipher.final("hex");
34
+ function encryptValue(plain: any): string | null {
35
+ if (plain === null || plain === undefined) return null;
36
+ var text = String(plain);
41
37
 
42
- return iv.toString("hex") + ":" + encrypted;
38
+ var iv = crypto.randomBytes(16);
39
+ var cipher = crypto.createCipheriv(ALGORITHM, KEY, iv);
40
+ var enc = Buffer.concat([cipher.update(text, "utf8"), cipher.final()]);
41
+ return iv.toString("hex") + ":" + enc.toString("hex");
43
42
  }
44
43
 
45
44
  export const JsonEncryptionTransformer: ValueTransformer = {
46
45
  to(value: any): string {
47
46
  if (typeof value !== "object" || value === null) return JSON.stringify({});
48
47
 
49
- const encryptedJson: Record<string, any> = {};
50
-
51
- for (const key in value) {
52
- if (key.startsWith("encrypted_")) {
53
- encryptedJson[key] = encryptValue(value[key]);
48
+ var out: { [k: string]: any } = {};
49
+ for (var k in value) {
50
+ var v = value[k];
51
+ // No usamos startsWith (no tipado en ES5)
52
+ if (k.length >= 10 && k.substr(0, 10) === "encrypted_") {
53
+ out[k] = encryptValue(v);
54
54
  } else {
55
- encryptedJson[key] = value[key];
55
+ out[k] = v;
56
56
  }
57
57
  }
58
-
59
- return JSON.stringify(encryptedJson);
58
+ return JSON.stringify(out);
60
59
  },
61
60
 
62
61
  from(dbValue: string): any {
63
62
  try {
64
- const parsed = JSON.parse(dbValue);
65
- for (const key in parsed) {
66
- if (key.startsWith("encrypted_")) {
67
- parsed[key] = decryptValue(parsed[key]);
63
+ var parsed = JSON.parse(dbValue);
64
+ for (var k in parsed) {
65
+ if (k.length >= 10 && k.substr(0, 10) === "encrypted_") {
66
+ try {
67
+ parsed[k] = decryptValue(parsed[k]);
68
+ } catch {
69
+ // si falla, deja el valor original
70
+ }
68
71
  }
69
72
  }
70
73
  return parsed;