forked from aiscript-dev/aiscript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenrng.ts
More file actions
51 lines (48 loc) 路 2.15 KB
/
Copy pathgenrng.ts
File metadata and controls
51 lines (48 loc) 路 2.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import seedrandom from 'seedrandom';
import { FN_NATIVE, NULL, NUM } from '../../interpreter/value.js';
import { textEncoder } from '../../const.js';
import { SeedRandomWrapper } from './seedrandom.js';
import { ChaCha20 } from './chacha20.js';
import type { Value, VNativeFn, VNum, VStr } from '../../interpreter/value.js';
export function GenerateLegacyRandom(seed: VNum | VStr): VNativeFn {
const rng = seedrandom(seed.value.toString());
return FN_NATIVE(([min, max]) => {
if (min && min.type === 'num' && max && max.type === 'num') {
return NUM(Math.floor(rng() * (Math.floor(max.value) - Math.ceil(min.value) + 1) + Math.ceil(min.value)));
}
return NUM(rng());
});
}
export function GenerateRC4Random(seed: VNum | VStr): VNativeFn {
const rng = new SeedRandomWrapper(seed.value);
return FN_NATIVE(([min, max]) => {
if (min && min.type === 'num' && max && max.type === 'num') {
const result = rng.generateRandomIntegerInRange(min.value, max.value);
return typeof result === 'number' ? NUM(result) : NULL;
}
return NUM(rng.generateNumber0To1());
});
}
export async function GenerateChaCha20Random(seed: VNum | VStr, options: Map<string, Value> | undefined): Promise<VNativeFn> {
let actualSeed: Uint8Array;
if (seed.type === 'num') {
const float64Array = new Float64Array([seed.value]);
const numberAsIntegerOptionValue = options?.get('chacha20_number_seed_legacy_behavior');
let numberAsInteger = false;
if (numberAsIntegerOptionValue?.type === 'bool') {
numberAsInteger = numberAsIntegerOptionValue.value;
}
const seedToDigest = numberAsInteger ? new Uint8Array(float64Array) : new Uint8Array(float64Array.buffer);
actualSeed = new Uint8Array(await crypto.subtle.digest('SHA-384', seedToDigest));
} else {
actualSeed = new Uint8Array(await crypto.subtle.digest('SHA-384', new Uint8Array(textEncoder.encode(seed.value))));
}
const rng = new ChaCha20(actualSeed);
return FN_NATIVE(([min, max]) => {
if (min && min.type === 'num' && max && max.type === 'num') {
const result = rng.generateRandomIntegerInRange(min.value, max.value);
return typeof result === 'number' ? NUM(result) : NULL;
}
return NUM(rng.generateNumber0To1());
});
}