- Added BytesLike type for handling various byte representations. - Implemented LiteHashDRBG class for SHA-512 based deterministic random bit generator. - Created RNG interface defining methods for random number generation. - Developed RandUtil class providing utility functions for RNG operations. - Added conversion functions for BytesLike to ArrayBuffer and Uint8Array. - Implemented comprehensive tests for RNG functionality and expected behaviors. - Configured Vitest for testing environment setup.
18 lines
541 B
TypeScript
18 lines
541 B
TypeScript
import type { BytesLike } from './BytesLike';
|
|
|
|
export function convertBytesLikeToArrayBuffer(data: BytesLike, encodeUTF8 = true): ArrayBuffer {
|
|
if (data instanceof ArrayBuffer) {
|
|
return data;
|
|
}
|
|
if (data instanceof Uint8Array) {
|
|
return data.buffer;
|
|
}
|
|
if (typeof (data) === 'string') {
|
|
if (encodeUTF8) {
|
|
return (new TextEncoder()).encode(data).buffer;
|
|
}
|
|
return new Uint8Array(data.split('').map((s) => s.codePointAt(0) as number)).buffer;
|
|
}
|
|
return data.buffer;
|
|
}
|