Elgg  Version 1.11
Public Member Functions | Public Attributes | Protected Member Functions | List of all members
ElggCrypto Class Reference

Public Member Functions

 getRandomBytes ($length)
 Generate a string of highly randomized bytes (over the full 8-bit range). More...
 
 getHmac ($data, $algo= 'sha256', $key= '')
 Get an HMAC token builder/validator object. More...
 
 getRandomString ($length, $chars=null)
 Generate a random string of specified length. More...
 
 areEqual ($str1, $str2)
 Are two strings equal (compared in constant time)? More...
 

Public Attributes

const CHARS_PASSWORD = 'bcdfghjklmnpqrstvwxyz2346789'
 Character set for temp passwords (no risk of embedded profanity/glyphs that look similar) More...
 
const CHARS_HEX = '0123456789abcdef'
 Character set for hexadecimal. More...
 

Protected Member Functions

 strlen ($binary_string)
 Count the number of bytes in a string. More...
 

Detailed Description

Definition at line 10 of file ElggCrypto.php.

Member Function Documentation

ElggCrypto::areEqual (   $str1,
  $str2 
)

Are two strings equal (compared in constant time)?

Parameters
string$str1First string to compare
string$str2Second string to compare
Returns
bool

Based on password_verify in PasswordCompat

Author
Anthony Ferrara ircma.nosp@m.xell.nosp@m.@php..nosp@m.net http://www.opensource.org/licenses/mit-license.html MIT License

Definition at line 245 of file ElggCrypto.php.

ElggCrypto::getHmac (   $data,
  $algo = 'sha256',
  $key = '' 
)

Get an HMAC token builder/validator object.

Parameters
mixed$dataHMAC data or serializable data
string$algoHash algorithm
string$keyOptional key (default uses site secret)
Returns

Definition at line 169 of file ElggCrypto.php.

ElggCrypto::getRandomBytes (   $length)

Generate a string of highly randomized bytes (over the full 8-bit range).

Parameters
int$lengthNumber of bytes needed
Returns
string Random bytes
Author
George Argyros argyr.nosp@m.os.g.nosp@m.eorge.nosp@m.@gma.nosp@m.il.co.nosp@m.m

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GEORGE ARGYROS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Our primary choice for a cryptographic strong randomness function is openssl_random_pseudo_bytes.

If mcrypt extension is available then we use it to gather entropy from the operating system's PRNG. This is better than reading /dev/urandom directly since it avoids reading larger blocks of data than needed.

No build-in crypto randomness function found. We collect any entropy available in the PHP core PRNGs along with some filesystem info and memory stats. To make this data cryptographically strong we add data either from /dev/urandom or if its unavailable, we gather entropy by measuring the time needed to compute a number of SHA-1 hashes.

Definition at line 55 of file ElggCrypto.php.

ElggCrypto::getRandomString (   $length,
  $chars = null 
)

Generate a random string of specified length.

Uses supplied character list for generating the new string. If no character list provided - uses Base64 URL character set.

Parameters
int$lengthDesired length of the string
string | null$charsCharacters to be chosen from randomly. If not given, the Base64 URL charset will be used.
Returns
string The random string
Exceptions
InvalidArgumentException
See also
https://github.com/zendframework/zf2/blob/master/library/Zend/Math/Rand.php#L179

Definition at line 195 of file ElggCrypto.php.

ElggCrypto::strlen (   $binary_string)
protected

Count the number of bytes in a string.

We cannot simply use strlen() for this, because it might be overwritten by the mbstring extension. In this case, strlen() will count the number of characters based on the internal encoding. A sequence of bytes might be regarded as a single multibyte character.

Use elgg_strlen() to count UTF-characters instead of bytes.

Parameters
string$binary_stringThe input string
Returns
int The number of bytes

From PasswordCompat

Author
Anthony Ferrara ircma.nosp@m.xell.nosp@m.@php..nosp@m.net http://www.opensource.org/licenses/mit-license.html MIT License

Definition at line 278 of file ElggCrypto.php.

Member Data Documentation

const ElggCrypto::CHARS_HEX = '0123456789abcdef'

Character set for hexadecimal.

Definition at line 20 of file ElggCrypto.php.

const ElggCrypto::CHARS_PASSWORD = 'bcdfghjklmnpqrstvwxyz2346789'

Character set for temp passwords (no risk of embedded profanity/glyphs that look similar)

Definition at line 15 of file ElggCrypto.php.


The documentation for this class was generated from the following file: