Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

webcrypto-core

PeculiarVentures8.4mMIT1.8.1TypeScript support: included

Common layer to be used by crypto libraries based on WebCrypto API for input validation.

webcrypto, crypto, polyfill, aes, rsa, sha, ec, shake

readme

License test Coverage Status npm version

NPM

webcrypto-core

We have created a number of WebCrypto polyfills including: node-webcrypto-ossl, node-webcrypto-p11, and webcrypto-liner. webcrypto-core was designed to be a common layer to be used by all of these libraries for input validation.

Unless you intend to create a WebCrypto polyfill this library is probably not useful to you.

Installing

npm install webcrypto-core

Example

Current examples shows how you can implement your own WebCrypt interface

const core = require(".");
const crypto = require("crypto");

class Sha1Provider extends core.ProviderCrypto {

  constructor() {
    super();

    this.name = "SHA-1";
    this.usages = [];
  }

  async onDigest(algorithm, data) {
    const hash = crypto.createHash("SHA1").update(Buffer.from(data)).digest();
    return new Uint8Array(hash).buffer;
  }

}

class SubtleCrypto extends core.SubtleCrypto {
  constructor() {
    super();

    // Add SHA1 provider to SubtleCrypto
    this.providers.set(new Sha1Provider());
  }
}

class Crypto extends core.Crypto {

  constructor() {
    this.subtle = new SubtleCrypto();
  }

  getRandomValues(array) {
    const buffer = Buffer.from(array.buffer);
    crypto.randomFillSync(buffer);
    return array;
  }

}

const webcrypto = new Crypto();
webcrypto.subtle.digest("SHA-1", Buffer.from("TEST MESSAGE"))
  .then((hash) => {
    console.log(Buffer.from(hash).toString("hex")); // dbca505deb07e1612d944a69c0c851f79f3a4a60
  })
  .catch((err) => {
    console.error(err);
  });

changelog

Changelog

All notable changes to this project will be documented in this file. See standard-version for commit guidelines.

1.8.1 (2024-10-08)

Bug Fixes

  • Improve point size calculation in toWebCryptoSignature method (5fe372e)

1.8.0 (2024-05-28)

Features

  • Add x25519 and ed25519 to ec module (b65b6af)

Bug Fixes

  • Improve data validation for importKey and exportKey methods (4ef9eaa)

1.7.9 (2024-03-28)

Bug Fixes

  • nullability checks in AesGcmProvider (7b57920)

1.7.8 (2024-01-22)

Bug Fixes

  • add ...args for generateKey with Ed25519 algorithm (e8c65c3)
  • build error TypeScript 5.3 (b83ef17)
  • type annotations and add missing return types (c83d87e)

1.7.7 (2023-03-21)

1.7.6 (2023-02-10)

Bug Fixes

1.7.5 (2022-05-12)

1.7.4 (2022-05-12)

1.7.3 (2022-03-31)

Bug Fixes

  • Buffer is undefined for browsers (2068c27)

1.7.2 (2022-03-25)

Bug Fixes

  • @typescript/lib-dom dependency (47e57b0)
  • remove @types/web (e43fae2)

1.7.0 (2022-03-04)

Features

1.6.0 (2022-03-02)

Features

1.5.1 (2022-03-02)

Bug Fixes

1.5.0 (2022-03-02)

Features