@nzyme/crypto 0.0.0-stage → 0.15.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/getBasicAuthHeader.d.ts +16 -0
- package/dist/getBasicAuthHeader.js +16 -0
- package/dist/getBasicAuthHeader.js.map +1 -0
- package/dist/getMd5Hash.browser.d.ts +10 -0
- package/dist/getMd5Hash.browser.js +14 -0
- package/dist/getMd5Hash.browser.js.map +1 -0
- package/dist/getMd5Hash.node.d.ts +11 -0
- package/dist/getMd5Hash.node.js +15 -0
- package/dist/getMd5Hash.node.js.map +1 -0
- package/dist/index.browser.d.ts +4 -0
- package/dist/index.browser.js +5 -0
- package/dist/index.browser.js.map +1 -0
- package/dist/index.common.d.ts +6 -0
- package/dist/index.common.js +7 -0
- package/dist/index.common.js.map +1 -0
- package/dist/index.node.d.ts +16 -0
- package/dist/index.node.js +17 -0
- package/dist/index.node.js.map +1 -0
- package/dist/parseBasicAuth.d.ts +14 -0
- package/dist/parseBasicAuth.js +23 -0
- package/dist/parseBasicAuth.js.map +1 -0
- package/dist/parseBearerToken.d.ts +10 -0
- package/dist/parseBearerToken.js +18 -0
- package/dist/parseBearerToken.js.map +1 -0
- package/dist/randomInRange.d.ts +9 -0
- package/dist/randomInRange.js +12 -0
- package/dist/randomInRange.js.map +1 -0
- package/dist/randomInt.browser.d.ts +9 -0
- package/dist/randomInt.browser.js +12 -0
- package/dist/randomInt.browser.js.map +1 -0
- package/dist/randomInt.node.d.ts +9 -0
- package/dist/randomInt.node.js +13 -0
- package/dist/randomInt.node.js.map +1 -0
- package/dist/randomString.d.ts +29 -0
- package/dist/randomString.js +36 -0
- package/dist/randomString.js.map +1 -0
- package/dist/randomUuid.browser.d.ts +9 -0
- package/dist/randomUuid.browser.js +11 -0
- package/dist/randomUuid.browser.js.map +1 -0
- package/dist/randomUuid.node.d.ts +7 -0
- package/dist/randomUuid.node.js +8 -0
- package/dist/randomUuid.node.js.map +1 -0
- package/dist/rngMulberry32.d.ts +12 -0
- package/dist/rngMulberry32.js +21 -0
- package/dist/rngMulberry32.js.map +1 -0
- package/dist/stringEqualsTimingSafe.node.d.ts +13 -0
- package/dist/stringEqualsTimingSafe.node.js +22 -0
- package/dist/stringEqualsTimingSafe.node.js.map +1 -0
- package/dist/validateBasicAuth.d.ts +24 -0
- package/dist/validateBasicAuth.js +21 -0
- package/dist/validateBasicAuth.js.map +1 -0
- package/dist/validateBearerToken.d.ts +22 -0
- package/dist/validateBearerToken.js +21 -0
- package/dist/validateBearerToken.js.map +1 -0
- package/package.json +58 -5
- package/README.md +0 -3
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creates a Basic Authentication header value from user credentials
|
|
3
|
+
*
|
|
4
|
+
* Formats the user and password as a base64-encoded string in the format
|
|
5
|
+
* required for HTTP Basic Authentication.
|
|
6
|
+
*
|
|
7
|
+
* @util
|
|
8
|
+
* @param params - Object containing user and password
|
|
9
|
+
* @param params.password - Password for authentication
|
|
10
|
+
* @param params.user - Username or login
|
|
11
|
+
* @returns Formatted Basic Auth header value ('Basic base64-encoded-credentials')
|
|
12
|
+
*/
|
|
13
|
+
export declare function getBasicAuthHeader(params: {
|
|
14
|
+
password: string;
|
|
15
|
+
user: string;
|
|
16
|
+
}): string;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creates a Basic Authentication header value from user credentials
|
|
3
|
+
*
|
|
4
|
+
* Formats the user and password as a base64-encoded string in the format
|
|
5
|
+
* required for HTTP Basic Authentication.
|
|
6
|
+
*
|
|
7
|
+
* @util
|
|
8
|
+
* @param params - Object containing user and password
|
|
9
|
+
* @param params.password - Password for authentication
|
|
10
|
+
* @param params.user - Username or login
|
|
11
|
+
* @returns Formatted Basic Auth header value ('Basic base64-encoded-credentials')
|
|
12
|
+
*/
|
|
13
|
+
export function getBasicAuthHeader(params) {
|
|
14
|
+
return `Basic ${Buffer.from(`${params.user}:${params.password}`).toString('base64')}`;
|
|
15
|
+
}
|
|
16
|
+
//# sourceMappingURL=getBasicAuthHeader.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getBasicAuthHeader.js","sourceRoot":"","sources":["../src/getBasicAuthHeader.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,kBAAkB,CAAC,MAA0C;IACzE,OAAO,SAAS,MAAM,CAAC,IAAI,CAAC,GAAG,MAAM,CAAC,IAAI,IAAI,MAAM,CAAC,QAAQ,EAAE,CAAC,CAAC,QAAQ,CAAC,QAAQ,CAAC,EAAE,CAAC;AAC1F,CAAC"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Calculates the MD5 hash of the provided data (browser implementation)
|
|
3
|
+
*
|
|
4
|
+
* Uses the spark-md5 library for browser-compatible MD5 hash generation.
|
|
5
|
+
*
|
|
6
|
+
* @util
|
|
7
|
+
* @param data - The string data to hash
|
|
8
|
+
* @returns Hexadecimal MD5 hash string
|
|
9
|
+
*/
|
|
10
|
+
export declare function getMd5Hash(data: string): string;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import SparkMd5 from 'spark-md5';
|
|
2
|
+
/**
|
|
3
|
+
* Calculates the MD5 hash of the provided data (browser implementation)
|
|
4
|
+
*
|
|
5
|
+
* Uses the spark-md5 library for browser-compatible MD5 hash generation.
|
|
6
|
+
*
|
|
7
|
+
* @util
|
|
8
|
+
* @param data - The string data to hash
|
|
9
|
+
* @returns Hexadecimal MD5 hash string
|
|
10
|
+
*/
|
|
11
|
+
export function getMd5Hash(data) {
|
|
12
|
+
return SparkMd5.hash(data);
|
|
13
|
+
}
|
|
14
|
+
//# sourceMappingURL=getMd5Hash.browser.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getMd5Hash.browser.js","sourceRoot":"","sources":["../src/getMd5Hash.browser.ts"],"names":[],"mappings":"AAAA,OAAO,QAAQ,MAAM,WAAW,CAAC;AAEjC;;;;;;;;GAQG;AACH,MAAM,UAAU,UAAU,CAAC,IAAY;IACnC,OAAO,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;AAC/B,CAAC"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { BinaryLike } from 'node:crypto';
|
|
2
|
+
/**
|
|
3
|
+
* Calculates the MD5 hash of the provided data (Node.js implementation)
|
|
4
|
+
*
|
|
5
|
+
* Uses the Node.js crypto module to generate a hexadecimal MD5 hash.
|
|
6
|
+
*
|
|
7
|
+
* @util
|
|
8
|
+
* @param data - The data to hash, can be a string, Buffer, or other BinaryLike type
|
|
9
|
+
* @returns Hexadecimal MD5 hash string
|
|
10
|
+
*/
|
|
11
|
+
export declare function getMd5Hash(data: BinaryLike): string;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto';
|
|
2
|
+
/**
|
|
3
|
+
* Calculates the MD5 hash of the provided data (Node.js implementation)
|
|
4
|
+
*
|
|
5
|
+
* Uses the Node.js crypto module to generate a hexadecimal MD5 hash.
|
|
6
|
+
*
|
|
7
|
+
* @util
|
|
8
|
+
* @param data - The data to hash, can be a string, Buffer, or other BinaryLike type
|
|
9
|
+
* @returns Hexadecimal MD5 hash string
|
|
10
|
+
*/
|
|
11
|
+
export function getMd5Hash(data) {
|
|
12
|
+
const hashData = typeof data === 'string' || ArrayBuffer.isView(data) ? data : new Uint8Array(data);
|
|
13
|
+
return createHash('md5').update(hashData).digest('hex');
|
|
14
|
+
}
|
|
15
|
+
//# sourceMappingURL=getMd5Hash.node.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getMd5Hash.node.js","sourceRoot":"","sources":["../src/getMd5Hash.node.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAEzC;;;;;;;;GAQG;AACH,MAAM,UAAU,UAAU,CAAC,IAAgB;IACvC,MAAM,QAAQ,GAAG,OAAO,IAAI,KAAK,QAAQ,IAAI,WAAW,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,UAAU,CAAC,IAAI,CAAC,CAAC;IACpG,OAAO,UAAU,CAAC,KAAK,CAAC,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;AAC5D,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.browser.js","sourceRoot":"","sources":["../src/index.browser.ts"],"names":[],"mappings":"AAAA,cAAc,yBAAyB,CAAC;AACxC,cAAc,mBAAmB,CAAC;AAClC,cAAc,wBAAwB,CAAC;AACvC,cAAc,yBAAyB,CAAC"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export * from './getBasicAuthHeader.js';
|
|
2
|
+
export * from './parseBasicAuth.js';
|
|
3
|
+
export * from './parseBearerToken.js';
|
|
4
|
+
export * from './randomInRange.js';
|
|
5
|
+
export * from './randomString.js';
|
|
6
|
+
export * from './rngMulberry32.js';
|
|
7
|
+
//# sourceMappingURL=index.common.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.common.js","sourceRoot":"","sources":["../src/index.common.ts"],"names":[],"mappings":"AAAA,cAAc,yBAAyB,CAAC;AACxC,cAAc,qBAAqB,CAAC;AACpC,cAAc,uBAAuB,CAAC;AACtC,cAAc,oBAAoB,CAAC;AACnC,cAAc,mBAAmB,CAAC;AAClC,cAAc,oBAAoB,CAAC"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Node.js-specific cryptography and security utilities
|
|
3
|
+
*
|
|
4
|
+
* This module extends the common crypto utilities with Node.js-specific
|
|
5
|
+
* implementations for functions that require Node.js crypto module or
|
|
6
|
+
* need different handling in server environments.
|
|
7
|
+
*
|
|
8
|
+
* @module crypto/node
|
|
9
|
+
*/
|
|
10
|
+
export * from './getMd5Hash.node.js';
|
|
11
|
+
export * from './index.common.js';
|
|
12
|
+
export * from './randomInt.node.js';
|
|
13
|
+
export * from './randomUuid.node.js';
|
|
14
|
+
export * from './stringEqualsTimingSafe.node.js';
|
|
15
|
+
export * from './validateBasicAuth.js';
|
|
16
|
+
export * from './validateBearerToken.js';
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Node.js-specific cryptography and security utilities
|
|
3
|
+
*
|
|
4
|
+
* This module extends the common crypto utilities with Node.js-specific
|
|
5
|
+
* implementations for functions that require Node.js crypto module or
|
|
6
|
+
* need different handling in server environments.
|
|
7
|
+
*
|
|
8
|
+
* @module crypto/node
|
|
9
|
+
*/
|
|
10
|
+
export * from './getMd5Hash.node.js';
|
|
11
|
+
export * from './index.common.js';
|
|
12
|
+
export * from './randomInt.node.js';
|
|
13
|
+
export * from './randomUuid.node.js';
|
|
14
|
+
export * from './stringEqualsTimingSafe.node.js';
|
|
15
|
+
export * from './validateBasicAuth.js';
|
|
16
|
+
export * from './validateBearerToken.js';
|
|
17
|
+
//# sourceMappingURL=index.node.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.node.js","sourceRoot":"","sources":["../src/index.node.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,cAAc,sBAAsB,CAAC;AACrC,cAAc,mBAAmB,CAAC;AAClC,cAAc,qBAAqB,CAAC;AACpC,cAAc,sBAAsB,CAAC;AACrC,cAAc,kCAAkC,CAAC;AACjD,cAAc,wBAAwB,CAAC;AACvC,cAAc,0BAA0B,CAAC"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Parses a Basic Authentication token into login and password components
|
|
3
|
+
*
|
|
4
|
+
* Takes a Basic Auth token (either with or without the 'Basic ' prefix) and
|
|
5
|
+
* decodes the base64-encoded credentials, splitting them into login and password.
|
|
6
|
+
*
|
|
7
|
+
* @util
|
|
8
|
+
* @param token - The Basic Auth token string from an Authorization header
|
|
9
|
+
* @returns Object containing the extracted login and password
|
|
10
|
+
*/
|
|
11
|
+
export declare function parseBasicAuth(token: string): {
|
|
12
|
+
login: string;
|
|
13
|
+
password: string;
|
|
14
|
+
};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
const prefix = 'Basic ';
|
|
2
|
+
/**
|
|
3
|
+
* Parses a Basic Authentication token into login and password components
|
|
4
|
+
*
|
|
5
|
+
* Takes a Basic Auth token (either with or without the 'Basic ' prefix) and
|
|
6
|
+
* decodes the base64-encoded credentials, splitting them into login and password.
|
|
7
|
+
*
|
|
8
|
+
* @util
|
|
9
|
+
* @param token - The Basic Auth token string from an Authorization header
|
|
10
|
+
* @returns Object containing the extracted login and password
|
|
11
|
+
*/
|
|
12
|
+
export function parseBasicAuth(token) {
|
|
13
|
+
token = token.trim();
|
|
14
|
+
if (token.startsWith(prefix)) {
|
|
15
|
+
token = token.substring(prefix.length).trim();
|
|
16
|
+
}
|
|
17
|
+
const [login, password] = Buffer.from(token, 'base64').toString().split(':');
|
|
18
|
+
return {
|
|
19
|
+
login: login ?? '',
|
|
20
|
+
password: password ?? '',
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
//# sourceMappingURL=parseBasicAuth.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"parseBasicAuth.js","sourceRoot":"","sources":["../src/parseBasicAuth.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,GAAG,QAAQ,CAAC;AAExB;;;;;;;;;GASG;AACH,MAAM,UAAU,cAAc,CAAC,KAAa;IACxC,KAAK,GAAG,KAAK,CAAC,IAAI,EAAE,CAAC;IAErB,IAAI,KAAK,CAAC,UAAU,CAAC,MAAM,CAAC,EAAE,CAAC;QAC3B,KAAK,GAAG,KAAK,CAAC,SAAS,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,IAAI,EAAE,CAAC;IAClD,CAAC;IAED,MAAM,CAAC,KAAK,EAAE,QAAQ,CAAC,GAAG,MAAM,CAAC,IAAI,CAAC,KAAK,EAAE,QAAQ,CAAC,CAAC,QAAQ,EAAE,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;IAE7E,OAAO;QACH,KAAK,EAAE,KAAK,IAAI,EAAE;QAClB,QAAQ,EAAE,QAAQ,IAAI,EAAE;KAC3B,CAAC;AACN,CAAC"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Extracts the token value from a Bearer token string
|
|
3
|
+
*
|
|
4
|
+
* Removes the 'Bearer ' prefix if present and returns the actual token value.
|
|
5
|
+
*
|
|
6
|
+
* @util
|
|
7
|
+
* @param token - The Bearer token string from an Authorization header
|
|
8
|
+
* @returns The extracted token value without the 'Bearer ' prefix
|
|
9
|
+
*/
|
|
10
|
+
export declare function parseBearerToken(token: string): string;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
const prefix = 'Bearer ';
|
|
2
|
+
/**
|
|
3
|
+
* Extracts the token value from a Bearer token string
|
|
4
|
+
*
|
|
5
|
+
* Removes the 'Bearer ' prefix if present and returns the actual token value.
|
|
6
|
+
*
|
|
7
|
+
* @util
|
|
8
|
+
* @param token - The Bearer token string from an Authorization header
|
|
9
|
+
* @returns The extracted token value without the 'Bearer ' prefix
|
|
10
|
+
*/
|
|
11
|
+
export function parseBearerToken(token) {
|
|
12
|
+
token = token.trim();
|
|
13
|
+
if (token.startsWith(prefix)) {
|
|
14
|
+
token = token.substring(prefix.length).trim();
|
|
15
|
+
}
|
|
16
|
+
return token;
|
|
17
|
+
}
|
|
18
|
+
//# sourceMappingURL=parseBearerToken.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"parseBearerToken.js","sourceRoot":"","sources":["../src/parseBearerToken.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,GAAG,SAAS,CAAC;AAEzB;;;;;;;;GAQG;AACH,MAAM,UAAU,gBAAgB,CAAC,KAAa;IAC1C,KAAK,GAAG,KAAK,CAAC,IAAI,EAAE,CAAC;IAErB,IAAI,KAAK,CAAC,UAAU,CAAC,MAAM,CAAC,EAAE,CAAC;QAC3B,KAAK,GAAG,KAAK,CAAC,SAAS,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,IAAI,EAAE,CAAC;IAClD,CAAC;IAED,OAAO,KAAK,CAAC;AACjB,CAAC"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Generates a random floating-point number within a specified range
|
|
3
|
+
*
|
|
4
|
+
* @util
|
|
5
|
+
* @param min - The minimum value (inclusive)
|
|
6
|
+
* @param max - The maximum value (exclusive)
|
|
7
|
+
* @returns A random number between min (inclusive) and max (exclusive)
|
|
8
|
+
*/
|
|
9
|
+
export declare function randomInRange(min: number, max: number): number;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Generates a random floating-point number within a specified range
|
|
3
|
+
*
|
|
4
|
+
* @util
|
|
5
|
+
* @param min - The minimum value (inclusive)
|
|
6
|
+
* @param max - The maximum value (exclusive)
|
|
7
|
+
* @returns A random number between min (inclusive) and max (exclusive)
|
|
8
|
+
*/
|
|
9
|
+
export function randomInRange(min, max) {
|
|
10
|
+
return Math.random() * (max - min) + min;
|
|
11
|
+
}
|
|
12
|
+
//# sourceMappingURL=randomInRange.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"randomInRange.js","sourceRoot":"","sources":["../src/randomInRange.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,MAAM,UAAU,aAAa,CAAC,GAAW,EAAE,GAAW;IAClD,OAAO,IAAI,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,GAAG,GAAG,CAAC,GAAG,GAAG,CAAC;AAC7C,CAAC"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns a random integer between min (inclusive) and max (exclusive).
|
|
3
|
+
*
|
|
4
|
+
* @util
|
|
5
|
+
* @param min - The minimum value (inclusive).
|
|
6
|
+
* @param max - The maximum value (exclusive).
|
|
7
|
+
* @returns A random integer between min and max.
|
|
8
|
+
*/
|
|
9
|
+
export declare function randomInt(min?: number, max?: number): number;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns a random integer between min (inclusive) and max (exclusive).
|
|
3
|
+
*
|
|
4
|
+
* @util
|
|
5
|
+
* @param min - The minimum value (inclusive).
|
|
6
|
+
* @param max - The maximum value (exclusive).
|
|
7
|
+
* @returns A random integer between min and max.
|
|
8
|
+
*/
|
|
9
|
+
export function randomInt(min = 0, max = Number.MAX_SAFE_INTEGER) {
|
|
10
|
+
return Math.floor(Math.random() * (max - min)) + min;
|
|
11
|
+
}
|
|
12
|
+
//# sourceMappingURL=randomInt.browser.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"randomInt.browser.js","sourceRoot":"","sources":["../src/randomInt.browser.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,MAAM,UAAU,SAAS,CAAC,GAAG,GAAW,CAAC,EAAE,GAAG,GAAW,MAAM,CAAC,gBAAgB;IAC5E,OAAO,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,GAAG,GAAG,CAAC,CAAC,GAAG,GAAG,CAAC;AACzD,CAAC"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns a random integer between min (inclusive) and max (exclusive).
|
|
3
|
+
*
|
|
4
|
+
* @util
|
|
5
|
+
* @param min - The minimum value (inclusive).
|
|
6
|
+
* @param max - The maximum value (exclusive).
|
|
7
|
+
* @returns A random integer between min and max.
|
|
8
|
+
*/
|
|
9
|
+
export declare function randomInt(min?: number, max?: number): number;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { randomInt as randomIntCrypto } from 'node:crypto';
|
|
2
|
+
/**
|
|
3
|
+
* Returns a random integer between min (inclusive) and max (exclusive).
|
|
4
|
+
*
|
|
5
|
+
* @util
|
|
6
|
+
* @param min - The minimum value (inclusive).
|
|
7
|
+
* @param max - The maximum value (exclusive).
|
|
8
|
+
* @returns A random integer between min and max.
|
|
9
|
+
*/
|
|
10
|
+
export function randomInt(min = 0, max = Number.MAX_SAFE_INTEGER) {
|
|
11
|
+
return randomIntCrypto(min, max);
|
|
12
|
+
}
|
|
13
|
+
//# sourceMappingURL=randomInt.node.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"randomInt.node.js","sourceRoot":"","sources":["../src/randomInt.node.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,IAAI,eAAe,EAAE,MAAM,aAAa,CAAC;AAE3D;;;;;;;GAOG;AACH,MAAM,UAAU,SAAS,CAAC,GAAG,GAAW,CAAC,EAAE,GAAG,GAAW,MAAM,CAAC,gBAAgB;IAC5E,OAAO,eAAe,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC;AACrC,CAAC"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Numeric characters (0-9)
|
|
3
|
+
*/
|
|
4
|
+
export declare const CHARS_NUMBERS = "0123456789";
|
|
5
|
+
/**
|
|
6
|
+
* Lowercase alphabetic characters (a-z)
|
|
7
|
+
*/
|
|
8
|
+
export declare const CHARS_LOWERCASE = "abcdefghijklmnopqrstuvwxyz";
|
|
9
|
+
/**
|
|
10
|
+
* Uppercase alphabetic characters (A-Z)
|
|
11
|
+
*/
|
|
12
|
+
export declare const CHARS_UPPERCASE = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
|
13
|
+
/**
|
|
14
|
+
* All alphabetic characters (a-z, A-Z)
|
|
15
|
+
*/
|
|
16
|
+
export declare const CHARS_LETTERS: string;
|
|
17
|
+
/**
|
|
18
|
+
* Base36 character set (0-9, a-z)
|
|
19
|
+
*/
|
|
20
|
+
export declare const CHARS_BASE36: string;
|
|
21
|
+
/**
|
|
22
|
+
* Generates a random string of specified length using provided character set
|
|
23
|
+
*
|
|
24
|
+
* @util
|
|
25
|
+
* @param length - The length of the random string to generate
|
|
26
|
+
* @param characters - Character set to use for generating the random string (defaults to Base36)
|
|
27
|
+
* @returns A random string of the specified length
|
|
28
|
+
*/
|
|
29
|
+
export declare function randomString(length: number, characters?: string): string;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Numeric characters (0-9)
|
|
3
|
+
*/
|
|
4
|
+
export const CHARS_NUMBERS = '0123456789';
|
|
5
|
+
/**
|
|
6
|
+
* Lowercase alphabetic characters (a-z)
|
|
7
|
+
*/
|
|
8
|
+
export const CHARS_LOWERCASE = 'abcdefghijklmnopqrstuvwxyz';
|
|
9
|
+
/**
|
|
10
|
+
* Uppercase alphabetic characters (A-Z)
|
|
11
|
+
*/
|
|
12
|
+
export const CHARS_UPPERCASE = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
|
13
|
+
/**
|
|
14
|
+
* All alphabetic characters (a-z, A-Z)
|
|
15
|
+
*/
|
|
16
|
+
export const CHARS_LETTERS = CHARS_LOWERCASE + CHARS_UPPERCASE;
|
|
17
|
+
/**
|
|
18
|
+
* Base36 character set (0-9, a-z)
|
|
19
|
+
*/
|
|
20
|
+
export const CHARS_BASE36 = CHARS_NUMBERS + CHARS_LOWERCASE;
|
|
21
|
+
/**
|
|
22
|
+
* Generates a random string of specified length using provided character set
|
|
23
|
+
*
|
|
24
|
+
* @util
|
|
25
|
+
* @param length - The length of the random string to generate
|
|
26
|
+
* @param characters - Character set to use for generating the random string (defaults to Base36)
|
|
27
|
+
* @returns A random string of the specified length
|
|
28
|
+
*/
|
|
29
|
+
export function randomString(length, characters = CHARS_BASE36) {
|
|
30
|
+
let result = '';
|
|
31
|
+
for (let i = 0; i < length; i++) {
|
|
32
|
+
result += characters.charAt(Math.floor(Math.random() * characters.length));
|
|
33
|
+
}
|
|
34
|
+
return result;
|
|
35
|
+
}
|
|
36
|
+
//# sourceMappingURL=randomString.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"randomString.js","sourceRoot":"","sources":["../src/randomString.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,CAAC,MAAM,aAAa,GAAG,YAAY,CAAC;AAE1C;;GAEG;AACH,MAAM,CAAC,MAAM,eAAe,GAAG,4BAA4B,CAAC;AAE5D;;GAEG;AACH,MAAM,CAAC,MAAM,eAAe,GAAG,4BAA4B,CAAC;AAE5D;;GAEG;AACH,MAAM,CAAC,MAAM,aAAa,GAAG,eAAe,GAAG,eAAe,CAAC;AAE/D;;GAEG;AACH,MAAM,CAAC,MAAM,YAAY,GAAG,aAAa,GAAG,eAAe,CAAC;AAE5D;;;;;;;GAOG;AACH,MAAM,UAAU,YAAY,CAAC,MAAc,EAAE,UAAU,GAAW,YAAY;IAC1E,IAAI,MAAM,GAAG,EAAE,CAAC;IAChB,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;QAC9B,MAAM,IAAI,UAAU,CAAC,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,MAAM,EAAE,GAAG,UAAU,CAAC,MAAM,CAAC,CAAC,CAAC;IAC/E,CAAC;IACD,OAAO,MAAM,CAAC;AAClB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"randomUuid.browser.js","sourceRoot":"","sources":["../src/randomUuid.browser.ts"],"names":[],"mappings":"AAEA;;;;;;GAMG;AACH,MAAM,UAAU,UAAU;IACtB,OAAO,MAAM,CAAC,UAAU,EAAE,CAAC;AAC/B,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"randomUuid.node.js","sourceRoot":"","sources":["../src/randomUuid.node.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAEzC;;;;GAIG;AACH,MAAM,CAAC,MAAM,UAAU,GAAG,UAAU,CAAC"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creates a seeded pseudo-random number generator using the Mulberry32 algorithm
|
|
3
|
+
*
|
|
4
|
+
* This is a simple, fast generator with good statistical properties.
|
|
5
|
+
* It can be used when deterministic random sequences are needed.
|
|
6
|
+
*
|
|
7
|
+
* @util
|
|
8
|
+
* @param seed - A 32-bit integer seed value to initialize the generator
|
|
9
|
+
* @returns A function that returns a pseudo-random number between 0 and 1 when called
|
|
10
|
+
* @see https://stackoverflow.com/a/47593316/2202583
|
|
11
|
+
*/
|
|
12
|
+
export declare function rngMulberry32(seed: number): () => number;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creates a seeded pseudo-random number generator using the Mulberry32 algorithm
|
|
3
|
+
*
|
|
4
|
+
* This is a simple, fast generator with good statistical properties.
|
|
5
|
+
* It can be used when deterministic random sequences are needed.
|
|
6
|
+
*
|
|
7
|
+
* @util
|
|
8
|
+
* @param seed - A 32-bit integer seed value to initialize the generator
|
|
9
|
+
* @returns A function that returns a pseudo-random number between 0 and 1 when called
|
|
10
|
+
* @see https://stackoverflow.com/a/47593316/2202583
|
|
11
|
+
*/
|
|
12
|
+
export function rngMulberry32(seed) {
|
|
13
|
+
seed = seed | 0; // turn into a 32-bit integer
|
|
14
|
+
return () => {
|
|
15
|
+
let t = (seed += 0x6d2b79f5);
|
|
16
|
+
t = Math.imul(t ^ (t >>> 15), t | 1);
|
|
17
|
+
t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
|
|
18
|
+
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
//# sourceMappingURL=rngMulberry32.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rngMulberry32.js","sourceRoot":"","sources":["../src/rngMulberry32.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AACH,MAAM,UAAU,aAAa,CAAC,IAAY;IACtC,IAAI,GAAG,IAAI,GAAG,CAAC,CAAC,CAAC,6BAA6B;IAC9C,OAAO,GAAG,EAAE;QACR,IAAI,CAAC,GAAG,CAAC,IAAI,IAAI,UAAU,CAAC,CAAC;QAC7B,CAAC,GAAG,IAAI,CAAC,IAAI,CAAC,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,CAAC,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC;QACrC,CAAC,IAAI,CAAC,GAAG,IAAI,CAAC,IAAI,CAAC,CAAC,GAAG,CAAC,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,GAAG,EAAE,CAAC,CAAC;QAC1C,OAAO,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,CAAC,CAAC,KAAK,CAAC,CAAC,GAAG,UAAU,CAAC;IACjD,CAAC,CAAC;AACN,CAAC"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Compares two strings using a timing-safe comparison method
|
|
3
|
+
*
|
|
4
|
+
* This function provides protection against timing attacks by ensuring
|
|
5
|
+
* the comparison takes the same amount of time regardless of where the
|
|
6
|
+
* difference occurs in the strings.
|
|
7
|
+
*
|
|
8
|
+
* @util
|
|
9
|
+
* @param a - First string to compare
|
|
10
|
+
* @param b - Second string to compare
|
|
11
|
+
* @returns true if strings are identical, false otherwise
|
|
12
|
+
*/
|
|
13
|
+
export declare function stringEqualTimingSafe(a: string, b: string): boolean;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { timingSafeEqual } from 'node:crypto';
|
|
2
|
+
/**
|
|
3
|
+
* Compares two strings using a timing-safe comparison method
|
|
4
|
+
*
|
|
5
|
+
* This function provides protection against timing attacks by ensuring
|
|
6
|
+
* the comparison takes the same amount of time regardless of where the
|
|
7
|
+
* difference occurs in the strings.
|
|
8
|
+
*
|
|
9
|
+
* @util
|
|
10
|
+
* @param a - First string to compare
|
|
11
|
+
* @param b - Second string to compare
|
|
12
|
+
* @returns true if strings are identical, false otherwise
|
|
13
|
+
*/
|
|
14
|
+
export function stringEqualTimingSafe(a, b) {
|
|
15
|
+
try {
|
|
16
|
+
return timingSafeEqual(Buffer.from(a, 'utf8'), Buffer.from(b, 'utf8'));
|
|
17
|
+
}
|
|
18
|
+
catch {
|
|
19
|
+
return false;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
//# sourceMappingURL=stringEqualsTimingSafe.node.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stringEqualsTimingSafe.node.js","sourceRoot":"","sources":["../src/stringEqualsTimingSafe.node.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAE9C;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,qBAAqB,CAAC,CAAS,EAAE,CAAS;IACtD,IAAI,CAAC;QACD,OAAO,eAAe,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,EAAE,MAAM,CAAC,EAAE,MAAM,CAAC,IAAI,CAAC,CAAC,EAAE,MAAM,CAAC,CAAC,CAAC;IAC3E,CAAC;IAAC,MAAM,CAAC;QACL,OAAO,KAAK,CAAC;IACjB,CAAC;AACL,CAAC"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Parameters for validating Basic Authentication credentials
|
|
3
|
+
*
|
|
4
|
+
* @property login - The expected username or login
|
|
5
|
+
* @property password - The expected password
|
|
6
|
+
* @property token - The Basic Auth token to validate (from Authorization header)
|
|
7
|
+
*/
|
|
8
|
+
type BasicAuthParams = {
|
|
9
|
+
login: string;
|
|
10
|
+
password: string;
|
|
11
|
+
token: string | null | undefined;
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* Validates a Basic Authentication token against expected credentials
|
|
15
|
+
*
|
|
16
|
+
* Compares the credentials extracted from the token with the expected login
|
|
17
|
+
* and password using timing-safe comparison to prevent timing attacks.
|
|
18
|
+
*
|
|
19
|
+
* @util
|
|
20
|
+
* @param params - Object containing expected credentials and token to validate
|
|
21
|
+
* @returns true if credentials match, false otherwise or if token is missing
|
|
22
|
+
*/
|
|
23
|
+
export declare function validateBasicAuth(params: BasicAuthParams): boolean;
|
|
24
|
+
export {};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { parseBasicAuth } from './parseBasicAuth.js';
|
|
2
|
+
import { stringEqualTimingSafe } from './stringEqualsTimingSafe.node.js';
|
|
3
|
+
/**
|
|
4
|
+
* Validates a Basic Authentication token against expected credentials
|
|
5
|
+
*
|
|
6
|
+
* Compares the credentials extracted from the token with the expected login
|
|
7
|
+
* and password using timing-safe comparison to prevent timing attacks.
|
|
8
|
+
*
|
|
9
|
+
* @util
|
|
10
|
+
* @param params - Object containing expected credentials and token to validate
|
|
11
|
+
* @returns true if credentials match, false otherwise or if token is missing
|
|
12
|
+
*/
|
|
13
|
+
export function validateBasicAuth(params) {
|
|
14
|
+
const token = params.token;
|
|
15
|
+
if (!token) {
|
|
16
|
+
return false;
|
|
17
|
+
}
|
|
18
|
+
const parsed = parseBasicAuth(token);
|
|
19
|
+
return stringEqualTimingSafe(parsed.login, params.login) && stringEqualTimingSafe(parsed.password, params.password);
|
|
20
|
+
}
|
|
21
|
+
//# sourceMappingURL=validateBasicAuth.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"validateBasicAuth.js","sourceRoot":"","sources":["../src/validateBasicAuth.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AACrD,OAAO,EAAE,qBAAqB,EAAE,MAAM,kCAAkC,CAAC;AAezE;;;;;;;;;GASG;AACH,MAAM,UAAU,iBAAiB,CAAC,MAAuB;IACrD,MAAM,KAAK,GAAG,MAAM,CAAC,KAAK,CAAC;IAC3B,IAAI,CAAC,KAAK,EAAE,CAAC;QACT,OAAO,KAAK,CAAC;IACjB,CAAC;IAED,MAAM,MAAM,GAAG,cAAc,CAAC,KAAK,CAAC,CAAC;IACrC,OAAO,qBAAqB,CAAC,MAAM,CAAC,KAAK,EAAE,MAAM,CAAC,KAAK,CAAC,IAAI,qBAAqB,CAAC,MAAM,CAAC,QAAQ,EAAE,MAAM,CAAC,QAAQ,CAAC,CAAC;AACxH,CAAC"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Parameters for validating a Bearer token
|
|
3
|
+
*
|
|
4
|
+
* @property tokenProvided - The Bearer token to validate (from Authorization header)
|
|
5
|
+
* @property tokenRequested - The expected token value to compare against
|
|
6
|
+
*/
|
|
7
|
+
type BearerTokenParams = {
|
|
8
|
+
tokenProvided: string | null | undefined;
|
|
9
|
+
tokenRequested: string;
|
|
10
|
+
};
|
|
11
|
+
/**
|
|
12
|
+
* Validates a Bearer token against an expected value
|
|
13
|
+
*
|
|
14
|
+
* Extracts the token value from the provided Bearer token string and compares it
|
|
15
|
+
* with the expected token using timing-safe comparison to prevent timing attacks.
|
|
16
|
+
*
|
|
17
|
+
* @util
|
|
18
|
+
* @param params - Object containing the provided token and expected token
|
|
19
|
+
* @returns true if tokens match, false otherwise or if no token is provided
|
|
20
|
+
*/
|
|
21
|
+
export declare function validateBearerToken(params: BearerTokenParams): boolean;
|
|
22
|
+
export {};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { parseBearerToken } from './parseBearerToken.js';
|
|
2
|
+
import { stringEqualTimingSafe } from './stringEqualsTimingSafe.node.js';
|
|
3
|
+
/**
|
|
4
|
+
* Validates a Bearer token against an expected value
|
|
5
|
+
*
|
|
6
|
+
* Extracts the token value from the provided Bearer token string and compares it
|
|
7
|
+
* with the expected token using timing-safe comparison to prevent timing attacks.
|
|
8
|
+
*
|
|
9
|
+
* @util
|
|
10
|
+
* @param params - Object containing the provided token and expected token
|
|
11
|
+
* @returns true if tokens match, false otherwise or if no token is provided
|
|
12
|
+
*/
|
|
13
|
+
export function validateBearerToken(params) {
|
|
14
|
+
let token = params.tokenProvided;
|
|
15
|
+
if (!token) {
|
|
16
|
+
return false;
|
|
17
|
+
}
|
|
18
|
+
token = parseBearerToken(token);
|
|
19
|
+
return stringEqualTimingSafe(token, params.tokenRequested);
|
|
20
|
+
}
|
|
21
|
+
//# sourceMappingURL=validateBearerToken.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"validateBearerToken.js","sourceRoot":"","sources":["../src/validateBearerToken.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AACzD,OAAO,EAAE,qBAAqB,EAAE,MAAM,kCAAkC,CAAC;AAazE;;;;;;;;;GASG;AACH,MAAM,UAAU,mBAAmB,CAAC,MAAyB;IACzD,IAAI,KAAK,GAAG,MAAM,CAAC,aAAa,CAAC;IACjC,IAAI,CAAC,KAAK,EAAE,CAAC;QACT,OAAO,KAAK,CAAC;IACjB,CAAC;IAED,KAAK,GAAG,gBAAgB,CAAC,KAAK,CAAC,CAAC;IAEhC,OAAO,qBAAqB,CAAC,KAAK,EAAE,MAAM,CAAC,cAAc,CAAC,CAAC;AAC/D,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,59 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
2
|
+
"name": "@nzyme/crypto",
|
|
3
|
+
"version": "0.15.0",
|
|
4
|
+
"description": "Cryptography helpers (hashing, UUID, encryption)",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"author": "Michał Kędrzyński <m.kedrzynski@gmail.com>",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "https://github.com/kedrzu/nzyme.git"
|
|
10
|
+
},
|
|
11
|
+
"files": [
|
|
12
|
+
"dist"
|
|
13
|
+
],
|
|
14
|
+
"type": "module",
|
|
15
|
+
"sideEffects": false,
|
|
16
|
+
"module": "./dist/index.node.js",
|
|
17
|
+
"types": "./dist/index.node.d.ts",
|
|
18
|
+
"exports": {
|
|
19
|
+
".": {
|
|
20
|
+
"node": {
|
|
21
|
+
"types": "./dist/index.node.d.ts",
|
|
22
|
+
"default": "./dist/index.node.js"
|
|
23
|
+
},
|
|
24
|
+
"default": {
|
|
25
|
+
"types": "./dist/index.browser.d.ts",
|
|
26
|
+
"default": "./dist/index.browser.js"
|
|
27
|
+
}
|
|
28
|
+
},
|
|
29
|
+
"./node": {
|
|
30
|
+
"types": "./dist/index.node.d.ts",
|
|
31
|
+
"default": "./dist/index.node.js"
|
|
32
|
+
},
|
|
33
|
+
"./browser": {
|
|
34
|
+
"types": "./dist/index.browser.d.ts",
|
|
35
|
+
"default": "./dist/index.browser.js"
|
|
36
|
+
}
|
|
37
|
+
},
|
|
38
|
+
"publishConfig": {
|
|
39
|
+
"access": "public"
|
|
40
|
+
},
|
|
41
|
+
"scripts": {
|
|
42
|
+
"build": "tsgo --build tsconfig.esm.json",
|
|
43
|
+
"format": "oxfmt ./src > /dev/null"
|
|
44
|
+
},
|
|
45
|
+
"dependencies": {
|
|
46
|
+
"@nzyme/types": "0.15.0",
|
|
47
|
+
"spark-md5": "3.0.2"
|
|
48
|
+
},
|
|
49
|
+
"devDependencies": {
|
|
50
|
+
"@nzyme/tsconfig": "0.15.0",
|
|
51
|
+
"@types/node": "26.6.4",
|
|
52
|
+
"@types/spark-md5": "3.0.5",
|
|
53
|
+
"@typescript/native-preview": "^7.0.0-dev.20260707.2",
|
|
54
|
+
"oxfmt": "0.71.0"
|
|
55
|
+
},
|
|
56
|
+
"nzyme": {
|
|
57
|
+
"cjs": true
|
|
58
|
+
}
|
|
59
|
+
}
|
package/README.md
DELETED