@@ -3,7 +3,7 @@ import { FN_NATIVE, NULL, NUM } from '../../interpreter/value.js';
33import { textEncoder } from '../../const.js' ;
44import { SeedRandomWrapper } from './seedrandom.js' ;
55import { ChaCha20 } from './chacha20.js' ;
6- import type { VNativeFn , VNum , VStr } from '../../interpreter/value.js' ;
6+ import type { Value , VNativeFn , VNum , VStr } from '../../interpreter/value.js' ;
77
88export function GenerateLegacyRandom ( seed : VNum | VStr ) : VNativeFn {
99 const rng = seedrandom ( seed . value . toString ( ) ) ;
@@ -26,11 +26,17 @@ export function GenerateRC4Random(seed: VNum | VStr): VNativeFn {
2626 } ) ;
2727}
2828
29- export async function GenerateChaCha20Random ( seed : VNum | VStr ) : Promise < VNativeFn > {
29+ export async function GenerateChaCha20Random ( seed : VNum | VStr , options : Map < string , Value > | undefined ) : Promise < VNativeFn > {
3030 let actualSeed : Uint8Array ;
31- if ( seed . type === 'num' )
32- {
33- actualSeed = new Uint8Array ( await crypto . subtle . digest ( 'SHA-384' , new Uint8Array ( new Float64Array ( [ seed . value ] ) ) ) ) ;
31+ if ( seed . type === 'num' ) {
32+ let float64Array = new Float64Array ( [ seed . value ] ) ;
33+ let numberAsIntegerOptionValue = options ?. get ( "chacha20NumberSeedLegacyBehaviour" ) ;
34+ let numberAsInteger = false ;
35+ if ( numberAsIntegerOptionValue ?. type === "bool" ) {
36+ numberAsInteger = numberAsIntegerOptionValue . value ;
37+ }
38+ let seedToDigest = numberAsInteger ? new Uint8Array ( float64Array ) : new Uint8Array ( float64Array . buffer ) ;
39+ actualSeed = new Uint8Array ( await crypto . subtle . digest ( 'SHA-384' , seedToDigest ) ) ;
3440 } else {
3541 actualSeed = new Uint8Array ( await crypto . subtle . digest ( 'SHA-384' , new Uint8Array ( textEncoder . encode ( seed . value ) ) ) ) ;
3642 }
0 commit comments