Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ============================================================
- // Hash_DRBG-inspired PRNG using llSHA256String() + Base64
- // Demonstration only
- // ============================================================
- // PRNG state
- string prng_internal_state = "";
- integer prng_reseed_counter = 0; // Ensures unique blocks per call
- integer PRNG_RESEED_INTERVAL = 10; // Max blocks before automatic reseed
- // ============================================================
- // Seed or reseed PRNG
- //
- // Seeds or refreshes the PRNG using provided entropy material.
- // Resets the counter to ensure new blocks are unique.
- // ============================================================
- llSeedRandomBase64(string entropy_material)
- {
- // Incorporate provided entropy into PRNG state
- // Can be called for initial seeding or later reseeding
- prng_internal_state = llSHA256String(prng_internal_state + entropy_material);
- prng_reseed_counter = 1;
- }
- // ============================================================
- // Generate number_of_bits random bits as Base64
- //
- // Produces a Base64 string representing the requested number of bits.
- // Uses Hash_DRBG-style counter for unique blocks and automatic reseeding.
- // ============================================================
- string llRandomBase64(integer number_of_bits)
- {
- // Hex string to accumulate SHA256 outputs
- string hex_output = "";
- // Generate enough hex characters to cover requested bits
- while (llStringLength(hex_output) * 4 < number_of_bits)
- {
- // Automatic reseed if counter exceeds threshold
- if (prng_reseed_counter >= PRNG_RESEED_INTERVAL)
- {
- // Use local source of entropy for demonstration
- // In a real system, stronger entropy would be preferable
- string entropy = (string)llFrand(1000000.0);
- llSeedRandomBase64(entropy);
- }
- // Include counter to ensure unique SHA256 block
- string hash_block = llSHA256String(prng_internal_state + (string)prng_reseed_counter);
- prng_internal_state = hash_block;
- prng_reseed_counter += 1;
- hex_output += hash_block;
- }
- // Append extra zeros to ensure last 6-character chunk is complete
- // Necessary to properly left-align the last random bits in their 24-bit chunk
- hex_output += "00000";
- // Convert hex string to Base64 in 6-hex-character (24-bit) chunks
- string base64_output = "";
- integer i;
- for (i = 0; i <= llStringLength(hex_output) - 6; i += 6)
- {
- string hex_chunk = llGetSubString(hex_output, i, i + 5);
- integer chunk24 = (integer)("0x" + hex_chunk);
- integer aligned32 = chunk24 << 8; // Left-align 24 bits for llIntegerToBase64
- string b64 = llGetSubString(llIntegerToBase64(aligned32), 0, 3);
- base64_output += b64;
- }
- // Truncate Base64 string to exactly the requested number of bits
- integer total_base64_chars = (integer)ceil((float)number_of_bits / 6.0);
- // NOTE: Extra bits in the last chunk are used as-is for simplicity
- // Specification would require zeroing out unrequested bits
- base64_output = llGetSubString(base64_output, 0, total_base64_chars - 1);
- return base64_output;
- }
- // ============================================================
- // Example usage
- // ============================================================
- default
- {
- state_entry()
- {
- // Initial seed
- llSeedRandomBase64("Example seed material 12345");
- // Generate 256 bits (43 Base64 characters)
- string random_bits = llRandomBase64(256);
- llOwnerSay("Random Base64 (256 bits): " + random_bits);
- // Reseed with additional entropy
- llSeedRandomBase64("Extra entropy 98765");
- random_bits = llRandomBase64(256);
- llOwnerSay("Random Base64 after reseed (256 bits): " + random_bits);
- }
- }
Add Comment
Please, Sign In to add comment