Crypt::URandom(3)

✨ Crypt::URandom ✨

okay bestie, so this is the Crypt::URandom(3) page β€” yes that's the perldoc section 3 situation β€” and it's literally all about Crypt::URandom - Provide non blocking randomness. no cap, that's the whole name. πŸ’œ

πŸ’– VERSION πŸ’–

So this doc is describing Crypt::URandom version 0.55. that's it, that's the version. periodt.

πŸ¦‹ SYNOPSIS πŸ¦‹

okay so there's two ways to do this and both are valid, pick your fighter:

use Crypt::URandom();

my $random_string_50_bytes_long = Crypt::URandom::urandom(50);

OR β€” and this is the one where you import the function directly, main character energy:

use Crypt::URandom qw( urandom );

my $random_string_50_bytes_long = urandom(50);

🌸 DESCRIPTION 🌸

So the whole point of this module, bestie, is that it gives you an interface to the strongest available source of non-blocking randomness on whatever platform you're on right now. iconic behaviour honestly.

The platforms it supports currently: anything that supports getrandom(2), anything with /dev/urandom, and versions of Windows greater than or equal to Windows 2000. that's the roster. 🫢

⭐ SUBROUTINES/METHODS ⭐

πŸŽ€ urandom

This one takes an integer and hands you back a string of the same size filled with random data. slay.

It will throw an exception if the requested amount of random data is not returned β€” so like, it's not gonna silently give you less and let you live a lie. 🫑

The first call will initialize the native cryptographic libraries (if necessary) and load all the required Perl libraries.

And heads up: this call is a buffered read on non Win32 platforms that do not support getrandom(2) or equivalent. iykyk.

πŸ’… urandom_ub

Same deal, different vibe: accepts an integer, returns a string of the same size filled with random data. throws an exception if the requested amount of random data is not returned.

First call initializes the native cryptographic libraries (if necessary) and loads all the required Perl libraries.

The difference? This call is an unbuffered sysread on non Win32 platforms that do not support getrandom(2) or equivalent. the _ub is giving unbuffered, she said what she said. πŸ’…

πŸ”₯ getrandom

This function accepts an integer and returns a string of the same size filled with random data on platforms that implement getrandom(2). throws an exception if the requested amount of random data is not returned.

okay real talk ⚠️ β€” this is NOT portable across all operating systems. it's just made available if you need high-speed generation of random numbers. don't come crying to her when it doesn't work on your weird platform, she warned you.

And like, a very specific little quirk: if an integer is not supplied as an argument, this function will return an empty string as a result β€” which is the same thing that happens as if the integer 0 is supplied as an argument. so nothing and zero are wearing the same outfit. πŸ’€

πŸ™ˆ DIAGNOSTICS πŸ™ˆ

Okay so here's every way this module can yell at you, and what she means by it:

  • No secure alternative for random number generation for Win32 versions older than W2K β€” the module cannot run on versions of Windows earlier than Windows 2000, because there are no cryptographic functions provided by the operating system. sorry babe.
  • Could not import CryptAcquireContext β€” the module was unable to load the CryptAcquireContextA function from the advapi32 dynamic library. The advapi32 library cannot probably be loaded.
  • CryptAcquireContext failed β€” the module was unable to call the CryptAcquireContextA function from the advapi32 dynamic library.
  • Could not import CryptGenRandom β€” the module was unable to load the CryptGenRandom function from the advapi32 dynamic library.
  • Could not import SystemFunction036 β€” the module was unable to load the SystemFunction036 function from the advapi32 dynamic library.
  • The length argument must be supplied and must be an integer β€” the get method must be called with an integer argument to describe how many random bytes are required. it needs to know the count, bestie. πŸ”’
  • CryptGenRandom failed β€” the Windows 2000 CryptGenRandom method call failed to generate the required amount of randomness.
  • RtlGenRand failed β€” the post Windows 2000 RtlGenRand method call failed to generate the required amount of randomness.
  • Only read n bytes from path β€” the /dev/urandom device did not return the desired amount of random bytes.
  • Failed to read from path β€” the /dev/urandom device returned an error when being read from.
  • Failed to open path β€” the /dev/urandom device returned an error when being opened.

🌈 CONFIGURATION AND ENVIRONMENT 🌈

Good news: Crypt::URandom requires no configuration files or environment variables. zero setup, we love a low-maintenance icon. 🧚

BUT β€” one tiny exception, and it's a build-time thing: if the environment variable CRYPT_URANDOM_BUILD_DEBUG is specified when running perl Makefile.PL or make test AND getrandom(2) or its equivalents cannot be detected, extra debug will be shown to show the failures to detect these functions. so it's like a little detective mode for the build. πŸ”

πŸ“ DEPENDENCIES πŸ“

  • If the platform is Win32, the Win32::API module will be required.
  • Otherwise, no other modules other than those provided by perl will be required. so like, core and clean. ✨

πŸ‘‘ INCOMPATIBILITIES πŸ‘‘

None reported. she's not aware of any beef. πŸ•ŠοΈ

πŸ’€ BUGS AND LIMITATIONS πŸ’€

To report a bug, or view the current list of bugs, please visit:

https://github.com/david-dick/crypt-urandom/issues

that's the spot. go tell her what's wrong, she'll listen. 🫢

πŸ’œ AUTHOR πŸ’œ

David Dick "<ddick@cpan.org>" β€” put some respect on the name.

🌟 ACKNOWLEDGEMENTS 🌟

The Win32::API code for interacting with Microsoft's CryptoAPI https://en.wikipedia.org/wiki/Microsoft_CryptoAPI was copied with extreme gratitude from Crypt::Random::Source::Strong::Win32 by Max Kanat-Alexander https://metacpan.org/author/MKANAT. credit where credit is due, always. πŸ«‘πŸ’–

Copyright (c) 2025, David Dick "<ddick@cpan.org>". All rights reserved.

This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. so go off, remix it. πŸ’…

🚨 DISCLAIMER OF WARRANTY 🚨

okay so this part is in ALL CAPS in the original and we are absolutely keeping it that way because it is legally screaming at you. read it, bestie, it matters:

BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR, OR CORRECTION.

IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.


and that's the whole page, babe! πŸ’– little sign-off from the footer so nothing gets lost: this all comes from perl v5.42.0, dated 2026-03-23, and the page is Crypt::URandom(3). πŸŽ€βœ¨ now go generate some genuinely unpredictable bytes, mwah 🫢🌈

πŸ“¦ where this page comes from