@excofy/utils 2.0.0 → 2.1.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.
@@ -20,9 +20,6 @@ jobs:
20
20
  - name: Install Dependencies
21
21
  run: npm ci
22
22
 
23
- - name: Lint Code
24
- run: npm run lint
25
-
26
23
  - name: Build Package
27
24
  run: npm run build
28
25
 
package/dist/index.cjs CHANGED
@@ -729,9 +729,24 @@ var decryptPayload = async ({
729
729
  );
730
730
  return decoder.decode(buffer);
731
731
  };
732
+ async function generateDeterministicHash(code, secret) {
733
+ const key = await crypto.subtle.importKey(
734
+ "raw",
735
+ encoder.encode(secret),
736
+ { name: "HMAC", hash: "SHA-256" },
737
+ false,
738
+ ["sign"]
739
+ );
740
+ const signature = await crypto.subtle.sign("HMAC", key, encoder.encode(code));
741
+ return Array.from(new Uint8Array(signature)).map((b) => b.toString(16).padStart(2, "0")).join("");
742
+ }
743
+ async function verifyDeterministicHash(inputCode, storedHash, secret) {
744
+ const hash = await generateDeterministicHash(inputCode, secret);
745
+ return hash === storedHash;
746
+ }
732
747
  var cryptoUtils = {
733
748
  uuidV4: () => crypto.randomUUID(),
734
- hash: async (password, salt) => {
749
+ hash: async (password, salt = 10) => {
735
750
  const importedKey = await crypto.subtle.importKey(
736
751
  "raw",
737
752
  encoder.encode(password),
@@ -807,7 +822,9 @@ var cryptoUtils = {
807
822
  return Array.from(new Uint8Array(buffer)).map((b) => b.toString(16).padStart(2, "0")).join("");
808
823
  },
809
824
  encryptPayload,
810
- decryptPayload
825
+ decryptPayload,
826
+ generateDeterministicHash,
827
+ verifyDeterministicHash
811
828
  };
812
829
 
813
830
  // src/helpers/slug.ts
package/dist/index.d.cts CHANGED
@@ -133,6 +133,8 @@ interface ICrypto {
133
133
  AUTH_PAYLOAD_SECRET: string;
134
134
  token: string;
135
135
  }) => Promise<string>;
136
+ generateDeterministicHash: (code: string, secret: string) => Promise<string>;
137
+ verifyDeterministicHash: (inputCode: string, storedHash: string, secret: string) => Promise<boolean>;
136
138
  }
137
139
  declare const cryptoUtils: ICrypto;
138
140
 
package/dist/index.d.ts CHANGED
@@ -133,6 +133,8 @@ interface ICrypto {
133
133
  AUTH_PAYLOAD_SECRET: string;
134
134
  token: string;
135
135
  }) => Promise<string>;
136
+ generateDeterministicHash: (code: string, secret: string) => Promise<string>;
137
+ verifyDeterministicHash: (inputCode: string, storedHash: string, secret: string) => Promise<boolean>;
136
138
  }
137
139
  declare const cryptoUtils: ICrypto;
138
140
 
package/dist/index.js CHANGED
@@ -693,9 +693,24 @@ var decryptPayload = async ({
693
693
  );
694
694
  return decoder.decode(buffer);
695
695
  };
