@unchainedshop/utils 4.5.0 → 4.6.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.
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import Hashids from 'hashids';
|
|
2
2
|
const hashids = new Hashids('unchained', 6, 'ABCDEFGHIJKLMNPQRSTUVWXYZ23456789');
|
|
3
3
|
export default () => {
|
|
4
|
-
const
|
|
4
|
+
const randomBytes = crypto.getRandomValues(new Uint32Array(1));
|
|
5
|
+
const randomNumber = (randomBytes[0] % 999999998) + 1;
|
|
5
6
|
return hashids.encode(randomNumber);
|
|
6
7
|
};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export async function timingSafeEqual(bufferA, bufferB) {
|
|
2
|
+
if (bufferA.byteLength !== bufferB.byteLength) {
|
|
3
|
+
return false;
|
|
4
|
+
}
|
|
5
|
+
const algorithm = { name: 'HMAC', hash: 'SHA-256' };
|
|
6
|
+
const key = await crypto.subtle.generateKey(algorithm, false, ['sign', 'verify']);
|
|
7
|
+
const signature = await crypto.subtle.sign(algorithm, key, bufferA);
|
|
8
|
+
return crypto.subtle.verify(algorithm, key, signature, bufferB);
|
|
9
|
+
}
|
|
10
|
+
export async function timingSafeStringEqual(a, b) {
|
|
11
|
+
if (a === null || a === undefined || b === null || b === undefined) {
|
|
12
|
+
return false;
|
|
13
|
+
}
|
|
14
|
+
const encoder = new TextEncoder();
|
|
15
|
+
const bufferA = encoder.encode(a);
|
|
16
|
+
const bufferB = encoder.encode(b);
|
|
17
|
+
return timingSafeEqual(bufferA.buffer, bufferB.buffer);
|
|
18
|
+
}
|
package/lib/utils-index.d.ts
CHANGED
|
@@ -8,6 +8,7 @@ export { default as generateRandomHash } from './generate-random-hash.ts';
|
|
|
8
8
|
export { default as buildObfuscatedFieldsFilter } from './build-obfuscated-fields-filter.ts';
|
|
9
9
|
export { default as sha256 } from './sha256.ts';
|
|
10
10
|
export { default as sha1 } from './sha1.ts';
|
|
11
|
+
export { timingSafeEqual, timingSafeStringEqual } from './timing-safe-equal.ts';
|
|
11
12
|
export declare const SortDirection: {
|
|
12
13
|
readonly ASC: "ASC";
|
|
13
14
|
readonly DESC: "DESC";
|
package/lib/utils-index.js
CHANGED
|
@@ -8,6 +8,7 @@ export { default as generateRandomHash } from "./generate-random-hash.js";
|
|
|
8
8
|
export { default as buildObfuscatedFieldsFilter } from "./build-obfuscated-fields-filter.js";
|
|
9
9
|
export { default as sha256 } from "./sha256.js";
|
|
10
10
|
export { default as sha1 } from "./sha1.js";
|
|
11
|
+
export { timingSafeEqual, timingSafeStringEqual } from "./timing-safe-equal.js";
|
|
11
12
|
export const SortDirection = {
|
|
12
13
|
ASC: 'ASC',
|
|
13
14
|
DESC: 'DESC',
|
package/package.json
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@unchainedshop/utils",
|
|
3
|
-
"
|
|
3
|
+
"description": "Common utility functions and base classes for the Unchained Engine",
|
|
4
|
+
"version": "4.6.1",
|
|
4
5
|
"main": "lib/utils-index.js",
|
|
5
6
|
"types": "lib/utils-index.d.ts",
|
|
6
7
|
"type": "module",
|
|
@@ -28,7 +29,7 @@
|
|
|
28
29
|
},
|
|
29
30
|
"homepage": "https://github.com/unchainedshop/unchained#readme",
|
|
30
31
|
"dependencies": {
|
|
31
|
-
"@unchainedshop/logger": "^4.
|
|
32
|
+
"@unchainedshop/logger": "^4.6.0",
|
|
32
33
|
"hashids": "^2.3.0",
|
|
33
34
|
"resolve-accept-language": "^3.1.11"
|
|
34
35
|
},
|