MediaWiki
REL1_34
MWCryptRand.php
Go to the documentation of this file.
1
<?php
27
class
MWCryptRand
{
28
36
public
static
function
generateHex
( $chars ) {
37
// hex strings are 2x the length of raw binary so we divide the length in half
38
// odd numbers will result in a .5 that leads the generate() being 1 character
39
// short, so we use ceil() to ensure that we always have enough bytes
40
$bytes = ceil( $chars / 2 );
41
// Generate the data and then convert it to a hex string
42
$hex = bin2hex( random_bytes( $bytes ) );
43
44
// A bit of paranoia here, the caller asked for a specific length of string
45
// here, and it's possible (eg when given an odd number) that we may actually
46
// have at least 1 char more than they asked for. Just in case they made this
47
// call intending to insert it into a database that does truncation we don't
48
// want to give them too much and end up with their database and their live
49
// code having two different values because part of what we gave them is truncated
50
// hence, we strip out any run of characters longer than what we were asked for.
51
return
substr( $hex, 0, $chars );
52
}
53
}
MWCryptRand
Definition
MWCryptRand.php:27
MWCryptRand\generateHex
static generateHex( $chars)
Generate a run of cryptographically random data and return it in hexadecimal string format.
Definition
MWCryptRand.php:36
includes
utils
MWCryptRand.php
Generated on Fri Apr 5 2024 23:10:45 for MediaWiki by
1.9.8