JWT Framework
Search…
Key (JWK)
You can create a JWK object using two static methods:
    new JWK(array $values): creates a JWK using direct values.
    JWK::createFromJson(string $json): creates a JWK using a JSON object.
Hereafter all methods available for a JWK object. The variable $jwk is a valid JWK object.
Please note a JWK object is an immutable object
1
<?php
2
// Check if the key has a parameter.
3
$jwk->has('kty');
4
5
// Retrieve the key parameter.
6
$jwk->get('kty');
7
8
// Retrieve all key parameters.
9
$jwk->all();
10
11
// Calculate the thumbprint of the key. Acceptable hash algorithms are those returned by the PHP function "hash_algos".
12
$jwk->thumbprint('sha256');
13
14
// If the key is a private key (RSA, EC, OKP), it can be converted into public:
15
$public_key = $jwk->toPublic();
16
17
// The JWK object can be serialized into JSON
18
json_encode($jwk);
Copied!

Generate A New Key

This framework is able to create private and public keys easily using the JWKFactory. It is available in the web-token/jwt-key-mgmt component.
1
composer require web-token/jwt-key-mgmt
Copied!
4 types of keys are supported:
    Symmetric Key:
      oct: octet string
    Asymmetric Key:
      RSA: RSA key pair
      EC : Elliptic Curve key pair
      OKP: Octet key pair
The none algorithm needs a key of type none. This is a specific key type that must only be used with this algorithm.

Octet String

The following example will show you how to create an oct key.
Additional parameters will be set to limit the scope of this key (e.g. signature/verification only with the HS256 algorithm).
1
<?php
2
3
use Jose\Component\KeyManagement\JWKFactory;
4
5
$key = JWKFactory::createOctKey(
6
1024, // Size in bits of the key. We recommend at least 128 bits.
7
[
8
'alg' => 'HS256', // This key must only be used with the HS256 algorithm
9
'use' => 'sig' // This key is used for signature/verification operations only
10
]
11
);
Copied!
If you already have a shared secret, you can use it to create an oct key:
1
<?php
2
3
use Jose\Component\KeyManagement\JWKFactory;
4
5
$jwk = JWKFactory::createFromSecret(
6
'My Secret Key', // The shared secret
7
[ // Optional additional members
8
'alg' => 'HS256',
9
'use' => 'sig'
10
]
11
);
Copied!

RSA Key Pair

The following example will show you how to create a RSA key.
The key size must be of 384 bits at least.
1
<?php
2
3
use Jose\Component\KeyManagement\JWKFactory;
4
5
$private_key = JWKFactory::createRSAKey(
6
4096, // Size in bits of the key. We recommend at least 2048 bits.
7
[
8
'alg' => 'RSA-OAEP-256', // This key must only be used with the RSA-OAEP-256 algorithm
9
'use' => 'enc' // This key is used for encryption/decryption operations only
10
]);
Copied!

Elliptic Curve Key Pair

The following example will show you how to create a EC key.
1
<?php
2
3
use Jose\Component\KeyManagement\JWKFactory;
4
5
$key = JWKFactory::createECKey('P-256');
Copied!
The supported curves are:
    P-256
    P-384
    P-521 (note that this is 521 and not 512)

Octet Key Pair

The following example will show you how to create a OKP key.
1
<?php
2
3
use Jose\Component\KeyManagement\JWKFactory;
4
5
$key = JWKFactory::createOKPKey('X25519');
Copied!
The supported curves are:
    Ed25519 for signature/verification only
    X25519 for encryption/decryption only

None Key

The none key type is a special type used only for the none algorithm.
1
<?php
2
3
use Jose\Component\KeyManagement\JWKFactory;
4
5
$key = JWKFactory::createNoneKey();
Copied!

Create Key From External Sources

From Values

In case you already have key values, you can create a key by passing those values as an argument:
1
<?php
2
3
use Jose\Component\KeyManagement\JWKFactory;
4
5
$key = JWKFactory::createFromValues([
6
'kid' => '71ee230371d19630bc17fb90ccf20ae632ad8cf8',
7
'kty' => 'RSA',
8
'alg' => 'RS256',
9
'use' => 'sig',
10
'n' => 'vnMTRCMvsS04M1yaKR112aB8RxOkWHFixZO68wCRlVLxK4ugckXVD_Ebcq-kms1T2XpoWntVfBuX40r2GvcD9UsTFt_MZlgd1xyGwGV6U_tfQUll5mKxCPjr60h83LXKJ_zmLXIqkV8tAoIg78a5VRWoms_0Bn09DKT3-RBWFjk=',
11
'e' => 'AQAB',
12
]);
Copied!

From A Key File

You can convert a PKCS#1 or PKCS#8 key file into a JWK. The following method supports PEM and DER formats. Encrypted keys are also supported.
1
<?php
2
3
use Jose\Component\KeyManagement\JWKFactory;
4
5
$key = JWKFactory::createFromKeyFile(
6
'/path/to/my/key/file.pem', // The filename
7
'Secret', // Secret if the key is encrypted
8
[
9
'use' => 'sig', // Additional parameters
10
]
11
);
Copied!

From A PKCS#12 Certificate

You can convert a PKCS#12 Certificate into a JWK. Encrypted certificates are also supported.
1
<?php
2
3
use Jose\Component\KeyManagement\JWKFactory;
4
5
$key = JWKFactory::createFromPKCS12CertificateFile(
6
'/path/to/my/key/file.p12', // The filename
7
'Secret', // Secret if the key is encrypted
8
[
9
'use' => 'sig', // Additional parameters
10
]
11
);
Copied!

From A X.509 Certificate

You can convert a X.509 Certificate into a JWK.
1
<?php
2
3
use Jose\Component\KeyManagement\JWKFactory;
4
5
$key = JWKFactory::createFromCertificateFile(
6
'/path/to/my/key/file.crt', // The filename
7
[
8
'use' => 'sig', // Additional parameters
9
]
10
);
Copied!
Please note that X.509 certificates only contains public keys.
Last modified 9mo ago