@lukeed/csprng
An alias package for `crypto.randomBytes` in Node.js and/or browsers
This package has a good security score with no known vulnerabilities.
Community Reviews
Minimal abstraction over crypto.randomBytes with solid cross-platform support
From an operations perspective, error handling passes through directly from the underlying platform APIs. In Node.js you get standard crypto module errors, in browsers you get Web Crypto API exceptions. There are no retry mechanisms or custom error wrapping, which is actually appropriate for a CSPRNG - if the platform can't generate random bytes, you want to know immediately. Memory footprint is negligible since it's stateless.
The main value proposition is cross-platform consistency without pulling in polyfills or larger dependencies. For services that need cryptographically secure random values in both Node and browser contexts, this eliminates conditional imports. Performance is native speed since there's no overhead beyond a function call.
Best for: Projects needing cross-platform CSPRNG with minimal dependencies and no abstraction overhead.
Avoid if: You only target Node.js or need additional features like encoding helpers or error normalization.
Minimalist cross-platform crypto randomness with zero learning curve
The main value proposition is abstracting away the platform differences between Node's `crypto.randomBytes` and the browser's `crypto.getRandomValues`. The package is tiny (under 300 bytes), has no dependencies, and just works. Error handling is straightforward - if you pass invalid input, you get clear type errors. The documentation is minimal but sufficient since the API surface is so small.
One minor friction point is the lack of async support - it only provides synchronous random byte generation. For most use cases this is fine, but if you need `randomBytes().promise()` behavior in Node, you'll need to wrap it yourself or use the native APIs directly. Otherwise, it's a solid, no-frills utility that delivers on its promise.
Best for: Projects needing simple cross-platform cryptographically secure random bytes without platform-specific conditional code.
Avoid if: You need only Node.js support (use native crypto directly) or require async random byte generation with promises.
Solid CSPRNG abstraction with minimal overhead and good cross-platform support
The API is dead simple - just call csprng(length) and you get a Buffer/Uint8Array with cryptographically secure random bytes. Error handling is transparent, passing through whatever the underlying crypto API throws. The package correctly uses platform-native CSPRNGs rather than implementing its own, which is the right security decision.
The main gotcha is that it returns different types depending on environment (Buffer in Node, Uint8Array in browsers), so you need to handle that in your code if you're doing cross-platform work. Also, there's minimal input validation - passing negative numbers or non-integers will fail ungracefully. For security-critical applications, you'll want to wrap it with your own validation layer.
Best for: Projects needing simple, lightweight CSPRNG access across Node.js and browsers without bringing in heavy dependencies.
Avoid if: You need robust input validation, error handling abstractions, or are already using a crypto library with built-in CSPRNG utilities.
Sign in to write a review
Sign In