Moira_Lachesis

llRandomBase64()

Sep 22nd, 2025 (edited)
13
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.90 KB | Source Code | 0 0
  1. // ============================================================
  2. // Hash_DRBG-inspired PRNG using llSHA256String() + Base64
  3. // Demonstration only
  4. // ============================================================
  5.  
  6. // PRNG state
  7. string prng_internal_state = "";
  8. integer prng_reseed_counter = 0; // Ensures unique blocks per call
  9. integer PRNG_RESEED_INTERVAL = 10; // Max blocks before automatic reseed
  10.  
  11. // ============================================================
  12. // Seed or reseed PRNG
  13. //
  14. // Seeds or refreshes the PRNG using provided entropy material.
  15. // Resets the counter to ensure new blocks are unique.
  16. // ============================================================
  17. llSeedRandomBase64(string entropy_material)
  18. {
  19. // Incorporate provided entropy into PRNG state
  20. // Can be called for initial seeding or later reseeding
  21. prng_internal_state = llSHA256String(prng_internal_state + entropy_material);
  22. prng_reseed_counter = 1;
  23. }
  24.  
  25. // ============================================================
  26. // Generate number_of_bits random bits as Base64
  27. //
  28. // Produces a Base64 string representing the requested number of bits.
  29. // Uses Hash_DRBG-style counter for unique blocks and automatic reseeding.
  30. // ============================================================
  31. string llRandomBase64(integer number_of_bits)
  32. {
  33. // Hex string to accumulate SHA256 outputs
  34. string hex_output = "";
  35.  
  36. // Generate enough hex characters to cover requested bits
  37. while (llStringLength(hex_output) * 4 < number_of_bits)
  38. {
  39. // Automatic reseed if counter exceeds threshold
  40. if (prng_reseed_counter >= PRNG_RESEED_INTERVAL)
  41. {
  42. // Use local source of entropy for demonstration
  43. // In a real system, stronger entropy would be preferable
  44. string entropy = (string)llFrand(1000000.0);
  45. llSeedRandomBase64(entropy);
  46. }
  47.  
  48. // Include counter to ensure unique SHA256 block
  49. string hash_block = llSHA256String(prng_internal_state + (string)prng_reseed_counter);
  50. prng_internal_state = hash_block;
  51. prng_reseed_counter += 1;
  52.  
  53. hex_output += hash_block;
  54. }
  55.  
  56. // Append extra zeros to ensure last 6-character chunk is complete
  57. // Necessary to properly left-align the last random bits in their 24-bit chunk
  58. hex_output += "00000";
  59.  
  60. // Convert hex string to Base64 in 6-hex-character (24-bit) chunks
  61. string base64_output = "";
  62. integer i;
  63. for (i = 0; i <= llStringLength(hex_output) - 6; i += 6)
  64. {
  65. string hex_chunk = llGetSubString(hex_output, i, i + 5);
  66. integer chunk24 = (integer)("0x" + hex_chunk);
  67. integer aligned32 = chunk24 << 8; // Left-align 24 bits for llIntegerToBase64
  68. string b64 = llGetSubString(llIntegerToBase64(aligned32), 0, 3);
  69. base64_output += b64;
  70. }
  71.  
  72. // Truncate Base64 string to exactly the requested number of bits
  73. integer total_base64_chars = (integer)ceil((float)number_of_bits / 6.0);
  74.  
  75. // NOTE: Extra bits in the last chunk are used as-is for simplicity
  76. // Specification would require zeroing out unrequested bits
  77. base64_output = llGetSubString(base64_output, 0, total_base64_chars - 1);
  78.  
  79. return base64_output;
  80. }
  81.  
  82. // ============================================================
  83. // Example usage
  84. // ============================================================
  85. default
  86. {
  87. state_entry()
  88. {
  89. // Initial seed
  90. llSeedRandomBase64("Example seed material 12345");
  91.  
  92. // Generate 256 bits (43 Base64 characters)
  93. string random_bits = llRandomBase64(256);
  94. llOwnerSay("Random Base64 (256 bits): " + random_bits);
  95.  
  96. // Reseed with additional entropy
  97. llSeedRandomBase64("Extra entropy 98765");
  98. random_bits = llRandomBase64(256);
  99. llOwnerSay("Random Base64 after reseed (256 bits): " + random_bits);
  100. }
  101. }
  102.  
Add Comment
Please, Sign In to add comment