You are here

protected static function Hash::hashInput in Acquia Purge 8

Create a hash with the given input and length.

Parameters

string $input: The input string to be hashed.

int $length: The length of the hash.

Return value

string Cryptographic hash with the given length.

2 calls to Hash::hashInput()
Hash::cacheTags in src/AcquiaCloud/Hash.php
Create unique hashes/IDs for a list of cache tag strings.
Hash::siteIdentifier in src/AcquiaCloud/Hash.php
Create a unique hash that identifies this site.

File

src/AcquiaCloud/Hash.php, line 21

Class

Hash
Helper class that centralizes string hashing for security and maintenance.

Namespace

Drupal\acquia_purge\AcquiaCloud

Code

protected static function hashInput($input, $length) {

  // MD5 is the fastest algorithm beyond CRC32 (which is 30% faster, but high
  // collision risk), so this is the best bet for now. If collisions are going
  // to be a major problem in the future, we might have to consider a hash DB.
  $hex = md5($input);

  // The produced HEX can be converted to BASE32 number to take less space.
  // For example 5 characters HEX can be stored in 4 characters BASE32.
  $hash = base_convert(substr($hex, 0, ceil($length * 1.25)), 16, 32);

  // Return a hash with consistent length, padding zeroes if needed.
  return strtolower(str_pad(substr($hash, 0, $length), $length, '0', STR_PAD_LEFT));
}