bllohar/php-jwt-class-with-rsa-support

带有RSA支持的JSON Web Token的PHP实现。

dev-master 2015-07-16 13:41 UTC

This package is not auto-updated.

Last update: 2024-10-02 10:45:25 UTC


README

带有RSA支持的JSON Web Token的PHP实现。

我需要一个客户端移动应用程序的JWT支持,该应用程序使用RSA公钥/私钥。

我认为我应该与大家分享。请随意修改并在您的项目中使用。

支持的算法:RSA(公钥/私钥对)

RS256 - RSA using SHA-256 hash algorithm
RS384 - RSA using SHA-384 hash algorithm
RS512 - RSA using SHA-512 hash algorithm

HMAC Algos : 

HS256 - HMAC using SHA-256 hash algorithm (default)
HS384 - HMAC using SHA-384 hash algorithm
HS512 - HMAC using SHA-512 hash algorithm

如何使用RSA公钥/私钥

使用composer

composer require bllohar/php-jwt-class-with-rsa-support

或者

git clone https://github.com/bllohar/php-jwt-class-with-RSA-support.git
include 'php-jwt-class-with-RSA-support/src/JWToken.php';
$payload = array(
		'username' => 'bllohar',
		'userId'   => 1
);

//Please do not use key provided here in production they are just for demo.
$private_key = file_get_contents('keys/private_key.pem');
$public_key = file_get_contents('keys/public_key.pem');

// Generate token with Private key
$token = JWToken::encode($payload, $private_key,'RS256');

// Verifying the token
try{
	$data = JWToken::decode($token,$public_key,'RS256');

        var_dump($data);
}catch(Exception $e){
	echo $e->getMessage();
}

如何使用HMAC

$payload = array(
		'username' => 'bllohar',
		'userId'   => 1
);

$secret = "^&562!2wzJGH!222"; // your secret key [should store in ENV variable]

// Generate token with secret
$token = JWToken::encode($payload, $secret,'HS512');

// Verifying the token
try{
	    $data = JWToken::decode($token,$secret,'HS512');
        var_dump($data);
}catch(Exception $e){
	echo $e->getMessage();
}