@elaraai/east-web-std 1.0.85
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/CLA.md +26 -0
- package/CONTRIBUTING.md +28 -0
- package/LICENSE.md +31 -0
- package/README.md +191 -0
- package/THIRD_PARTY_NOTICES.md +123 -0
- package/dist/src/compliance.spec.d.ts +6 -0
- package/dist/src/compliance.spec.d.ts.map +1 -0
- package/dist/src/compliance.spec.js +89 -0
- package/dist/src/compliance.spec.js.map +1 -0
- package/dist/src/console.d.ts +237 -0
- package/dist/src/console.d.ts.map +1 -0
- package/dist/src/console.js +260 -0
- package/dist/src/console.js.map +1 -0
- package/dist/src/crypto.d.ts +224 -0
- package/dist/src/crypto.d.ts.map +1 -0
- package/dist/src/crypto.js +295 -0
- package/dist/src/crypto.js.map +1 -0
- package/dist/src/fetch.d.ts +383 -0
- package/dist/src/fetch.d.ts.map +1 -0
- package/dist/src/fetch.js +412 -0
- package/dist/src/fetch.js.map +1 -0
- package/dist/src/index.d.ts +106 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/index.js +93 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/path.d.ts +243 -0
- package/dist/src/path.d.ts.map +1 -0
- package/dist/src/path.js +562 -0
- package/dist/src/path.js.map +1 -0
- package/dist/src/random/crypto-rng.d.ts +38 -0
- package/dist/src/random/crypto-rng.d.ts.map +1 -0
- package/dist/src/random/crypto-rng.js +55 -0
- package/dist/src/random/crypto-rng.js.map +1 -0
- package/dist/src/random/distributions/bates.d.ts +17 -0
- package/dist/src/random/distributions/bates.d.ts.map +1 -0
- package/dist/src/random/distributions/bates.js +15 -0
- package/dist/src/random/distributions/bates.js.map +1 -0
- package/dist/src/random/distributions/bernoulli.d.ts +17 -0
- package/dist/src/random/distributions/bernoulli.d.ts.map +1 -0
- package/dist/src/random/distributions/bernoulli.js +13 -0
- package/dist/src/random/distributions/bernoulli.js.map +1 -0
- package/dist/src/random/distributions/binomial.d.ts +17 -0
- package/dist/src/random/distributions/binomial.d.ts.map +1 -0
- package/dist/src/random/distributions/binomial.js +24 -0
- package/dist/src/random/distributions/binomial.js.map +1 -0
- package/dist/src/random/distributions/exponential.d.ts +17 -0
- package/dist/src/random/distributions/exponential.d.ts.map +1 -0
- package/dist/src/random/distributions/exponential.js +13 -0
- package/dist/src/random/distributions/exponential.js.map +1 -0
- package/dist/src/random/distributions/geometric.d.ts +17 -0
- package/dist/src/random/distributions/geometric.d.ts.map +1 -0
- package/dist/src/random/distributions/geometric.js +14 -0
- package/dist/src/random/distributions/geometric.js.map +1 -0
- package/dist/src/random/distributions/irwin-hall.d.ts +17 -0
- package/dist/src/random/distributions/irwin-hall.d.ts.map +1 -0
- package/dist/src/random/distributions/irwin-hall.js +18 -0
- package/dist/src/random/distributions/irwin-hall.js.map +1 -0
- package/dist/src/random/distributions/log-normal.d.ts +17 -0
- package/dist/src/random/distributions/log-normal.d.ts.map +1 -0
- package/dist/src/random/distributions/log-normal.js +12 -0
- package/dist/src/random/distributions/log-normal.js.map +1 -0
- package/dist/src/random/distributions/normal.d.ts +18 -0
- package/dist/src/random/distributions/normal.d.ts.map +1 -0
- package/dist/src/random/distributions/normal.js +16 -0
- package/dist/src/random/distributions/normal.js.map +1 -0
- package/dist/src/random/distributions/pareto.d.ts +17 -0
- package/dist/src/random/distributions/pareto.d.ts.map +1 -0
- package/dist/src/random/distributions/pareto.js +14 -0
- package/dist/src/random/distributions/pareto.js.map +1 -0
- package/dist/src/random/distributions/poisson.d.ts +17 -0
- package/dist/src/random/distributions/poisson.d.ts.map +1 -0
- package/dist/src/random/distributions/poisson.js +81 -0
- package/dist/src/random/distributions/poisson.js.map +1 -0
- package/dist/src/random/distributions/uniform-int.d.ts +17 -0
- package/dist/src/random/distributions/uniform-int.d.ts.map +1 -0
- package/dist/src/random/distributions/uniform-int.js +16 -0
- package/dist/src/random/distributions/uniform-int.js.map +1 -0
- package/dist/src/random/distributions/uniform.d.ts +17 -0
- package/dist/src/random/distributions/uniform.d.ts.map +1 -0
- package/dist/src/random/distributions/uniform.js +10 -0
- package/dist/src/random/distributions/uniform.js.map +1 -0
- package/dist/src/random/rng.d.ts +21 -0
- package/dist/src/random/rng.d.ts.map +1 -0
- package/dist/src/random/rng.js +7 -0
- package/dist/src/random/rng.js.map +1 -0
- package/dist/src/random/xorshift128.d.ts +42 -0
- package/dist/src/random/xorshift128.d.ts.map +1 -0
- package/dist/src/random/xorshift128.js +119 -0
- package/dist/src/random/xorshift128.js.map +1 -0
- package/dist/src/random.d.ts +822 -0
- package/dist/src/random.d.ts.map +1 -0
- package/dist/src/random.js +992 -0
- package/dist/src/random.js.map +1 -0
- package/dist/src/test.d.ts +86 -0
- package/dist/src/test.d.ts.map +1 -0
- package/dist/src/test.js +160 -0
- package/dist/src/test.js.map +1 -0
- package/dist/src/time.d.ts +201 -0
- package/dist/src/time.d.ts.map +1 -0
- package/dist/src/time.js +300 -0
- package/dist/src/time.js.map +1 -0
- package/package.json +64 -0
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) 2025 Elara AI Pty Ltd
|
|
3
|
+
* Dual-licensed under AGPL-3.0 and commercial license. See LICENSE for details.
|
|
4
|
+
*/
|
|
5
|
+
import { StringType, IntegerType, BlobType } from "@elaraai/east";
|
|
6
|
+
import type { PlatformFunction } from "@elaraai/east/internal";
|
|
7
|
+
/**
|
|
8
|
+
* Generates cryptographically secure random bytes.
|
|
9
|
+
*
|
|
10
|
+
* Produces random bytes using a cryptographically secure pseudo-random number generator (CSPRNG).
|
|
11
|
+
* Suitable for generating encryption keys, initialization vectors, tokens, and other security-sensitive
|
|
12
|
+
* random data.
|
|
13
|
+
*
|
|
14
|
+
* This is a platform function for the East language, enabling secure random generation
|
|
15
|
+
* in East programs running in a browser, from the web-standard `crypto.getRandomValues`.
|
|
16
|
+
*
|
|
17
|
+
* @param length - The number of random bytes to generate (from 0 to 2^31 - 1)
|
|
18
|
+
* @returns Random bytes as a Blob (Uint8Array)
|
|
19
|
+
*
|
|
20
|
+
* @throws {EastError} When random generation fails:
|
|
21
|
+
* - Negative length, or more than 2^31 - 1 bytes
|
|
22
|
+
* - The host's entropy source is unavailable
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* ```ts
|
|
26
|
+
* const generateToken = East.function([], BlobType, $ => {
|
|
27
|
+
* return Crypto.randomBytes(32n); // 256-bit token
|
|
28
|
+
* });
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
export declare const crypto_random_bytes: import("@elaraai/east").PlatformDefinition<[IntegerType], BlobType>;
|
|
32
|
+
/**
|
|
33
|
+
* Computes SHA-256 hash of a string.
|
|
34
|
+
*
|
|
35
|
+
* Calculates the SHA-256 cryptographic hash of a UTF-8 encoded string and returns
|
|
36
|
+
* the result as a lowercase hexadecimal string (64 characters). SHA-256 is a one-way
|
|
37
|
+
* hash function commonly used for data integrity verification.
|
|
38
|
+
*
|
|
39
|
+
* This is a platform function for the East language, enabling cryptographic hashing
|
|
40
|
+
* in East programs running in a browser.
|
|
41
|
+
*
|
|
42
|
+
* @param data - The string to hash (will be UTF-8 encoded)
|
|
43
|
+
* @returns The SHA-256 hash as a lowercase hexadecimal string (64 characters)
|
|
44
|
+
*
|
|
45
|
+
* @example
|
|
46
|
+
* ```ts
|
|
47
|
+
* const hashPassword = East.function([StringType], StringType, ($, password) => {
|
|
48
|
+
* return Crypto.hashSha256(password);
|
|
49
|
+
* // Returns: "5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8"
|
|
50
|
+
* });
|
|
51
|
+
* ```
|
|
52
|
+
*
|
|
53
|
+
* @remarks
|
|
54
|
+
* The hash is East's own SHA-256 (`sha256Hex`), not the browser's
|
|
55
|
+
* `crypto.subtle.digest`: this platform function is synchronous, as it is on
|
|
56
|
+
* every runtime, and `crypto.subtle` answers only asynchronously.
|
|
57
|
+
*/
|
|
58
|
+
export declare const crypto_hash_sha256: import("@elaraai/east").PlatformDefinition<[StringType], StringType>;
|
|
59
|
+
/**
|
|
60
|
+
* Computes SHA-256 hash of binary data.
|
|
61
|
+
*
|
|
62
|
+
* Calculates the SHA-256 cryptographic hash of binary data and returns the result
|
|
63
|
+
* as raw bytes (32 bytes / 256 bits). This is the binary variant of {@link crypto_hash_sha256},
|
|
64
|
+
* useful when working with binary protocols or when you need the raw hash bytes.
|
|
65
|
+
*
|
|
66
|
+
* This is a platform function for the East language, enabling cryptographic hashing
|
|
67
|
+
* in East programs running in a browser.
|
|
68
|
+
*
|
|
69
|
+
* @param data - The binary data to hash (Blob/Uint8Array)
|
|
70
|
+
* @returns The SHA-256 hash as binary data (32 bytes)
|
|
71
|
+
*
|
|
72
|
+
* @example
|
|
73
|
+
* ```ts
|
|
74
|
+
* const hashFile = East.function([BlobType], BlobType, ($, fileData) => {
|
|
75
|
+
* return Crypto.hashSha256Bytes(fileData);
|
|
76
|
+
* // Returns: 32 bytes of hash data
|
|
77
|
+
* });
|
|
78
|
+
* ```
|
|
79
|
+
*
|
|
80
|
+
* @remarks
|
|
81
|
+
* Synchronous, like {@link crypto_hash_sha256}, for the same reason.
|
|
82
|
+
*/
|
|
83
|
+
export declare const crypto_hash_sha256_bytes: import("@elaraai/east").PlatformDefinition<[BlobType], BlobType>;
|
|
84
|
+
/**
|
|
85
|
+
* Generates a random UUID v4.
|
|
86
|
+
*
|
|
87
|
+
* Creates a version 4 UUID (Universally Unique Identifier) using cryptographically
|
|
88
|
+
* secure random numbers. UUIDs are 128-bit identifiers formatted as 36-character
|
|
89
|
+
* strings (e.g., "550e8400-e29b-41d4-a716-446655440000").
|
|
90
|
+
*
|
|
91
|
+
* This is a platform function for the East language, enabling UUID generation
|
|
92
|
+
* in East programs running in a browser, from the web-standard `crypto.randomUUID`.
|
|
93
|
+
*
|
|
94
|
+
* @returns A UUID v4 string in standard format (8-4-4-4-12 hex digits)
|
|
95
|
+
*
|
|
96
|
+
* @example
|
|
97
|
+
* ```ts
|
|
98
|
+
* const createRecord = East.function([], StringType, $ => {
|
|
99
|
+
* const id = $.let(Crypto.uuid());
|
|
100
|
+
* return id;
|
|
101
|
+
* // Returns: "550e8400-e29b-41d4-a716-446655440000"
|
|
102
|
+
* });
|
|
103
|
+
* ```
|
|
104
|
+
*
|
|
105
|
+
* @remarks
|
|
106
|
+
* A browser gives `crypto.randomUUID` only to a secure context (a page served
|
|
107
|
+
* over HTTPS, or from localhost); elsewhere the call fails with an `EastError`.
|
|
108
|
+
*/
|
|
109
|
+
export declare const crypto_uuid: import("@elaraai/east").PlatformDefinition<[], StringType>;
|
|
110
|
+
/**
|
|
111
|
+
* Browser implementation of cryptographic platform functions.
|
|
112
|
+
*
|
|
113
|
+
* Pass this array to {@link East.compile} to enable crypto operations.
|
|
114
|
+
*/
|
|
115
|
+
declare const CryptoImpl: PlatformFunction[];
|
|
116
|
+
/**
|
|
117
|
+
* Grouped cryptographic platform functions.
|
|
118
|
+
*
|
|
119
|
+
* Provides cryptographic operations for East programs.
|
|
120
|
+
*
|
|
121
|
+
* @example
|
|
122
|
+
* ```ts
|
|
123
|
+
* import { East, StringType } from "@elaraai/east";
|
|
124
|
+
* import { Crypto } from "@elaraai/east-web-std";
|
|
125
|
+
*
|
|
126
|
+
* const generateId = East.function([], StringType, $ => {
|
|
127
|
+
* return Crypto.uuid();
|
|
128
|
+
* });
|
|
129
|
+
*
|
|
130
|
+
* const compiled = East.compile(generateId, Crypto.Implementation);
|
|
131
|
+
* compiled(); // "550e8400-e29b-41d4-a716-446655440000"
|
|
132
|
+
* ```
|
|
133
|
+
*/
|
|
134
|
+
export declare const Crypto: {
|
|
135
|
+
/**
|
|
136
|
+
* Generates cryptographically secure random bytes.
|
|
137
|
+
*
|
|
138
|
+
* Produces random bytes using a CSPRNG, suitable for generating encryption keys,
|
|
139
|
+
* tokens, and other security-sensitive random data.
|
|
140
|
+
*
|
|
141
|
+
* @param length - The number of random bytes to generate (from 0 to 2^31 - 1)
|
|
142
|
+
* @returns Random bytes as a Blob (Uint8Array)
|
|
143
|
+
* @throws {EastError} When random generation fails
|
|
144
|
+
*
|
|
145
|
+
* @example
|
|
146
|
+
* ```ts
|
|
147
|
+
* const generateToken = East.function([], BlobType, $ => {
|
|
148
|
+
* return Crypto.randomBytes(32n);
|
|
149
|
+
* });
|
|
150
|
+
*
|
|
151
|
+
* const compiled = East.compile(generateToken, Crypto.Implementation);
|
|
152
|
+
* compiled(); // Uint8Array(32) [...]
|
|
153
|
+
* ```
|
|
154
|
+
*/
|
|
155
|
+
readonly randomBytes: import("@elaraai/east").PlatformDefinition<[IntegerType], BlobType>;
|
|
156
|
+
/**
|
|
157
|
+
* Computes SHA-256 hash of a string.
|
|
158
|
+
*
|
|
159
|
+
* Calculates the SHA-256 cryptographic hash of a UTF-8 encoded string and returns
|
|
160
|
+
* the result as a lowercase hexadecimal string (64 characters).
|
|
161
|
+
*
|
|
162
|
+
* @param data - The string to hash (will be UTF-8 encoded)
|
|
163
|
+
* @returns The SHA-256 hash as a lowercase hexadecimal string (64 characters)
|
|
164
|
+
*
|
|
165
|
+
* @example
|
|
166
|
+
* ```ts
|
|
167
|
+
* const hashPassword = East.function([StringType], StringType, ($, password) => {
|
|
168
|
+
* return Crypto.hashSha256(password);
|
|
169
|
+
* });
|
|
170
|
+
*
|
|
171
|
+
* const compiled = East.compile(hashPassword, Crypto.Implementation);
|
|
172
|
+
* compiled("password"); // "5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8"
|
|
173
|
+
* ```
|
|
174
|
+
*/
|
|
175
|
+
readonly hashSha256: import("@elaraai/east").PlatformDefinition<[StringType], StringType>;
|
|
176
|
+
/**
|
|
177
|
+
* Computes SHA-256 hash of binary data.
|
|
178
|
+
*
|
|
179
|
+
* Calculates the SHA-256 cryptographic hash of binary data and returns the result
|
|
180
|
+
* as raw bytes (32 bytes).
|
|
181
|
+
*
|
|
182
|
+
* @param data - The binary data to hash (Blob/Uint8Array)
|
|
183
|
+
* @returns The SHA-256 hash as binary data (32 bytes)
|
|
184
|
+
*
|
|
185
|
+
* @example
|
|
186
|
+
* ```ts
|
|
187
|
+
* const hashFile = East.function([BlobType], BlobType, ($, fileData) => {
|
|
188
|
+
* return Crypto.hashSha256Bytes(fileData);
|
|
189
|
+
* });
|
|
190
|
+
*
|
|
191
|
+
* const compiled = East.compile(hashFile, Crypto.Implementation);
|
|
192
|
+
* const fileData = new Uint8Array([1, 2, 3]);
|
|
193
|
+
* compiled(fileData); // Uint8Array(32) [...]
|
|
194
|
+
* ```
|
|
195
|
+
*/
|
|
196
|
+
readonly hashSha256Bytes: import("@elaraai/east").PlatformDefinition<[BlobType], BlobType>;
|
|
197
|
+
/**
|
|
198
|
+
* Generates a random UUID v4.
|
|
199
|
+
*
|
|
200
|
+
* Creates a version 4 UUID using cryptographically secure random numbers.
|
|
201
|
+
* Returns a 36-character string in standard format.
|
|
202
|
+
*
|
|
203
|
+
* @returns A UUID v4 string in standard format (8-4-4-4-12 hex digits)
|
|
204
|
+
*
|
|
205
|
+
* @example
|
|
206
|
+
* ```ts
|
|
207
|
+
* const createRecord = East.function([], StringType, $ => {
|
|
208
|
+
* return Crypto.uuid();
|
|
209
|
+
* });
|
|
210
|
+
*
|
|
211
|
+
* const compiled = East.compile(createRecord, Crypto.Implementation);
|
|
212
|
+
* compiled(); // "550e8400-e29b-41d4-a716-446655440000"
|
|
213
|
+
* ```
|
|
214
|
+
*/
|
|
215
|
+
readonly uuid: import("@elaraai/east").PlatformDefinition<[], StringType>;
|
|
216
|
+
/**
|
|
217
|
+
* Browser implementation of cryptographic platform functions.
|
|
218
|
+
*
|
|
219
|
+
* Pass this to {@link East.compile} to enable crypto operations.
|
|
220
|
+
*/
|
|
221
|
+
readonly Implementation: PlatformFunction[];
|
|
222
|
+
};
|
|
223
|
+
export { CryptoImpl };
|
|
224
|
+
//# sourceMappingURL=crypto.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"crypto.d.ts","sourceRoot":"","sources":["../../src/crypto.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,OAAO,EAAQ,UAAU,EAAE,WAAW,EAAE,QAAQ,EAAa,MAAM,eAAe,CAAC;AACnF,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAC;AAyB/D;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,eAAO,MAAM,mBAAmB,qEAAgE,CAAC;AAEjG;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,eAAO,MAAM,kBAAkB,sEAAgE,CAAC;AAEhG;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,eAAO,MAAM,wBAAwB,kEAAkE,CAAC;AAExG;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,eAAO,MAAM,WAAW,4DAA+C,CAAC;AAExE;;;;GAIG;AACH,QAAA,MAAM,UAAU,EAAE,gBAAgB,EAgDjC,CAAC;AAEF;;;;;;;;;;;;;;;;;GAiBG;AACH,eAAO,MAAM,MAAM;IACf;;;;;;;;;;;;;;;;;;;OAmBG;;IAGH;;;;;;;;;;;;;;;;;;OAkBG;;IAGH;;;;;;;;;;;;;;;;;;;OAmBG;;IAGH;;;;;;;;;;;;;;;;;OAiBG;;IAGH;;;;OAIG;;CAEG,CAAC;AAGX,OAAO,EAAE,UAAU,EAAE,CAAC"}
|
|
@@ -0,0 +1,295 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) 2025 Elara AI Pty Ltd
|
|
3
|
+
* Dual-licensed under AGPL-3.0 and commercial license. See LICENSE for details.
|
|
4
|
+
*/
|
|
5
|
+
import { East, StringType, IntegerType, BlobType, sha256Hex } from "@elaraai/east";
|
|
6
|
+
import { EastError } from "@elaraai/east/internal";
|
|
7
|
+
/** The most bytes `crypto.getRandomValues` fills in one call. */
|
|
8
|
+
const GET_RANDOM_VALUES_MAX = 65536;
|
|
9
|
+
/** The most bytes one call may ask for: east-node-std's `randomBytes` gives
|
|
10
|
+
* no more than 2^31 - 1. */
|
|
11
|
+
const RANDOM_BYTES_MAX = 2147483647n;
|
|
12
|
+
/** Encodes a string as UTF-8, as east-node-std hashes it. */
|
|
13
|
+
const utf8 = new TextEncoder();
|
|
14
|
+
/**
|
|
15
|
+
* The bytes of a SHA-256 digest, from the 64 lowercase hex digits
|
|
16
|
+
* {@link sha256Hex} gives for it.
|
|
17
|
+
*/
|
|
18
|
+
function digestBytes(hex) {
|
|
19
|
+
const bytes = new Uint8Array(hex.length / 2);
|
|
20
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
21
|
+
bytes[i] = parseInt(hex.slice(2 * i, 2 * i + 2), 16);
|
|
22
|
+
}
|
|
23
|
+
return bytes;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Generates cryptographically secure random bytes.
|
|
27
|
+
*
|
|
28
|
+
* Produces random bytes using a cryptographically secure pseudo-random number generator (CSPRNG).
|
|
29
|
+
* Suitable for generating encryption keys, initialization vectors, tokens, and other security-sensitive
|
|
30
|
+
* random data.
|
|
31
|
+
*
|
|
32
|
+
* This is a platform function for the East language, enabling secure random generation
|
|
33
|
+
* in East programs running in a browser, from the web-standard `crypto.getRandomValues`.
|
|
34
|
+
*
|
|
35
|
+
* @param length - The number of random bytes to generate (from 0 to 2^31 - 1)
|
|
36
|
+
* @returns Random bytes as a Blob (Uint8Array)
|
|
37
|
+
*
|
|
38
|
+
* @throws {EastError} When random generation fails:
|
|
39
|
+
* - Negative length, or more than 2^31 - 1 bytes
|
|
40
|
+
* - The host's entropy source is unavailable
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* ```ts
|
|
44
|
+
* const generateToken = East.function([], BlobType, $ => {
|
|
45
|
+
* return Crypto.randomBytes(32n); // 256-bit token
|
|
46
|
+
* });
|
|
47
|
+
* ```
|
|
48
|
+
*/
|
|
49
|
+
export const crypto_random_bytes = East.platform("crypto_random_bytes", [IntegerType], BlobType);
|
|
50
|
+
/**
|
|
51
|
+
* Computes SHA-256 hash of a string.
|
|
52
|
+
*
|
|
53
|
+
* Calculates the SHA-256 cryptographic hash of a UTF-8 encoded string and returns
|
|
54
|
+
* the result as a lowercase hexadecimal string (64 characters). SHA-256 is a one-way
|
|
55
|
+
* hash function commonly used for data integrity verification.
|
|
56
|
+
*
|
|
57
|
+
* This is a platform function for the East language, enabling cryptographic hashing
|
|
58
|
+
* in East programs running in a browser.
|
|
59
|
+
*
|
|
60
|
+
* @param data - The string to hash (will be UTF-8 encoded)
|
|
61
|
+
* @returns The SHA-256 hash as a lowercase hexadecimal string (64 characters)
|
|
62
|
+
*
|
|
63
|
+
* @example
|
|
64
|
+
* ```ts
|
|
65
|
+
* const hashPassword = East.function([StringType], StringType, ($, password) => {
|
|
66
|
+
* return Crypto.hashSha256(password);
|
|
67
|
+
* // Returns: "5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8"
|
|
68
|
+
* });
|
|
69
|
+
* ```
|
|
70
|
+
*
|
|
71
|
+
* @remarks
|
|
72
|
+
* The hash is East's own SHA-256 (`sha256Hex`), not the browser's
|
|
73
|
+
* `crypto.subtle.digest`: this platform function is synchronous, as it is on
|
|
74
|
+
* every runtime, and `crypto.subtle` answers only asynchronously.
|
|
75
|
+
*/
|
|
76
|
+
export const crypto_hash_sha256 = East.platform("crypto_hash_sha256", [StringType], StringType);
|
|
77
|
+
/**
|
|
78
|
+
* Computes SHA-256 hash of binary data.
|
|
79
|
+
*
|
|
80
|
+
* Calculates the SHA-256 cryptographic hash of binary data and returns the result
|
|
81
|
+
* as raw bytes (32 bytes / 256 bits). This is the binary variant of {@link crypto_hash_sha256},
|
|
82
|
+
* useful when working with binary protocols or when you need the raw hash bytes.
|
|
83
|
+
*
|
|
84
|
+
* This is a platform function for the East language, enabling cryptographic hashing
|
|
85
|
+
* in East programs running in a browser.
|
|
86
|
+
*
|
|
87
|
+
* @param data - The binary data to hash (Blob/Uint8Array)
|
|
88
|
+
* @returns The SHA-256 hash as binary data (32 bytes)
|
|
89
|
+
*
|
|
90
|
+
* @example
|
|
91
|
+
* ```ts
|
|
92
|
+
* const hashFile = East.function([BlobType], BlobType, ($, fileData) => {
|
|
93
|
+
* return Crypto.hashSha256Bytes(fileData);
|
|
94
|
+
* // Returns: 32 bytes of hash data
|
|
95
|
+
* });
|
|
96
|
+
* ```
|
|
97
|
+
*
|
|
98
|
+
* @remarks
|
|
99
|
+
* Synchronous, like {@link crypto_hash_sha256}, for the same reason.
|
|
100
|
+
*/
|
|
101
|
+
export const crypto_hash_sha256_bytes = East.platform("crypto_hash_sha256_bytes", [BlobType], BlobType);
|
|
102
|
+
/**
|
|
103
|
+
* Generates a random UUID v4.
|
|
104
|
+
*
|
|
105
|
+
* Creates a version 4 UUID (Universally Unique Identifier) using cryptographically
|
|
106
|
+
* secure random numbers. UUIDs are 128-bit identifiers formatted as 36-character
|
|
107
|
+
* strings (e.g., "550e8400-e29b-41d4-a716-446655440000").
|
|
108
|
+
*
|
|
109
|
+
* This is a platform function for the East language, enabling UUID generation
|
|
110
|
+
* in East programs running in a browser, from the web-standard `crypto.randomUUID`.
|
|
111
|
+
*
|
|
112
|
+
* @returns A UUID v4 string in standard format (8-4-4-4-12 hex digits)
|
|
113
|
+
*
|
|
114
|
+
* @example
|
|
115
|
+
* ```ts
|
|
116
|
+
* const createRecord = East.function([], StringType, $ => {
|
|
117
|
+
* const id = $.let(Crypto.uuid());
|
|
118
|
+
* return id;
|
|
119
|
+
* // Returns: "550e8400-e29b-41d4-a716-446655440000"
|
|
120
|
+
* });
|
|
121
|
+
* ```
|
|
122
|
+
*
|
|
123
|
+
* @remarks
|
|
124
|
+
* A browser gives `crypto.randomUUID` only to a secure context (a page served
|
|
125
|
+
* over HTTPS, or from localhost); elsewhere the call fails with an `EastError`.
|
|
126
|
+
*/
|
|
127
|
+
export const crypto_uuid = East.platform("crypto_uuid", [], StringType);
|
|
128
|
+
/**
|
|
129
|
+
* Browser implementation of cryptographic platform functions.
|
|
130
|
+
*
|
|
131
|
+
* Pass this array to {@link East.compile} to enable crypto operations.
|
|
132
|
+
*/
|
|
133
|
+
const CryptoImpl = [
|
|
134
|
+
crypto_random_bytes.implement((length) => {
|
|
135
|
+
try {
|
|
136
|
+
if (length < 0n || length > RANDOM_BYTES_MAX) {
|
|
137
|
+
throw new RangeError(`the length must be from 0 to ${RANDOM_BYTES_MAX}, got ${length}`);
|
|
138
|
+
}
|
|
139
|
+
const bytes = new Uint8Array(Number(length));
|
|
140
|
+
for (let offset = 0; offset < bytes.length; offset += GET_RANDOM_VALUES_MAX) {
|
|
141
|
+
globalThis.crypto.getRandomValues(bytes.subarray(offset, offset + GET_RANDOM_VALUES_MAX));
|
|
142
|
+
}
|
|
143
|
+
return bytes;
|
|
144
|
+
}
|
|
145
|
+
catch (err) {
|
|
146
|
+
throw new EastError(`Failed to generate random bytes: ${err.message}`, {
|
|
147
|
+
location: [{ filename: "crypto_random_bytes", line: 0n, column: 0n }],
|
|
148
|
+
cause: err
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
}),
|
|
152
|
+
crypto_hash_sha256.implement((data) => {
|
|
153
|
+
try {
|
|
154
|
+
return sha256Hex(utf8.encode(data));
|
|
155
|
+
}
|
|
156
|
+
catch (err) {
|
|
157
|
+
throw new EastError(`Failed to compute SHA-256 hash: ${err.message}`, {
|
|
158
|
+
location: [{ filename: "crypto_hash_sha256", line: 0n, column: 0n }],
|
|
159
|
+
cause: err
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
}),
|
|
163
|
+
crypto_hash_sha256_bytes.implement((data) => {
|
|
164
|
+
try {
|
|
165
|
+
return digestBytes(sha256Hex(data));
|
|
166
|
+
}
|
|
167
|
+
catch (err) {
|
|
168
|
+
throw new EastError(`Failed to compute SHA-256 hash: ${err.message}`, {
|
|
169
|
+
location: [{ filename: "crypto_hash_sha256_bytes", line: 0n, column: 0n }],
|
|
170
|
+
cause: err
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
}),
|
|
174
|
+
crypto_uuid.implement(() => {
|
|
175
|
+
try {
|
|
176
|
+
return globalThis.crypto.randomUUID();
|
|
177
|
+
}
|
|
178
|
+
catch (err) {
|
|
179
|
+
throw new EastError(`Failed to generate UUID: ${err.message}`, {
|
|
180
|
+
location: [{ filename: "crypto_uuid", line: 0n, column: 0n }],
|
|
181
|
+
cause: err
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
}),
|
|
185
|
+
];
|
|
186
|
+
/**
|
|
187
|
+
* Grouped cryptographic platform functions.
|
|
188
|
+
*
|
|
189
|
+
* Provides cryptographic operations for East programs.
|
|
190
|
+
*
|
|
191
|
+
* @example
|
|
192
|
+
* ```ts
|
|
193
|
+
* import { East, StringType } from "@elaraai/east";
|
|
194
|
+
* import { Crypto } from "@elaraai/east-web-std";
|
|
195
|
+
*
|
|
196
|
+
* const generateId = East.function([], StringType, $ => {
|
|
197
|
+
* return Crypto.uuid();
|
|
198
|
+
* });
|
|
199
|
+
*
|
|
200
|
+
* const compiled = East.compile(generateId, Crypto.Implementation);
|
|
201
|
+
* compiled(); // "550e8400-e29b-41d4-a716-446655440000"
|
|
202
|
+
* ```
|
|
203
|
+
*/
|
|
204
|
+
export const Crypto = {
|
|
205
|
+
/**
|
|
206
|
+
* Generates cryptographically secure random bytes.
|
|
207
|
+
*
|
|
208
|
+
* Produces random bytes using a CSPRNG, suitable for generating encryption keys,
|
|
209
|
+
* tokens, and other security-sensitive random data.
|
|
210
|
+
*
|
|
211
|
+
* @param length - The number of random bytes to generate (from 0 to 2^31 - 1)
|
|
212
|
+
* @returns Random bytes as a Blob (Uint8Array)
|
|
213
|
+
* @throws {EastError} When random generation fails
|
|
214
|
+
*
|
|
215
|
+
* @example
|
|
216
|
+
* ```ts
|
|
217
|
+
* const generateToken = East.function([], BlobType, $ => {
|
|
218
|
+
* return Crypto.randomBytes(32n);
|
|
219
|
+
* });
|
|
220
|
+
*
|
|
221
|
+
* const compiled = East.compile(generateToken, Crypto.Implementation);
|
|
222
|
+
* compiled(); // Uint8Array(32) [...]
|
|
223
|
+
* ```
|
|
224
|
+
*/
|
|
225
|
+
randomBytes: crypto_random_bytes,
|
|
226
|
+
/**
|
|
227
|
+
* Computes SHA-256 hash of a string.
|
|
228
|
+
*
|
|
229
|
+
* Calculates the SHA-256 cryptographic hash of a UTF-8 encoded string and returns
|
|
230
|
+
* the result as a lowercase hexadecimal string (64 characters).
|
|
231
|
+
*
|
|
232
|
+
* @param data - The string to hash (will be UTF-8 encoded)
|
|
233
|
+
* @returns The SHA-256 hash as a lowercase hexadecimal string (64 characters)
|
|
234
|
+
*
|
|
235
|
+
* @example
|
|
236
|
+
* ```ts
|
|
237
|
+
* const hashPassword = East.function([StringType], StringType, ($, password) => {
|
|
238
|
+
* return Crypto.hashSha256(password);
|
|
239
|
+
* });
|
|
240
|
+
*
|
|
241
|
+
* const compiled = East.compile(hashPassword, Crypto.Implementation);
|
|
242
|
+
* compiled("password"); // "5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8"
|
|
243
|
+
* ```
|
|
244
|
+
*/
|
|
245
|
+
hashSha256: crypto_hash_sha256,
|
|
246
|
+
/**
|
|
247
|
+
* Computes SHA-256 hash of binary data.
|
|
248
|
+
*
|
|
249
|
+
* Calculates the SHA-256 cryptographic hash of binary data and returns the result
|
|
250
|
+
* as raw bytes (32 bytes).
|
|
251
|
+
*
|
|
252
|
+
* @param data - The binary data to hash (Blob/Uint8Array)
|
|
253
|
+
* @returns The SHA-256 hash as binary data (32 bytes)
|
|
254
|
+
*
|
|
255
|
+
* @example
|
|
256
|
+
* ```ts
|
|
257
|
+
* const hashFile = East.function([BlobType], BlobType, ($, fileData) => {
|
|
258
|
+
* return Crypto.hashSha256Bytes(fileData);
|
|
259
|
+
* });
|
|
260
|
+
*
|
|
261
|
+
* const compiled = East.compile(hashFile, Crypto.Implementation);
|
|
262
|
+
* const fileData = new Uint8Array([1, 2, 3]);
|
|
263
|
+
* compiled(fileData); // Uint8Array(32) [...]
|
|
264
|
+
* ```
|
|
265
|
+
*/
|
|
266
|
+
hashSha256Bytes: crypto_hash_sha256_bytes,
|
|
267
|
+
/**
|
|
268
|
+
* Generates a random UUID v4.
|
|
269
|
+
*
|
|
270
|
+
* Creates a version 4 UUID using cryptographically secure random numbers.
|
|
271
|
+
* Returns a 36-character string in standard format.
|
|
272
|
+
*
|
|
273
|
+
* @returns A UUID v4 string in standard format (8-4-4-4-12 hex digits)
|
|
274
|
+
*
|
|
275
|
+
* @example
|
|
276
|
+
* ```ts
|
|
277
|
+
* const createRecord = East.function([], StringType, $ => {
|
|
278
|
+
* return Crypto.uuid();
|
|
279
|
+
* });
|
|
280
|
+
*
|
|
281
|
+
* const compiled = East.compile(createRecord, Crypto.Implementation);
|
|
282
|
+
* compiled(); // "550e8400-e29b-41d4-a716-446655440000"
|
|
283
|
+
* ```
|
|
284
|
+
*/
|
|
285
|
+
uuid: crypto_uuid,
|
|
286
|
+
/**
|
|
287
|
+
* Browser implementation of cryptographic platform functions.
|
|
288
|
+
*
|
|
289
|
+
* Pass this to {@link East.compile} to enable crypto operations.
|
|
290
|
+
*/
|
|
291
|
+
Implementation: CryptoImpl,
|
|
292
|
+
};
|
|
293
|
+
// Exported beside `Crypto.Implementation`, as east-node-std exports it
|
|
294
|
+
export { CryptoImpl };
|
|
295
|
+
//# sourceMappingURL=crypto.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"crypto.js","sourceRoot":"","sources":["../../src/crypto.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,OAAO,EAAE,IAAI,EAAE,UAAU,EAAE,WAAW,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,eAAe,CAAC;AAEnF,OAAO,EAAE,SAAS,EAAE,MAAM,wBAAwB,CAAC;AAEnD,iEAAiE;AACjE,MAAM,qBAAqB,GAAG,KAAK,CAAC;AAEpC;6BAC6B;AAC7B,MAAM,gBAAgB,GAAG,WAAW,CAAC;AAErC,6DAA6D;AAC7D,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