Exemple #1
0
 /**
  * Implementation of the PBKDF2 key derivation function as described in RFC 2898.
  *
  * This is essentially just a really thorough hashing algorithm
  *
  * PBKDF2 was published as part of PKCS #5 v2.0 by RSA Security. The standard is
  * also documented in IETF RFC 2898.
  *
  * @author Henry Merriam <*****@*****.**>
  * @param string $string The value being hashed
  * @param string $salt The hashing salt
  * @param int $keyLength The derived key length (octets)
  * @param int $iterations The number of iterations to perform
  * @return string Returns the hashed value
  */
 public static function pbkdf2($string, $salt, $keyLength = 32, $iterations = 1000)
 {
     $string = (string) $string;
     $salt = (string) $salt;
     $keyLength = max(1, \r8\numVal($keyLength));
     $iterations = max(1, (int) $iterations);
     $algo = "sha256";
     $hashLength = 32;
     if ($keyLength > (pow(2, 32) - 1) * $hashLength) {
         throw new \r8\Exception\Argument(3, "Key Length", "Derived key length is too long");
     }
     // number of derived key blocks to compute
     $max = ceil($keyLength / $hashLength);
     $accum = null;
     for ($i = 1; $i <= $max; $i++) {
         $f = $u = hash_hmac($algo, $salt . pack('N', $i), $string, true);
         for ($j = 1; $j < $iterations; $j++) {
             $f ^= $u = hash_hmac($algo, $u, $string, true);
         }
         // concatenate blocks of the derived key
         $accum .= $f;
     }
     // Shorten down the accumulated hash to the requested key length
     return substr($accum, 0, $keyLength);
 }
Exemple #2
0
 /**
  * Returns a cached value based on it's key
  *
  * @param String $key The value to retrieve
  * @return mixed Returns the cached value
  */
 public function get($key)
 {
     $this->connect();
     $result = $this->link->get($key);
     if (is_numeric($result)) {
         return \r8\numVal($result);
     } else {
         if ($result == "b:0;") {
             return FALSE;
         }
     }
     $result = @unserialize($result);
     if ($result === FALSE) {
         return NULL;
     }
     return $result;
 }
Exemple #3
0
 /**
  * Constructor...
  *
  * @param Mixed $start The starting value
  * @param Mixed $end The ending value
  * @param Integer $step The size of the step to take between values
  */
 public function __construct($start, $end, $step = 1)
 {
     $start = \r8\reduce($start);
     $end = \r8\reduce($end);
     if (ctype_alpha($start) && ctype_alpha($end)) {
         if (ctype_upper(substr($start, 0, 1))) {
             $this->mode = self::MODE_ALPHA_UPPER;
         } else {
             $this->mode = self::MODE_ALPHA_LOWER;
         }
         $this->start = self::alpha2num((string) $start);
         $this->end = self::alpha2num((string) $end);
     } else {
         $this->mode = self::MODE_NUMERIC;
         $this->start = \r8\numVal($start);
         $this->end = \r8\numVal($end);
     }
     $this->step = (int) $step == 0 ? 1 : $step;
 }
Exemple #4
0
/**
 * Keeps a value within a range by wrapping values that fall outside the boundaries.
 *
 * Unlike intWrap, this function handles float values. However, because of this,
 * it means that the limits are considered equal.
 *
 * @param Integer|Float $value The value to be wrapped
 * @param Integer|Float $lower The lower limit of the wrap range
 * @param Integer|Float $upper The upper limit of the wrap range
 * @param Boolean $useLower Because the limits are equal, this toggles whether the upper or lower limit will be returned if the value becomes equal to one of them.
 *
 * @return Integer|Float Returns the value, wrapped to within the given range
 */
function numWrap($value, $lower, $upper, $useLower = TRUE)
{
    $value = \r8\numVal($value);
    $lower = \r8\numVal($lower);
    $upper = \r8\numVal($upper);
    $useLower = (bool) $useLower;
    if ($lower == $upper) {
        return $lower;
    }
    if ($upper < $lower) {
        \r8\swap($upper, $lower);
    }
    if (\r8\num\between($value, $lower, $upper)) {
        $out = $value;
    } else {
        $delta = $upper - $lower;
        if ($value < $lower) {
            $distance = $lower - $value;
            if ($distance > $delta) {
                $distance -= floor($distance / $delta) * $delta;
            }
            $out = $upper - $distance;
        } else {
            $distance = $value - $upper;
            if ($distance > $delta) {
                $distance -= floor($distance / $delta) * $delta;
            }
            $out = $lower + $distance;
        }
    }
    if ($useLower && $out == $upper) {
        return $lower;
    } else {
        if (!$useLower && $out == $lower) {
            return $upper;
        }
    }
    return $out;
}
Exemple #5
0
 public function testNumVal()
 {
     $this->assertEquals(1, \r8\numVal(1));
     $this->assertEquals(1.5, \r8\numVal(1.5));
     $this->assertEquals(1, \r8\numVal("1"));
     $this->assertEquals(1.5, \r8\numVal("1.5"));
 }