@excofy/utils 2.4.0 → 2.5.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.
package/dist/index.cjs CHANGED
@@ -809,6 +809,19 @@ async function verifyDeterministicHash(inputCode, storedHash, secret) {
809
809
  }
810
810
  var cryptoUtils = {
811
811
  uuidV4: () => crypto.randomUUID(),
812
+ ulid: () => {
813
+ function randomChar() {
814
+ const chars = "0123456789ABCDEFGHJKMNPQRSTVWXYZ";
815
+ return chars[Math.floor(Math.random() * chars.length)];
816
+ }
817
+ const timestamp = Date.now();
818
+ const timestampStr = timestamp.toString(36).toUpperCase().padStart(10, "0");
819
+ let randomPart = "";
820
+ for (let i = 0; i < 16; i++) {
821
+ randomPart += randomChar();
822
+ }
823
+ return timestampStr + randomPart;
824
+ },
812
825
  hash: async (password, salt = 10) => {
813
826
  const importedKey = await crypto.subtle.importKey(
814
827
  "raw",
package/dist/index.d.cts CHANGED
@@ -122,6 +122,7 @@ interface IValidateAccessToken {
122
122
  }
123
123
  interface ICrypto {
124
124
  uuidV4: () => string;
125
+ ulid: () => string;
125
126
  hash: (password: string, salt: number) => Promise<string>;
126
127
  isMatch: (password: string, hash: string, salt?: number) => Promise<boolean>;
127
128
  generateAccessToken: (data: IGenerateAccessToken) => Promise<string>;
package/dist/index.d.ts CHANGED
@@ -122,6 +122,7 @@ interface IValidateAccessToken {
122
122
  }
123
123
  interface ICrypto {
124
124
  uuidV4: () => string;
125
+ ulid: () => string;
125
126
  hash: (password: string, salt: number) => Promise<string>;
126
127
  isMatch: (password: string, hash: string, salt?: number) => Promise<boolean>;
127
128
  generateAccessToken: (data: IGenerateAccessToken) => Promise<string>;
package/dist/index.js CHANGED
@@ -771,6 +771,19 @@ async function verifyDeterministicHash(inputCode, storedHash, secret) {
771
771
  }
772
772
  var cryptoUtils = {
773
773
  uuidV4: () => crypto.randomUUID(),
774
+ ulid: () => {
775
+ function randomChar() {
776
+ const chars = "0123456789ABCDEFGHJKMNPQRSTVWXYZ";
777
+ return chars[Math.floor(Math.random() * chars.length)];
778
+ }
779
+ const timestamp = Date.now();
780
+ const timestampStr = timestamp.toString(36).toUpperCase().padStart(10, "0");
781
+ let randomPart = "";
782
+ for (let i = 0; i < 16; i++) {
783
+ randomPart += randomChar();
784
+ }
785
+ return timestampStr + randomPart;
786
+ },
774
787
  hash: async (password, salt = 10) => {
775
788
  const importedKey = await crypto.subtle.importKey(
776
789
  "raw",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@excofy/utils",
3
- "version": "2.4.0",
3
+ "version": "2.5.0",
4
4
  "description": "Biblioteca de utilitários para o Excofy",
5
5
  "main": "dist/index.js",
6
6
  "type": "module",
@@ -17,6 +17,7 @@ interface IValidateAccessToken {
17
17
 
18
18
  interface ICrypto {
19
19
  uuidV4: () => string;
20
+ ulid: () => string;
20
21
  hash: (password: string, salt: number) => Promise<string>;
21
22
  isMatch: (password: string, hash: string, salt?: number) => Promise<boolean>;
22
23
  generateAccessToken: (data: IGenerateAccessToken) => Promise<string>;
@@ -34,7 +35,7 @@ interface ICrypto {
34
35
  verifyDeterministicHash: (
35
36
  inputCode: string,
36
37
  storedHash: string,
37
- secret: string
38
+ secret: string,
38
39
  ) => Promise<boolean>;
39
40
  }
40
41
 
@@ -54,7 +55,7 @@ const signatureKey = async (AUTH_SIGN_SECRET: string) =>
54
55
  encoder.encode(AUTH_SIGN_SECRET),
55
56
  { name: 'HMAC', hash: 'SHA-256' },
56
57
  false,
57
- ['sign', 'verify']
58
+ ['sign', 'verify'],
58
59
  );
59
60
 
60
61
  const payloadKey = async (AUTH_PAYLOAD_SECRET: string) =>
@@ -63,7 +64,7 @@ const payloadKey = async (AUTH_PAYLOAD_SECRET: string) =>
63
64
  encoder.encode(AUTH_PAYLOAD_SECRET.substring(0, 32)),
64
65
  { name: 'AES-GCM', length: 256 },
65
66
  true,
66
- ['encrypt', 'decrypt']
67
+ ['encrypt', 'decrypt'],
67
68
  );
68
69
 
69
70
  /* --------------------- Payload (encrypt/decrypt) --------------------- */
@@ -80,7 +81,7 @@ const encryptPayload = async ({
80
81
  const buffer = await crypto.subtle.encrypt(
81
82
  { name: 'AES-GCM', iv },
82
83
  key,
83
- encoder.encode(payload)
84
+ encoder.encode(payload),
84
85
  );
85
86
 
86
87
  return `${toBase64(iv)}.${toBase64(buffer)}`;
@@ -99,7 +100,7 @@ const decryptPayload = async ({
99
100
  const buffer = await crypto.subtle.decrypt(
100
101
  { name: 'AES-GCM', iv: fromBase64(header).buffer as ArrayBuffer },
101
102
  key,
102
- fromBase64(payload).buffer as ArrayBuffer
103
+ fromBase64(payload).buffer as ArrayBuffer,
103
104
  );
104
105
 
105
106
  return decoder.decode(buffer);
@@ -115,14 +116,14 @@ const decryptPayload = async ({
115
116
  */
116
117
  export async function generateDeterministicHash(
117
118
  code: string,
118
- secret: string
119
+ secret: string,
119
120
  ): Promise<string> {
120
121
  const key = await crypto.subtle.importKey(
121
122
  'raw',
122
123
  encoder.encode(secret),
123
124
  { name: 'HMAC', hash: 'SHA-256' },
124
125
  false,
125
- ['sign']
126
+ ['sign'],
126
127
  );
127
128
 
128
129
  const signature = await crypto.subtle.sign('HMAC', key, encoder.encode(code));
@@ -143,7 +144,7 @@ export async function generateDeterministicHash(
143
144
  export async function verifyDeterministicHash(
144
145
  inputCode: string,
145
146
  storedHash: string,
146
- secret: string
147
+ secret: string,
147
148
  ): Promise<boolean> {
148
149
  const hash = await generateDeterministicHash(inputCode, secret);
149
150
  return hash === storedHash;
@@ -153,13 +154,30 @@ export async function verifyDeterministicHash(
153
154
  export const cryptoUtils: ICrypto = {
154
155
  uuidV4: (): string => crypto.randomUUID(),
155
156
 
157
+ ulid: (): string => {
158
+ function randomChar() {
159
+ const chars = '0123456789ABCDEFGHJKMNPQRSTVWXYZ'; // Crockford's Base32
160
+ return chars[Math.floor(Math.random() * chars.length)];
161
+ }
162
+
163
+ const timestamp = Date.now();
164
+ const timestampStr = timestamp.toString(36).toUpperCase().padStart(10, '0');
165
+
166
+ let randomPart = '';
167
+ for (let i = 0; i < 16; i++) {
168
+ randomPart += randomChar();
169
+ }
170
+
171
+ return timestampStr + randomPart;
172
+ },
173
+
156
174
  hash: async (password: string, salt = 10): Promise<string> => {
157
175
  const importedKey = await crypto.subtle.importKey(
158
176
  'raw',
159
177
  encoder.encode(password),
160
178
  { name: 'PBKDF2' },
161
179
  false,
162
- ['deriveBits']
180
+ ['deriveBits'],
163
181
  );
164
182
 
165
183
  const derivedKey = await crypto.subtle.deriveBits(
@@ -170,7 +188,7 @@ export const cryptoUtils: ICrypto = {
170
188
  hash: 'SHA-256',
171
189
  },
172
190
  importedKey,
173
- 256
191
+ 256,
174
192
  );
175
193
 
176
194
  return Array.from(new Uint8Array(derivedKey))
@@ -200,7 +218,7 @@ export const cryptoUtils: ICrypto = {
200
218
  const signatureBuffer = await crypto.subtle.sign(
201
219
  'HMAC',
202
220
  key,
203
- encoder.encode(payload)
221
+ encoder.encode(payload),
204
222
  );
205
223
  const signature = toBase64(signatureBuffer);
206
224
 
@@ -227,7 +245,7 @@ export const cryptoUtils: ICrypto = {
227
245
  'HMAC',
228
246
  key,
229
247
  fromBase64(signature).buffer as ArrayBuffer,
230
- encoder.encode(payloadDecrypted)
248
+ encoder.encode(payloadDecrypted),
231
249
  );
232
250
 
233
251
  if (!valid) throw new Error('Invalid access token');