PublicKey

PublicKey

Class representing a generic public key, with utility functions to convert between different formats using Webcrypto

Constructor

(package) new PublicKey() → {CryptoKey}

Source:

Create a empty public key

Returns:
Type
CryptoKey

Methods

(async) fromCose(cose) → {Promise.<PublicKey>}

Source:

Import public key from COSE data. Throws on any type of failure.

Internally this function converts COSE to a JWK, then calls .fromJwk() to import key to CryptoKey

Parameters:
Name Type Description
cose object

COSE data

Returns:
  • Returns itself for chaining
Type
Promise.<PublicKey>

fromCryptoKey(key, algopt) → {CryptoKey}

Source:

Import a CryptoKey, makes basic checks and throws on failure

Parameters:
Name Type Attributes Description
key CryptoKey

CryptoKey to import

alg object <optional>

Algorithm override

Returns:
  • Returns this for chaining
Type
CryptoKey

(async) fromJWK(jwk) → {Promise.<PublicKey>}

Source:

Import public key from JWK. Throws on any type of failure.

Parameters:
Name Type Description
jwk object

JWK object

Returns:
  • Returns itself for chaining
Type
Promise.<PublicKey>

(async) fromPem(pem) → {Promise.<PublicKey>}

Source:

Import public key from SPKI PEM. Throws on any type of failure.

Parameters:
Name Type Description
pem string

PEM formatted string

Returns:
  • Returns itself for chaining
Type
Promise.<PublicKey>

getAlgorithm() → {object|undefined}

Source:

Returns internal algorithm, which should be of one of the following formats

  • RsaHashedImportParams
  • EcKeyImportParams
  • undefined
Returns:
  • Internal algorithm representation, or undefined
Type
object | undefined

getKey() → {CryptoKey}

Source:

Returns internal key in CryptoKey format

  • Mainly intended for internal use
  • Throws if internal CryptoKey does not exist
Returns:
  • Internal CryptoKey instance, or undefined
Type
CryptoKey

setAlgorithm(algorithmInput) → {object|undefined}

Source:

Sets internal algorithm identifier in format used by webcrypto, should be one of

  • Allows adding missing properties
  • Makes sure alg.hash is is { hash: { name: 'foo'} } format
  • Syncs back updated algorithm to this._key
Parameters:
Name Type Description
algorithmInput object

RsaHashedImportParams, EcKeyImportParams, JWK or JWK-like

Returns:
  • Internal algorithm representation, or undefined
Type
object | undefined

toCose() → {object}

Source:

Exports public key to COSE data

  • Only works if original cose data from 'fromCose()' is available
  • Throws on any kind of failure
Returns:
  • Returns COSE data object
Type
object

toJwk() → {object}

Source:

Exports public key to JWK.

  • Only works if original jwk from 'fromJwk()' is available
  • Throws on any kind of failure
Returns:
  • Returns JWK object
Type
object

(async) toPem(forcedExportopt) → {Promise.<string>}

Source:

Exports public key to PEM.

  • Reuses original PEM string if present.
  • Possible to force regeneration of PEM string by setting 'forcedExport' parameter to true
  • Throws on any kind of failure
Parameters:
Name Type Attributes Description
forcedExport boolean <optional>

Force regeneration of PEM string even if original PEM-string is available

Returns:
  • Returns PEM string
Type
Promise.<string>