类: Certificate

SPKAC最初是由Netscape实现证书签名请求机制并正式指定为HTML5的keygen元素的一部分。

HTML5’s keygen element

<keygen> is deprecated since HTML 5.2 and new projects should not use this element anymore.

The crypto module provides the Certificate class for working with SPKAC data. The most common usage is handling output generated by the HTML5 <keygen> element. Node.js uses OpenSSL’s SPKAC implementation internally.

Certificate.exportChallenge(spkac)

  • spkac {string | Buffer | TypedArray | DataView}
  • Returns: {Buffer} The challenge component of the spkac data structure, which includes a public key and a challenge.
const { Certificate } = require('crypto');
const spkac = getSpkacSomehow();
const challenge = Certificate.exportChallenge(spkac);
console.log(challenge.toString('utf8'));
// Prints: the challenge as a UTF8 string

Certificate.exportPublicKey(spkac[, encoding])

  • spkac {string | Buffer | TypedArray | DataView}
  • encoding {string} The [encoding][] of the spkac string.
  • Returns: {Buffer} The public key component of the spkac data structure, which includes a public key and a challenge.
const { Certificate } = require('crypto');
const spkac = getSpkacSomehow();
const publicKey = Certificate.exportPublicKey(spkac);
console.log(publicKey);
// Prints: the public key as <Buffer ...>

Certificate.verifySpkac(spkac)

  • spkac {Buffer | TypedArray | DataView}
  • Returns: {boolean} true if the given spkac data structure is valid, false otherwise.
const { Certificate } = require('crypto');
const spkac = getSpkacSomehow();
console.log(Certificate.verifySpkac(Buffer.from(spkac)));
// Prints: true or false

Legacy API

As a still supported legacy interface, it is possible (but not recommended) to create new instances of the crypto.Certificate class as illustrated in the examples below.

new crypto.Certificate()

Instances of the Certificate class can be created using the new keyword or by calling crypto.Certificate() as a function:

const crypto = require('crypto');

const cert1 = new crypto.Certificate();
const cert2 = crypto.Certificate();

certificate.exportChallenge(spkac)

  • spkac {string | Buffer | TypedArray | DataView}
  • Returns: {Buffer} The challenge component of the spkac data structure, which includes a public key and a challenge.
const cert = require('crypto').Certificate();
const spkac = getSpkacSomehow();
const challenge = cert.exportChallenge(spkac);
console.log(challenge.toString('utf8'));
// Prints: the challenge as a UTF8 string

certificate.exportPublicKey(spkac)

  • spkac {string | Buffer | TypedArray | DataView}
  • Returns: {Buffer} The public key component of the spkac data structure, which includes a public key and a challenge.
const cert = require('crypto').Certificate();
const spkac = getSpkacSomehow();
const publicKey = cert.exportPublicKey(spkac);
console.log(publicKey);
// Prints: the public key as <Buffer ...>

certificate.verifySpkac(spkac)

  • spkac {Buffer | TypedArray | DataView}
  • Returns: {boolean} true if the given spkac data structure is valid, false otherwise.
const cert = require('crypto').Certificate();
const spkac = getSpkacSomehow();
console.log(cert.verifySpkac(Buffer.from(spkac)));
// Prints: true or false

最后修改 April 16, 2020: 加密 (a75e592)