uncrypto

repository·main·Indexed 42 days ago

https://github.com/unjs/uncrypto

A unified API for Web Crypto and Subtle Crypto that works across Node.js (version 15+), browsers, and other runtimes. It provides a consistent interface for accessing subtle, randomUUID, and getRandomValues, wrapping the native node:crypto module in Node.js and the standard Web Crypto API in browser environments.

Tokens
1.2K
Snippets
9
Records
10
Agent score
54%

What's inside uncrypto

  1. Check uncrypto runtime requirements

    main

    Before using uncrypto, ensure your environment meets the following requirements:

    • Node.js: Version 15 and above. Note that this library does not provide polyfills for older versions.
    • Browser: Must be a Secure Context (HTTPS or localhost) and running in a supported browser.
    • Other Runtimes: Must expose globalThis.crypto and globalThis.crypto.subtle. You may need to provide a polyfill if these are missing.
  2. Import uncrypto in ESM or CommonJS

    main

    Import the subtle, randomUUID, and getRandomValues APIs from uncrypto. The library uses conditional exports to provide the appropriate implementation for Node.js (using the Crypto Module) or Web targets (using the Web Crypto API).

    // ESM
    import { subtle, randomUUID, getRandomValues } from "uncrypto";
    
    // CommonJS
    const { subtle, randomUUID, getRandomValues } = require("uncrypto");
  3. Use uncrypto in Node.js environments

    main

    In Node.js environments, uncrypto provides a Web Crypto API compatible interface by wrapping the native node:crypto module. It exports subtle, randomUUID, and getRandomValues to match the standard Web Crypto specification.

    import crypto from 'uncrypto';
    
    // Generate a UUID
    const uuid = crypto.randomUUID();
    
    // Get random values
    const array = new Uint8Array(10);
    crypto.getRandomValues(array);
    
    // Use subtle crypto
    const subtle = crypto.subtle;
  4. Use the uncrypto default export for Web environments

    main

    In Web environments, uncrypto provides a unified interface to the standard Web Crypto API. You can import the default export to access subtle, randomUUID, and getRandomValues directly.

    import crypto from 'uncrypto';
    
    // Accessing the API
    const uuid = crypto.randomUUID();
    const subtle = crypto.subtle;
  5. Access Web Crypto primitives via subtle

    main

    The subtle object provides access to the Web Crypto API's low-level cryptographic primitives (such as digest, encrypt, and decrypt). In Node.js, this is sourced from node:crypto.webcrypto.subtle.

    import { subtle } from 'uncrypto';
    
    // Example: computing a SHA-256 hash
    const hash = await subtle.digest('SHA-256', new Uint8Array([1, 2, 3]));
  6. Generate a UUID with randomUUID()

    main

    The randomUUID() function returns a version 4 UUID using the Node.js native crypto implementation. It is a direct wrapper around node:crypto.randomUUID().

    import { randomUUID } from 'uncrypto';
    
    const uuid = randomUUID();
  7. Get random values with getRandomValues()

    main

    The getRandomValues(array) function fills a given typed array with cryptographically strong random values.

    import { getRandomValues } from 'uncrypto';
    
    const array = new Uint32Array(10);
    getRandomValues(array);
  8. Fill an array with random values using getRandomValues()

    main

    The getRandomValues(array) function fills a typed array with cryptographically strong random values. It uses node:crypto.webcrypto.getRandomValues().

    import { getRandomValues } from 'uncrypto';
    
    const buffer = new Uint8Array(16);
    getRandomValues(buffer);
  9. Access Web Crypto SubtleCrypto with subtle

    main

    The subtle export provides access to the SubtleCrypto interface, which includes low-level cryptographic operations such as encrypt, decrypt, digest, importKey, and sign.

    import { subtle } from 'uncrypto';
    
    // Example: computing a SHA-256 digest
    const hash = await subtle.digest('SHA-256', new TextEncoder().encode('hello'));