JsonWebKey
Open source

JsonWebKey

Caution

The experimental module k6/experimental/webcrypto has graduated, and its functionality is now available globally through the crypto object. The k6/experimental/webcrypto is deprecated and will be removed in the near future.

To migrate your scripts, remove the k6/experimental/webcrypto imports and use the crypto object instead.

The JsonWebKey object represents object/dictionary generated by exporting a CryptoKey or used as an input parameter for key import.

The properties of the JsonWebKey could vary depending on the algorithm and key type. See specification JsonWebKey for details.

Properties

PropertyTypeDescription
ktystringThe key type.
kstringThe key value.
algstringThe algorithm.
extboolThe key is extractable.
key_opsstring arrayThe key operations.
crvstringThe curve name.
xstringThe x coordinate.
ystringThe y coordinate.
dstringThe private key.

Example

JavaScript
import { crypto } from 'k6/experimental/webcrypto';

export default async function () {
  const jwk = {
    alg: 'HS256',
    ext: true,
    k: 'H6gLp3lw7w27NrPUn00WpcKU-IJojJdNzhL_8F6se2k',
    key_ops: ['sign', 'verify'],
    kty: 'oct',
  };

  const importedKey = await crypto.subtle.importKey(
    'jwk',
    jwk,
    { name: 'HMAC', hash: { name: 'SHA-256' } },
    true,
    ['sign', 'verify']
  );

  const exportedAgain = await crypto.subtle.exportKey('jwk', importedKey);

  console.log('exported again: ' + JSON.stringify(exportedAgain));
  // should print
  // INFO[0000] exported again: {"k":"H6gLp3lw7w27NrPUn00WpcKU-IJojJdNzhL_8F6se2k","kty":"oct","ext":true,"key_ops":["sign","verify"],"alg":"HS256"}  source=console
}