@groundbrick/crypto-service 1.0.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.
@@ -0,0 +1,24 @@
1
+ /**
2
+ * AES-256-GCM encryption for sensitive data at rest (e.g. per-petshop provider
3
+ * credentials). The encryption key is derived with scrypt so any-length env var
4
+ * yields a valid 32-byte key.
5
+ */
6
+ export declare class CryptoService {
7
+ private readonly key;
8
+ constructor(encryptionKey?: string);
9
+ encrypt(plaintext: string): string;
10
+ decrypt(ciphertext: string): string;
11
+ }
12
+ /**
13
+ * HMAC-SHA256 em hex, para assinar pedidos e webhooks. Não usa a ENCRYPTION_KEY:
14
+ * o segredo é o de quem assina (ex.: o segredo do webhook de um cliente).
15
+ */
16
+ export declare function hmacSha256Hex(secret: string, data: string): string;
17
+ /**
18
+ * Confere uma assinatura HMAC-SHA256 em hex sem denunciar, pelo tempo que leva,
19
+ * quantos caracteres batem certo.
20
+ */
21
+ export declare function verifyHmacSha256Hex(secret: string, data: string, signatureHex: string): boolean;
22
+ /** Identificador aleatório (UUID v4), para chaves de idempotência e afins. */
23
+ export declare function randomUuid(): string;
24
+ //# sourceMappingURL=CryptoService.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"CryptoService.d.ts","sourceRoot":"","sources":["../src/CryptoService.ts"],"names":[],"mappings":"AAMA;;;;GAIG;AACH,qBAAa,aAAa;IACtB,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAS;gBAEjB,aAAa,CAAC,EAAE,MAAM;IAOlC,OAAO,CAAC,SAAS,EAAE,MAAM,GAAG,MAAM;IASlC,OAAO,CAAC,UAAU,EAAE,MAAM,GAAG,MAAM;CAOtC;AAED;;;GAGG;AACH,wBAAgB,aAAa,CAAC,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM,CAElE;AAED;;;GAGG;AACH,wBAAgB,mBAAmB,CAAC,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM,GAAG,OAAO,CAK/F;AAED,8EAA8E;AAC9E,wBAAgB,UAAU,IAAI,MAAM,CAEnC"}
@@ -0,0 +1,58 @@
1
+ import { createCipheriv, createDecipheriv, createHmac, randomBytes, randomUUID, scryptSync, timingSafeEqual } from 'crypto';
2
+ const ALGORITHM = 'aes-256-gcm';
3
+ const IV_LENGTH = 12; // GCM standard
4
+ const KEY_LENGTH = 32;
5
+ /**
6
+ * AES-256-GCM encryption for sensitive data at rest (e.g. per-petshop provider
7
+ * credentials). The encryption key is derived with scrypt so any-length env var
8
+ * yields a valid 32-byte key.
9
+ */
10
+ export class CryptoService {
11
+ key;
12
+ constructor(encryptionKey) {
13
+ const raw = encryptionKey || process.env.ENCRYPTION_KEY;
14
+ if (!raw)
15
+ throw new Error('ENCRYPTION_KEY não definida');
16
+ // Deriva 32 bytes independentemente do tamanho da env var
17
+ this.key = scryptSync(raw, 'agendapet-salt', KEY_LENGTH);
18
+ }
19
+ encrypt(plaintext) {
20
+ const iv = randomBytes(IV_LENGTH);
21
+ const cipher = createCipheriv(ALGORITHM, this.key, iv);
22
+ const encrypted = Buffer.concat([cipher.update(plaintext, 'utf8'), cipher.final()]);
23
+ const tag = cipher.getAuthTag();
24
+ // Formato: iv:tag:dados — tudo em hex
25
+ return `${iv.toString('hex')}:${tag.toString('hex')}:${encrypted.toString('hex')}`;
26
+ }
27
+ decrypt(ciphertext) {
28
+ const [ivHex, tagHex, dataHex] = ciphertext.split(':');
29
+ if (!ivHex || !tagHex || !dataHex)
30
+ throw new Error('Formato de ciphertext inválido');
31
+ const decipher = createDecipheriv(ALGORITHM, this.key, Buffer.from(ivHex, 'hex'));
32
+ decipher.setAuthTag(Buffer.from(tagHex, 'hex'));
33
+ return decipher.update(Buffer.from(dataHex, 'hex')).toString('utf8') + decipher.final('utf8');
34
+ }
35
+ }
36
+ /**
37
+ * HMAC-SHA256 em hex, para assinar pedidos e webhooks. Não usa a ENCRYPTION_KEY:
38
+ * o segredo é o de quem assina (ex.: o segredo do webhook de um cliente).
39
+ */
40
+ export function hmacSha256Hex(secret, data) {
41
+ return createHmac('sha256', secret).update(data).digest('hex');
42
+ }
43
+ /**
44
+ * Confere uma assinatura HMAC-SHA256 em hex sem denunciar, pelo tempo que leva,
45
+ * quantos caracteres batem certo.
46
+ */
47
+ export function verifyHmacSha256Hex(secret, data, signatureHex) {
48
+ if (!/^[a-f0-9]+$/i.test(signatureHex))
49
+ return false;
50
+ const expected = Buffer.from(hmacSha256Hex(secret, data), 'hex');
51
+ const received = Buffer.from(signatureHex, 'hex');
52
+ return received.length === expected.length && timingSafeEqual(received, expected);
53
+ }
54
+ /** Identificador aleatório (UUID v4), para chaves de idempotência e afins. */
55
+ export function randomUuid() {
56
+ return randomUUID();
57
+ }
58
+ //# sourceMappingURL=CryptoService.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"CryptoService.js","sourceRoot":"","sources":["../src/CryptoService.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,gBAAgB,EAAE,UAAU,EAAE,WAAW,EAAE,UAAU,EAAE,UAAU,EAAE,eAAe,EAAE,MAAM,QAAQ,CAAC;AAE5H,MAAM,SAAS,GAAG,aAAa,CAAC;AAChC,MAAM,SAAS,GAAG,EAAE,CAAC,CAAG,eAAe;AACvC,MAAM,UAAU,GAAG,EAAE,CAAC;AAEtB;;;;GAIG;AACH,MAAM,OAAO,aAAa;IACL,GAAG,CAAS;IAE7B,YAAY,aAAsB;QAC9B,MAAM,GAAG,GAAG,aAAa,IAAI,OAAO,CAAC,GAAG,CAAC,cAAc,CAAC;QACxD,IAAI,CAAC,GAAG;YAAE,MAAM,IAAI,KAAK,CAAC,6BAA6B,CAAC,CAAC;QACzD,0DAA0D;QAC1D,IAAI,CAAC,GAAG,GAAG,UAAU,CAAC,GAAG,EAAE,gBAAgB,EAAE,UAAU,CAAC,CAAC;IAC7D,CAAC;IAED,OAAO,CAAC,SAAiB;QACrB,MA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AY;IACtD,OAAO,UAAU,CAAC,QAAQ,EAAE,MAAM,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;AACnE,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,mBAAmB,CAAC,MAAc,EAAE,IAAY,EAAE,YAAoB;IAClF,IAAI,CAAC,cAAc,CAAC,IAAI,CAAC,YAAY,CAAC;QAAE,OAAO,KAAK,CAAC;IACrD,MAAM,QAAQ,GAAG,MAAM,CAAC,IAAI,CAAC,aAAa,CAAC,MAAM,EAAE,IAAI,CAAC,EAAE,KAAK,CAAC,CAAC;IACjE,MAAM,QAAQ,GAAG,MAAM,CAAC,IAAI,CAAC,YAAY,EAAE,KAAK,CAAC,CAAC;IAClD,OAAO,QAAQ,CAAC,MAAM,KAAK,QAAQ,CAAC,MAAM,IAAI,eAAe,CAAC,QAAQ,EAAE,QAAQ,CAAC,CAAC;AACtF,CAAC;AAED,8EAA8E;AAC9E,MAAM,UAAU,UAAU;IACtB,OAAO,UAAU,EAAE,CAAC;AACxB,CAAC"}
@@ -0,0 +1,2 @@
1
+ export { CryptoService, hmacSha256Hex, verifyHmacSha256Hex, randomUuid } from './CryptoService.js';
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,aAAa,EAAE,mBAAmB,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,2 @@
1
+ export { CryptoService, hmacSha256Hex, verifyHmacSha256Hex, randomUuid } from './CryptoService.js';
2
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,aAAa,EAAE,mBAAmB,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAC"}
package/package.json ADDED
@@ -0,0 +1,35 @@
1
+ {
2
+ "name": "@groundbrick/crypto-service",
3
+ "version": "1.0.0",
4
+ "description": "Encriptação AES-256-GCM para dados sensíveis em repouso",
5
+ "type": "module",
6
+ "main": "dist/index.js",
7
+ "types": "dist/index.d.ts",
8
+ "files": [
9
+ "dist/**/*"
10
+ ],
11
+ "exports": {
12
+ ".": {
13
+ "import": "./dist/index.js",
14
+ "require": "./dist/index.js",
15
+ "types": "./dist/index.d.ts"
16
+ }
17
+ },
18
+ "author": "GroundBrick",
19
+ "license": "MIT",
20
+ "publishConfig": {
21
+ "access": "public"
22
+ },
23
+ "devDependencies": {
24
+ "@types/node": "^20.19.9",
25
+ "typescript": "^5.3.3"
26
+ },
27
+ "engines": {
28
+ "node": ">=18.0.0"
29
+ },
30
+ "scripts": {
31
+ "build": "tsc",
32
+ "dev": "tsc --watch",
33
+ "clean": "rm -rf dist *.tsbuildinfo"
34
+ }
35
+ }