Implementing the Web Crypto API for Node.js Core

October 20, 2020

The Web Cryptography (or Web Crypto) API is a collection of W3C standardised cryptography primitives defined in the Web Cryptography API specification. It was created after several browsers began adding their own non-interoperable cryptography functions... Read More: https://www.nearform.com/blog/implementing-the-web-cryptography-api-for-node-js-core/

generate-aes-key.js
const { subtle } = require('crypto').webcrypto;

// Node.js Web Crypto only supports 128-bit and 256-bit AES keys
async function generateKey(length) {
  return subtle.generateKey(
    { name: 'AES-CBC', length },
    true,
    ['encrypt', 'decrypt']);
}
generate-ec-pair.js
const { subtle } = require('crypto').webcrypto;

async function generateKeyPair() {
  return subtle.generateKey(
    { name: 'ECDSA', namedCurve: 'P-521' },
    false,
    ['sign', 'verify']);
}