MSR JavaScript Cryptography Library

1.5.8 · maintenance · verified Sun Apr 19

msrCrypto is a JavaScript cryptography library, originally developed by Microsoft Research and subsequently maintained by Kevlened. It serves as a polyfill for the W3C Web Cryptography API, enabling modern cryptographic primitives like RSA-OAEP, AES-CBC, SHA-256/384/512, HMAC, ECDH, and ECDSA in environments where native Web Crypto API support is absent or inconsistent. Its primary differentiation is its broad browser compatibility, extending to older browsers like Internet Explorer 8, 9, 10, and 11, alongside modern browsers. Since version 1.4, it has adopted a Promise-based API for asynchronous operations, aligning with the Web Crypto API specification. The current stable version, as per the user's input, is 1.5.8, though a `1.6.5` version appears on npm under `@dashdot/msrcrypto` with the last publish 4 years ago, and `1.5.8` itself was published 6 years ago. Its release cadence appears infrequent, with updates primarily addressing API compliance, security bug fixes, and feature enhancements for specific algorithms.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to generate an AES-CBC key and encrypt a plain text message using the `msrCrypto.subtle` API, mimicking the Web Crypto API. It highlights the promise-based asynchronous operations.

// This quickstart assumes msrcrypto.js has been loaded via a <script> tag,
// making the global `msrCrypto` object available.

// 1. Generate an AES-CBC key
const iv = new new Uint8Array(16); // Initialization Vector
// On modern browsers, use window.crypto.getRandomValues(iv);
// On older browsers, ensure PRNG is seeded (see warnings) or provide secure random bytes
// For this example, we'll simulate random values or assume a modern environment fallback
// For a truly secure setup in older IE, you'd need server-side entropy for msrCrypto.initPrng()

// Simulate getRandomValues for environments where it might not be available or fully polyfilled for iv generation
if (typeof window.crypto !== 'undefined' && typeof window.crypto.getRandomValues === 'function') {
  window.crypto.getRandomValues(iv);
} else {
  // Fallback for demonstration, NOT cryptographically secure in production for IV
  for (let i = 0; i < iv.length; i++) iv[i] = Math.floor(Math.random() * 256);
}

msrCrypto.subtle.generateKey(
  {
    name: "AES-CBC",
    length: 256, // For AES-256
  },
  true, // key is extractable
  ["encrypt", "decrypt"]
)
.then(function(key) {
  console.log("AES-CBC Key generated successfully.");
  // 2. Encrypt some data
  const dataToEncrypt = new TextEncoder().encode("Hello, msrcrypto, this is a secret message!");

  return msrCrypto.subtle.encrypt(
    {
      name: "AES-CBC",
      iv: iv,
    },
    key,
    dataToEncrypt
  );
})
.then(function(encryptedData) {
  console.log("Data encrypted successfully (ArrayBuffer):");
  // In IE8/9, this might be a regular Array, convert for consistent logging if needed
  console.log(new Uint8Array(encryptedData));
  // Further steps would involve decrypting or sending the data
})
.catch(function(error) {
  console.error("Encryption failed:", error);
});

view raw JSON →