JWT::sign PHP Method

sign() public static method

Sign a string with a given key and algorithm.
public static sign ( string $msg, string | resource $key, string $method = 'HS256' ) : string
$msg string The message to sign
$key string | resource The secret key
$method string The signing algorithm. Supported algorithms are 'HS256', 'HS384', 'HS512' and 'RS256'
return string An encrypted message
    public static function sign($msg, $key, $method = 'HS256')
    {
        if (empty(self::$methods[$method])) {
            throw new DomainException('Algorithm not supported');
        }
        list($function, $algo) = self::$methods[$method];
        switch ($function) {
            case 'hash_hmac':
                return hash_hmac($algo, $msg, $key, true);
            case 'openssl':
                $signature = '';
                $success = openssl_sign($msg, $signature, $key, $algo);
                if (!$success) {
                    throw new DomainException("OpenSSL unable to sign data");
                } else {
                    return $signature;
                }
        }
    }

Usage Example

Esempio n. 1
0
 /**
  * @param object|array $payload PHP object or array
  * @param string       $key     The secret key
  * @param string       $algo    The signing algorithm
  *
  * @return string A JWT
  */
 public static function encode($payload, $key, $algo = 'HS256')
 {
     $header = array('typ' => 'jwt', 'alg' => $algo);
     $segments = array();
     $segments[] = JWT::urlsafeB64Encode(JWT::jsonEncode($header));
     $segments[] = JWT::urlsafeB64Encode(JWT::jsonEncode($payload));
     $signing_input = implode('.', $segments);
     $signature = JWT::sign($signing_input, $key, $algo);
     $segments[] = JWT::urlsafeB64Encode($signature);
     return implode('.', $segments);
 }
All Usage Examples Of JWT::sign