@groundbrick/crypto-service 1.0.0 → 1.0.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/README.md +49 -0
- package/dist/CryptoService.d.ts +5 -7
- package/dist/CryptoService.d.ts.map +1 -1
- package/dist/CryptoService.js +6 -9
- package/dist/CryptoService.js.map +1 -1
- package/package.json +11 -3
package/README.md
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
# @groundbrick/crypto-service
|
|
2
|
+
|
|
3
|
+
Encriptação AES-256-GCM para dados sensíveis em repouso, assinaturas HMAC-SHA256 e UUIDs aleatórios. Usa o módulo `crypto` do Node.js.
|
|
4
|
+
|
|
5
|
+
## Instalação
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @groundbrick/crypto-service
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Uso
|
|
12
|
+
|
|
13
|
+
```typescript
|
|
14
|
+
import { CryptoService, hmacSha256Hex, verifyHmacSha256Hex, randomUuid } from '@groundbrick/crypto-service';
|
|
15
|
+
|
|
16
|
+
const crypto = new CryptoService(); // usa process.env.ENCRYPTION_KEY
|
|
17
|
+
const ciphertext = crypto.encrypt('valor secreto');
|
|
18
|
+
const plaintext = crypto.decrypt(ciphertext);
|
|
19
|
+
|
|
20
|
+
const signature = hmacSha256Hex('segredo', '{"id":1}');
|
|
21
|
+
verifyHmacSha256Hex('segredo', '{"id":1}', signature); // true
|
|
22
|
+
|
|
23
|
+
randomUuid(); // UUID v4
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## API
|
|
27
|
+
|
|
28
|
+
### `new CryptoService(encryptionKey?: string)`
|
|
29
|
+
|
|
30
|
+
A chave é o argumento ou, na sua ausência, a variável de ambiente `ENCRYPTION_KEY`. Pode ter qualquer tamanho: a chave de 32 bytes é derivada com scrypt e um salt fixo. Sem nenhuma das duas, lança `Error('ENCRYPTION_KEY não definida')`.
|
|
31
|
+
|
|
32
|
+
| Método | Descrição |
|
|
33
|
+
|---|---|
|
|
34
|
+
| `encrypt(plaintext: string): string` | Encripta em AES-256-GCM com um IV aleatório de 12 bytes. Devolve `iv:tag:dados`, em hex. |
|
|
35
|
+
| `decrypt(ciphertext: string): string` | Desencripta o formato devolvido por `encrypt`. |
|
|
36
|
+
|
|
37
|
+
`decrypt` lança `Error('Formato de ciphertext inválido')` quando falta ou está vazia alguma das três partes (o que inclui o resultado de `encrypt('')`), e o erro do Node.js quando a chave é outra ou os dados foram alterados.
|
|
38
|
+
|
|
39
|
+
### Funções
|
|
40
|
+
|
|
41
|
+
| Função | Descrição |
|
|
42
|
+
|---|---|
|
|
43
|
+
| `hmacSha256Hex(secret: string, data: string): string` | HMAC-SHA256 em hex. Não usa a `ENCRYPTION_KEY`. |
|
|
44
|
+
| `verifyHmacSha256Hex(secret: string, data: string, signatureHex: string): boolean` | Confere uma assinatura em hex, em tempo constante. Devolve `false` se não for hex ou tiver outro comprimento. |
|
|
45
|
+
| `randomUuid(): string` | UUID v4 aleatório. |
|
|
46
|
+
|
|
47
|
+
## Licença
|
|
48
|
+
|
|
49
|
+
MIT
|
package/dist/CryptoService.d.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* AES-256-GCM encryption for sensitive data at rest (e.g.
|
|
3
|
-
*
|
|
4
|
-
* yields a valid 32-byte key.
|
|
2
|
+
* AES-256-GCM encryption for sensitive data at rest (e.g. provider credentials).
|
|
3
|
+
* The 32-byte encryption key is derived with scrypt from a secret of any length.
|
|
5
4
|
*/
|
|
6
5
|
export declare class CryptoService {
|
|
7
6
|
private readonly key;
|
|
@@ -10,13 +9,12 @@ export declare class CryptoService {
|
|
|
10
9
|
decrypt(ciphertext: string): string;
|
|
11
10
|
}
|
|
12
11
|
/**
|
|
13
|
-
* HMAC-SHA256 em hex, para assinar pedidos e webhooks. Não usa a ENCRYPTION_KEY:
|
|
14
|
-
*
|
|
12
|
+
* HMAC-SHA256 em hex, para assinar pedidos e webhooks. Não usa a ENCRYPTION_KEY: o
|
|
13
|
+
* segredo é passado por quem chama.
|
|
15
14
|
*/
|
|
16
15
|
export declare function hmacSha256Hex(secret: string, data: string): string;
|
|
17
16
|
/**
|
|
18
|
-
* Confere uma assinatura HMAC-SHA256 em hex
|
|
19
|
-
* quantos caracteres batem certo.
|
|
17
|
+
* Confere uma assinatura HMAC-SHA256 em hex, em tempo constante.
|
|
20
18
|
*/
|
|
21
19
|
export declare function verifyHmacSha256Hex(secret: string, data: string, signatureHex: string): boolean;
|
|
22
20
|
/** Identificador aleatório (UUID v4), para chaves de idempotência e afins. */
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"CryptoService.d.ts","sourceRoot":"","sources":["../src/CryptoService.ts"],"names":[],"mappings":"AAMA
|
|
1
|
+
{"version":3,"file":"CryptoService.d.ts","sourceRoot":"","sources":["../src/CryptoService.ts"],"names":[],"mappings":"AAMA;;;GAGG;AACH,qBAAa,aAAa;IACtB,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAS;gBAEjB,aAAa,CAAC,EAAE,MAAM;IAMlC,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;;GAEG;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"}
|
package/dist/CryptoService.js
CHANGED
|
@@ -3,9 +3,8 @@ const ALGORITHM = 'aes-256-gcm';
|
|
|
3
3
|
const IV_LENGTH = 12; // GCM standard
|
|
4
4
|
const KEY_LENGTH = 32;
|
|
5
5
|
/**
|
|
6
|
-
* AES-256-GCM encryption for sensitive data at rest (e.g.
|
|
7
|
-
*
|
|
8
|
-
* yields a valid 32-byte key.
|
|
6
|
+
* AES-256-GCM encryption for sensitive data at rest (e.g. provider credentials).
|
|
7
|
+
* The 32-byte encryption key is derived with scrypt from a secret of any length.
|
|
9
8
|
*/
|
|
10
9
|
export class CryptoService {
|
|
11
10
|
key;
|
|
@@ -13,7 +12,6 @@ export class CryptoService {
|
|
|
13
12
|
const raw = encryptionKey || process.env.ENCRYPTION_KEY;
|
|
14
13
|
if (!raw)
|
|
15
14
|
throw new Error('ENCRYPTION_KEY não definida');
|
|
16
|
-
// Deriva 32 bytes independentemente do tamanho da env var
|
|
17
15
|
this.key = scryptSync(raw, 'agendapet-salt', KEY_LENGTH);
|
|
18
16
|
}
|
|
19
17
|
encrypt(plaintext) {
|
|
@@ -26,7 +24,7 @@ export class CryptoService {
|
|
|
26
24
|
}
|
|
27
25
|
decrypt(ciphertext) {
|
|
28
26
|
const [ivHex, tagHex, dataHex] = ciphertext.split(':');
|
|
29
|
-
if (!ivHex || !tagHex ||
|
|
27
|
+
if (!ivHex || !tagHex || dataHex === undefined)
|
|
30
28
|
throw new Error('Formato de ciphertext inválido');
|
|
31
29
|
const decipher = createDecipheriv(ALGORITHM, this.key, Buffer.from(ivHex, 'hex'));
|
|
32
30
|
decipher.setAuthTag(Buffer.from(tagHex, 'hex'));
|
|
@@ -34,15 +32,14 @@ export class CryptoService {
|
|
|
34
32
|
}
|
|
35
33
|
}
|
|
36
34
|
/**
|
|
37
|
-
* HMAC-SHA256 em hex, para assinar pedidos e webhooks. Não usa a ENCRYPTION_KEY:
|
|
38
|
-
*
|
|
35
|
+
* HMAC-SHA256 em hex, para assinar pedidos e webhooks. Não usa a ENCRYPTION_KEY: o
|
|
36
|
+
* segredo é passado por quem chama.
|
|
39
37
|
*/
|
|
40
38
|
export function hmacSha256Hex(secret, data) {
|
|
41
39
|
return createHmac('sha256', secret).update(data).digest('hex');
|
|
42
40
|
}
|
|
43
41
|
/**
|
|
44
|
-
* Confere uma assinatura HMAC-SHA256 em hex
|
|
45
|
-
* quantos caracteres batem certo.
|
|
42
|
+
* Confere uma assinatura HMAC-SHA256 em hex, em tempo constante.
|
|
46
43
|
*/
|
|
47
44
|
export function verifyHmacSha256Hex(secret, data, signatureHex) {
|
|
48
45
|
if (!/^[a-f0-9]+$/i.test(signatureHex))
|
|
@@ -1 +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
|
|
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;;;GAGG;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,IAAI,CAAC,GAAG,GAAG,UAAU,CAAC,GAAG,EAAE,gBAAgB,EAAE,UAAU,CAAC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;AAED;;;GAGG;AACH,MAAM,UAAU,aAAa,CAAC,MAAc,EAAE,IAAY;IACtD,OAAO,UAAU,CAAC,QAAQ,EAAE,MAAM,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;AACnE,CAAC;AAED;;GAEG;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"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@groundbrick/crypto-service",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.1",
|
|
4
4
|
"description": "Encriptação AES-256-GCM para dados sensíveis em repouso",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -22,14 +22,22 @@
|
|
|
22
22
|
},
|
|
23
23
|
"devDependencies": {
|
|
24
24
|
"@types/node": "^20.19.9",
|
|
25
|
-
"typescript": "^5.3.3"
|
|
25
|
+
"typescript": "^5.3.3",
|
|
26
|
+
"vitest": "^2.1.9"
|
|
26
27
|
},
|
|
27
28
|
"engines": {
|
|
28
29
|
"node": ">=18.0.0"
|
|
29
30
|
},
|
|
31
|
+
"repository": {
|
|
32
|
+
"type": "git",
|
|
33
|
+
"url": "git+https://github.com/200Systems/groundbrick.git",
|
|
34
|
+
"directory": "packages/crypto-service"
|
|
35
|
+
},
|
|
30
36
|
"scripts": {
|
|
31
37
|
"build": "tsc",
|
|
32
38
|
"dev": "tsc --watch",
|
|
33
|
-
"clean": "rm -rf dist *.tsbuildinfo"
|
|
39
|
+
"clean": "rm -rf dist *.tsbuildinfo",
|
|
40
|
+
"test": "vitest run",
|
|
41
|
+
"test:watch": "vitest"
|
|
34
42
|
}
|
|
35
43
|
}
|