696
+ async function generateDeterministicHash(code, secret) {
697
+ const key = await crypto.subtle.importKey(
698
+ "raw",
699
+ encoder.encode(secret),
700
+ { name: "HMAC", hash: "SHA-256" },
701
+ false,
702
+ ["sign"]
703
+ );
704
+ const signature = await crypto.subtle.sign("HMAC", key, encoder.encode(code));
705
+ return Array.from(new Uint8Array(signature)).map((b) => b.toString(16).padStart(2, "0")).join("");
706
+ }
707
+ async function verifyDeterministicHash(inputCode, storedHash, secret) {
708
+ const hash = await generateDeterministicHash(inputCode, secret);
709
+ return hash === storedHash;
710
+ }
696
711
  var cryptoUtils = {
697
712
  uuidV4: () => crypto.randomUUID(),
698
- hash: async (password, salt) => {
713
+ hash: async (password, salt = 10) => {
699
714
  const importedKey = await crypto.subtle.importKey(
700
715
  "raw",
701
716
  encoder.encode(password),
@@ -771,7 +786,9 @@ var cryptoUtils = {
771
786
  return Array.from(new Uint8Array(buffer)).map((b) => b.toString(16).padStart(2, "0")).join("");
772
787
  },
773
788
  encryptPayload,
774
- decryptPayload
789
+ decryptPayload,
790
+ generateDeterministicHash,
791
+ verifyDeterministicHash
775
792
  };
776
793
 
777
794
  // src/helpers/slug.ts
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@excofy/utils",
3
- "version": "2.0.0",
3
+ "version": "2.1.0",
4
4
  "description": "Biblioteca de utilitários para o Excofy",
5
5
  "main": "dist/index.js",
6
6
  "type": "module",
@@ -25,6 +25,12 @@ interface ICrypto {
25
25
  AUTH_PAYLOAD_SECRET: string;
26
26
  token: string;
27
27
  }) => Promise<string>;
28
+ generateDeterministicHash: (code: string, secret: string) => Promise<string>;
29
+ verifyDeterministicHash: (
30
+ inputCode: string,
31
+ storedHash: string,
32
+ secret: string
33
+ ) => Promise<boolean>;
28
34
  }
29
35
 
30
36
  /* --------------------- Helpers básicos --------------------- */
@@ -94,11 +100,55 @@ const decryptPayload = async ({
94
100
  return decoder.decode(buffer);
95
101
  };
96
102
 
103
+ /* --------------------- Hash (generate/verify) --------------------- */
104
+ /**
105
+ * Gera um hash determinístico (HMAC) a partir de um código e um segredo.
106
+ *
107
+ * @param code
108
+ * @param secret
109
+ * @returns
110
+ */
111
+ export async function generateDeterministicHash(
112
+ code: string,
113
+ secret: string
114
+ ): Promise<string> {
115
+ const key = await crypto.subtle.importKey(
116
+ 'raw',
117
+ encoder.encode(secret),
118
+ { name: 'HMAC', hash: 'SHA-256' },
119
+ false,
120
+ ['sign']
121
+ );
122
+
123
+ const signature = await crypto.subtle.sign('HMAC', key, encoder.encode(code));
124
+
125
+ return Array.from(new Uint8Array(signature))
126
+ .map((b) => b.toString(16).padStart(2, '0'))
127
+ .join('');
128
+ }
129
+
130
+ /**
131
+ * Verifica se o hash gerado a partir do código e segredo bate com o hash armazenado.
132
+ *
133
+ * @param inputCode
134
+ * @param storedHash
135
+ * @param secret
136
+ * @returns
137
+ */
138
+ export async function verifyDeterministicHash(
139
+ inputCode: string,
140
+ storedHash: string,
141
+ secret: string
142
+ ): Promise<boolean> {
143
+ const hash = await generateDeterministicHash(inputCode, secret);
144
+ return hash === storedHash;
145
+ }
146
+
97
147
  /* --------------------- Main API --------------------- */
98
148
  export const cryptoUtils: ICrypto = {
99
149
  uuidV4: (): string => crypto.randomUUID(),
100
150
 
101
- hash: async (password: string, salt: number): Promise<string> => {
151
+ hash: async (password: string, salt: number = 10): Promise<string> => {
102
152
  const importedKey = await crypto.subtle.importKey(
103
153
  'raw',
104
154
  encoder.encode(password),
@@ -199,4 +249,7 @@ export const cryptoUtils: ICrypto = {
199
249
 
200
250
  encryptPayload,
201
251
  decryptPayload,
252
+
253
+ generateDeterministicHash,
254
+ verifyDeterministicHash,
202
255
  };