Skip to content

Commit a6b2ffd

Browse files
authored
Update README.md
1 parent 7e06209 commit a6b2ffd

File tree

1 file changed

+57
-29
lines changed

1 file changed

+57
-29
lines changed

Diff for: README.md

+57-29
Original file line numberDiff line numberDiff line change
@@ -6,67 +6,62 @@
66

77
# PHP-JWT
88

9-
PHP-JWT is a package written in PHP programming language to encode (generate), decode (parse), verify and validate JWTs
10-
(JSON Web Tokens). It provides a fluent, easy-to-use, and object-oriented interface.
9+
PHP-JWT is a PHP programming language package designed for encoding (generation), decoding (parsing), verification, and validation of JSON Web Tokens (JWTs).
10+
Featuring a fluent, user-friendly, and object-oriented interface.
1111

1212
Confirmed by [JWT.io](https://jwt.io).
1313

1414
## Documentation
1515

16-
### Versions
17-
* 2.x.x (LTS)
18-
* 1.x.x (Unsupported)
19-
2016
### What is JWT?
2117

22-
In case you are unfamiliar with JWT you can read [Wikipedia](https://en.wikipedia.org/wiki/JSON_Web_Token) or
23-
[JWT.io](https://jwt.io).
18+
If you're not familiar with JWTs, you can refer to the [Wikipedia page](https://en.wikipedia.org/wiki/JSON_Web_Token) or visit [JWT.io](https://jwt.io) for more information.
2419

2520
### Installation
2621

27-
Add the package to your Composer dependencies with the following command:
22+
Include the package in your Composer dependencies using the following command:
2823

2924
```bash
30-
composer require miladrahimi/php-jwt "2.*"
25+
composer require miladrahimi/php-jwt "3.*"
3126
```
3227

33-
### Simple example
28+
### Quick Start
3429

35-
The following example shows how to generate a JWT and parse it using the *HS256* algorithm.
30+
Here's an example demonstrating how to generate a JWT and parse it using the `HS256` algorithm:
3631

3732
```php
3833
use MiladRahimi\Jwt\Generator;
3934
use MiladRahimi\Jwt\Parser;
4035
use MiladRahimi\Jwt\Cryptography\Algorithms\Hmac\HS256;
4136

42-
// Use HS256 to generate and parse tokens
37+
// Use HS256 to generate and parse JWTs
4338
$signer = new HS256('12345678901234567890123456789012');
4439

45-
// Generate a token
40+
// Generate a JWT
4641
$generator = new Generator($signer);
47-
$jwt = $generator->generate(['id' => 666, 'is-admin' => true]);
42+
$jwt = $generator->generate(['id' => 13, 'is-admin' => true]);
43+
44+
print_r($jwt); // "abc.123.xyz"
4845

4946
// Parse the token
5047
$parser = new Parser($signer);
5148
$claims = $parser->parse($jwt);
5249

53-
print_r($claims); // ['id' => 666, 'is-admin' => true]
50+
print_r($claims); // ['id' => 13, 'is-admin' => true]
5451
```
5552

5653
### HMAC Algorithms
5754

58-
HMAC algorithms use symmetric keys.
59-
A single key can both sign and verify JWTs.
60-
This package supports HS256, HS384, and HS512 of HMAC algorithms.
61-
The example mentioned above demonstrates how to use an HMAC algorithm (HS256) to sign and verify a JWT.
55+
HMAC algorithms indeed rely on symmetric keys, allowing a single key to both sign and verify JWTs.
56+
This package supports HS256, HS384, and HS512 HMAC algorithms.
57+
The example above showcases the utilization of an HMAC algorithm (HS256) to both sign and verify a JWT.
6258

6359
### RSA Algorithms
6460

65-
RSA algorithms are asymmetric.
66-
A paired key is needed to sign and verify tokens.
67-
To sign a JWT, we use a private key, and to verify it, we use the related public key.
68-
These algorithms can be useful when the authentication server cannot trust resource owners.
69-
Take a look at the following example:
61+
RSA algorithms work with pairs of keys: a private key for signing JWTs and a corresponding public key for verification.
62+
This method is useful when the authentication server can't completely trust resource owners.
63+
This package supports RS256, RS384, and RS512 RSA algorithms.
64+
The example below demonstrates this process.
7065

7166
```php
7267
use MiladRahimi\Jwt\Cryptography\Algorithms\Rsa\RS256Signer;
@@ -80,19 +75,52 @@ use MiladRahimi\Jwt\Parser;
8075
$privateKey = new RsaPrivateKey('/path/to/private.pem');
8176
$signer = new RS256Signer($privateKey);
8277
$generator = new Generator($signer);
83-
$jwt = $generator->generate(['id' => 666, 'is-admin' => true]);
78+
$jwt = $generator->generate(['id' => 13, 'is-admin' => true]);
79+
80+
print_r($jwt); // "abc.123.xyz"
8481

8582
// Parse the token
8683
$publicKey = new RsaPublicKey('/path/to/public.pem');
8784
$verifier = new RS256Verifier($publicKey);
8885
$parser = new Parser($verifier);
8986
$claims = $parser->parse($jwt);
9087

91-
print_r($claims); // ['id' => 666, 'is-admin' => true]
88+
print_r($claims); // ['id' => 13, 'is-admin' => true]
9289
```
9390

94-
You can read [this instruction](https://en.wikibooks.org/wiki/Cryptography/Generate_a_keypair_using_OpenSSL)
95-
to learn how to generate a pair (public/private) RSA key.
91+
You can refer to [this instruction](https://en.wikibooks.org/wiki/Cryptography/Generate_a_keypair_using_OpenSSL) to learn how to generate a pair of RSA keys,
92+
one for public use and the other for private use, using OpenSSL.
93+
94+
### ECDSA Algorithms
95+
96+
The ECDSA algorithm, like RSA, operates asymmetrically, providing a comparably secure solution while offering heightened security measures.
97+
This package supports ES256, ES256K, and RS384 ECDSA algorithms.
98+
The example below demonstrates this process.
99+
100+
```php
101+
use MiladRahimi\Jwt\Cryptography\Algorithms\Ecdsa\ES384Signer;
102+
use MiladRahimi\Jwt\Cryptography\Algorithms\Ecdsa\ES384Verifier;
103+
use MiladRahimi\Jwt\Cryptography\Keys\EcdsaPrivateKey;
104+
use MiladRahimi\Jwt\Cryptography\Keys\EcdsaPublicKey;
105+
use MiladRahimi\Jwt\Generator;
106+
use MiladRahimi\Jwt\Parser;
107+
108+
// Generate a token
109+
$privateKey = new EcdsaPrivateKey('/path/to/private.pem');
110+
$signer = new ES384Signer($privateKey);
111+
$generator = new Generator($signer);
112+
$jwt = $generator->generate(['id' => 13, 'is-admin' => true]);
113+
114+
print_r($jwt); // "abc.123.xyz"
115+
116+
// Parse the token
117+
$publicKey = new EcdsaPublicKey('/path/to/public.pem');
118+
$verifier = new ES384Verifier($publicKey);
119+
$parser = new Parser($verifier);
120+
$claims = $parser->parse($jwt);
121+
122+
print_r($claims); // ['id' => 13, 'is-admin' => true]
123+
```
96124

97125
### Validation
98126

0 commit comments

Comments
 (0)