- /**
- * Hashes a given string with a simple but stronger hash function (no salt)
- * and hex-encode it.
- *
- * @param $str The string to be hashed
- * @return $hash The hash from string $str
- */
- public static final function hash ($str) {
- // Hash given string with (better secure) hasher
- $hash = bin2hex(mhash(MHASH_SHA256, $str));
-
- // Return it
- return $hash;
- }
-
- /**
- * "Getter" for length of hash() output. This will be "cached" to speed up
- * things.
- *
- * @return $length Length of hash() output
- */
- public static final function getHashLength () {
- // Is it cashed?
- if (is_null(self::$hashLength)) {
- // No, then hash a string and save its length.
- self::$hashLength = strlen(self::hash('abc123'));
- }
-
- // Return it
- return self::$hashLength;
- }
-
- /**
- * Checks whether the given number is really a number (only chars 0-9).
- *
- * @param $num A string consisting only chars between 0 and 9
- * @param $castValue Whether to cast the value to double. Do only use this to secure numbers from Requestable classes.
- * @param $assertMismatch Whether to assert mismatches
- * @return $ret The (hopefully) secured numbered value
- */
- public function bigintval ($num, $castValue = true, $assertMismatch = false) {
- // Filter all numbers out
- $ret = preg_replace('/[^0123456789]/', '', $num);
-
- // Shall we cast?
- if ($castValue === true) {
- // Cast to biggest numeric type
- $ret = (double) $ret;
- }
-
- // Assert only if requested
- if ($assertMismatch === true) {
- // Has the whole value changed?
- assert(('' . $ret . '' != '' . $num . '') && (!is_null($num)));
- }
-
- // Return result
- return $ret;
- }
-
- /**
- * Checks whether the given hexadecimal number is really a hex-number (only chars 0-9,a-f).
- *
- * @param $num A string consisting only chars between 0 and 9
- * @param $assertMismatch Whether to assert mismatches
- * @return $ret The (hopefully) secured hext-numbered value
- */
- public function hexval ($num, $assertMismatch = false) {
- // Filter all numbers out
- $ret = preg_replace('/[^0123456789abcdefABCDEF]/', '', $num);
-
- // Assert only if requested
- if ($assertMismatch === true) {
- // Has the whole value changed?
- assert(('' . $ret . '' != '' . $num . '') && (!is_null($num)));
- }
-
- // Return result
- return $ret;
- }
-