Download the PHP package valorin/random without Composer

On this page you can find all versions of the php package valorin/random. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package random

Total Downloads Latest Stable Version License

Random by Valorin

Random generates cryptographically secure random values in a range of different formats through a simple helper package for PHP.

Random was created because I was constantly encountering weak and insecure random value generations within apps during my Laravel and PHP Security Audits and I wanted a secure solution to point my clients to without needing them to implement secure algorithms themselves. The idea was then expanded out a bit to support all the common random value types I've encountered.

Random is completely framework agnostic, the only production dependency is the excellent php-random-polyfill, which does nothing on PHP 8.2+ where the functions are included in core. It supports Laravel's Collections, but doesn't pull in any Laravel code.

You can find more about Random in the Securing Laravel In Depth article.

Installation

You can install the package via composer:

There is no need to install any service providers, Random should just work out of the box.

Random is supported on PHP 7.1 and later.

Usage

Random is designed to be as simple as possible to use. It's a static class, so you can just call the methods directly.

Import the class into your namespace:

Random Integers

Generate a random integer between $min, and $max (inclusive):

Note, this is only really useful if you're using a specific Randomizer Engine (such as when using seeds). For most use cases, I'd suggest sticking with random_int() for simplicity.

Random One-Time Password (Numeric fixed-length OTPs)

Generate a random numeric one-time password (OTP) of $length digits:

This is useful for generating OTPs for SMS or email verification codes. These are commonly done using rand(100000, 999999), which is both insecure and also loses 10% of the possible codes in the 0-99999 range. This provides a secure alternative which includes the full 000000-999999 range (with variable length).

Random String

Generate a random string of $length characters which includes characters from the enabled character types. By default, it will randomly select characters and not guarantee any specific character types are present. If you require one of each character to be included, you can set $requireAll = true.

The following are wrappers for common use cases:

To limit the characters available in any of the types (i.e. lower, upper, numbers, or symbols), you can create a custom Generator instance with your customer character set:

Note, you can chain the use*() methods in any order, and they will persist within that Generator only.

Shuffle Array, String, or Collection

Securely shuffle an array, string, or Laravel Collection, optionally preserving the keys.

Pick $count Items or Characters

Securely pick $count items (or characters) from an array, string, or Laravel Collection.

Passing $count > 1 will return the picks in the same type as $values, so either an array, a Collection, or a string of characters.

When passing $count = 1, the output will be a single array/collection item or character chosen from the input. You can also use the alias pickOne() to pick a single item from $values:

Using a specific \Random\Engine

By default Random will use the secure default \Random\Engine defined by PHP. To use a different Engine, pass it to the use() method and call the above methods on the returned Generator class.

The primary use case for use() is when you need to specify a specific random seed, in order to control the output. Only the returned \Valorin\Random\Generator object will use the provided Engine (and seed), allowing you to create and use the Generator independently of other uses of Random within your app.

You can use use() alongside the character set helpers (useLower(), useUpper(), useNumbers(), useSymbols()), although you will need to call use() first to define the Engine before customising the character set on the Generator object.

TODO

[ ] Use pickArrayKeys() inside shuffle() for better picking [ ] pick() should always return array/collection, while pickOne() returns a single value [ ] Use $collection->slice(0, 0)->push(...$items); to replace collection items

Support My Work! ❤️

You can support my work over on GitHub Sponsors or by becoming a paid subscriber to Securing Laravel, the essential security resource for Laravel and PHP developers!

Contributing

Contributions are very welcome! There isn't a formal guide, but throw in an Issue or PR, and we'll go from there.

Security Vulnerabilities

Please report any security vulnerabilities via the GitHub project or by contacting Stephen Rees-Carter directly.

License

Random is open-source software licensed under the MIT license.


All versions of random with dependencies

PHP Build Version
Package Version
Requires php Version ^7.1 || ^8.0
arokettu/random-polyfill Version ^1.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package valorin/random contains the following files

Loading the files please wait ....