Example #1
0
 /**
  * Encodes the given salt in MD5 format. If no salt is given a random token with max length is generated as the salt.
  *
  * @see http://php.net/manual/en/function.crypt.php PHP crypt function reference.
  * @param string|null $salt The salt to encode (up to 8 chars). The salt will also be truncated at the first $ found.
  * @return string Returns the encoded salt in MD5 format according to {@link http://php.net/manual/en/function.crypt.php PHP crypt function reference.}
  * @since 0.1
  */
 public function encode(string $salt = null) : string
 {
     if ($salt === null) {
         $encoded = Tokenizer::generate(self::MAX_LENGTH);
     } else {
         $encoded = substr($salt, 0, min(self::MAX_LENGTH, strlen($salt)));
     }
     if (($dollar = strpos($encoded, '$')) !== false) {
         $encoded = substr($encoded, 0, $dollar);
     }
     return self::PREFIX . $encoded . self::POSTFIX;
 }
Example #2
0
 /**
  * Encodes the given salt in StdDES format. If no salt is given a random token with max length is generated as the salt.
  *
  * @see http://php.net/manual/en/function.crypt.php PHP crypt function reference.
  * @param string|null $salt The salt to encode (up to 2 chars from the supported alphabet).
  * @return string Returns the encoded salt in StdDES format according to {@link http://php.net/manual/en/function.crypt.php PHP crypt function reference.}
  * @since 0.1
  */
 public function encode(string $salt = null) : string
 {
     if ($salt === null) {
         $encoded = Tokenizer::generate(self::LENGTH, self::ALPHABET);
     } else {
         if (preg_match('/[^' . preg_quote(self::ALPHABET, '/') . ']/', $salt) !== 0) {
             throw new InvalidArgumentException('The given salt has characters that are not part of the supported alphabet.');
         }
         if (($len = strlen($salt)) < self::LENGTH) {
             $encoded = $salt . Tokenizer::generate(self::LENGTH - $len, self::ALPHABET);
         } else {
             $encoded = substr($salt, 0, min(self::LENGTH, $len));
         }
     }
     return $encoded;
 }