Documentation

Security
in package

Functions for implementing security, including secure random numbers

Table of Contents

Methods

compareStrings()  : bool
Constant-time string comparison
getSecretSanitizer()  : Secrets
passwordComplexity()  : array<string|int, mixed>
Check the given password meets complexity requirements
randByte()  : string
Return a single random byte
randBytes()  : string
Returns a binary string of random bytes
randStr()  : string
Returns a string of random characters
serverKeySign()  : string
Generate a signature from a given set of fields, using the server key
serverKeyVerify()  : void
Verify a signature which was generated by {@see Security::serverKeySign}
getServerKey()  : string
Return the server key

Methods

compareStrings()

Constant-time string comparison

public static compareStrings(string $known_string, string $user_string) : bool
Parameters
$known_string : string

The known hash

$user_string : string

The user supplied hash to check

Return values
bool —

True if the strings match, false if they don't

passwordComplexity()

Check the given password meets complexity requirements

public static passwordComplexity(string $str, int $length, int $classes, bool $bad_list) : array<string|int, mixed>
Parameters
$str : string

String to check

$length : int

Minimum length in bytes

$classes : int

Minumum number of "character classes", so 2 would accept 'passWORD' but not 'password'

$bad_list : bool

SHould the password be checked against the "bad list" of most common passwords

Return values
array<string|int, mixed> —

Errors, may be an empty array

randByte()

Return a single random byte

public static randByte() : string
Return values
string —

Binary string; one byte

randBytes()

Returns a binary string of random bytes

public static randBytes(int $length) : string
Parameters
$length : int
Return values
string —

Binary string

randStr()

Returns a string of random characters

public static randStr([int $length = 16 ][, mixed $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890' ]) : string
Parameters
$length : int = 16
$chars : mixed = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890'
Return values
string

serverKeySign()

Generate a signature from a given set of fields, using the server key

public static serverKeySign(array<string|int, mixed> $fields) : string

For a given set of fields, the signature will always be the same value. Returned signatures are always URL and HTML safe

Parameters
$fields : array<string|int, mixed>

Key-value fields making up the data to sign

Tags
example

// In method which creates link to resource/download $sig = Security::serverKeySign(['id' => $id]); $file_url = "resource/download?id={$id}&sig={$sig}";

Return values
string —

Signature of the data, always url safe

serverKeyVerify()

Verify a signature which was generated by {@see Security::serverKeySign}

public static serverKeyVerify(array<string|int, mixed> $fields, string $signature) : void
Parameters
$fields : array<string|int, mixed>

Key-value fields making up the data to verify

$signature : string

Incoming signature to check

Tags
example

// In resource::download method Security::serverKeyVerify(['id' => $id], $_GET['sig']);

throws
SignatureInvalidException

A non-string value was specified for the signature

SignatureInvalidException

If the signature is not valid

getServerKey()

Return the server key

protected static getServerKey() : string
Tags
throws
InvalidArgumentException

Config option is not set

InvalidArgumentException

Test-server only value used in production

Return values
string —

Unqiue key for this site

On this page

Search results