Function importPEM

  • import key from PEM private/public key or certificate string

    Parameters

    • pem: string

      PEM PKCS#8 private key, public key string or PEM certificate

    • alg: string

      signature algorithm (SHA{1,224,256,384,512}with{RSA,RSAandMGF1,ECDSA})

    • Optional sigopt: string | number

      saltLength for RSA-PSS

    Returns Promise<CryptoKey>

    CryptoKey object of W3C Web Crypto API

    See

    https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/importKey

    Description

    This function import a CryptoKey object from PEM public key, private key or certificate string.
    NOTE1: For EC key, namedCurve value will be automatically detected by PEM file. So no need to specify.
    NOTE2: Importing a PEM certificate is supported since 0.5.0.

    Example

    prvkey = await importPEM("-----BEGIN PRIVATE...", "SHA256withRSA");
    prvkey = await importPEM("-----BEGIN PRIVATE...", "SHA256withRSAandMGF1");
    pubkey = await importPEM("-----BEGIN PUBLIC...", "SHA256withECDSA");
    pubkey = await importPEM("-----BEGIN CERTIFICATE...", "SHA256withECDSA");