Node-OPCUA Crypto Tools

5.3.5 · active · verified Sun Apr 19

Node-OPCUA Crypto is a robust, TypeScript-first JavaScript module designed to provide a comprehensive suite of cryptographic functionalities specifically for the OPC UA standard. It seamlessly operates in both Node.js and browser environments, ensuring broad compatibility for various industrial IoT and M2M applications. The library is currently on its stable version 5.3.5, with recent releases, such as v5.2.0, focusing on critical improvements like dependency reduction, enhanced browser compatibility, and the introduction of new cryptographic utilities including CRL-to-issuer matching. Key differentiators include its deep integration with OPC UA security requirements, support for generating private keys and X.509 self-signed certificates using native WebCrypto APIs, and dual CommonJS/ESM module support since version 3.0.0. This makes it a foundational component for securing OPC UA communications, offering tools for certificate management, key generation, and compliance with modern cryptographic practices. The project maintains an active release cadence, addressing bugs and introducing features regularly.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to generate a private key and then use it to create a self-signed X.509 certificate, common for OPC UA server and client identities.

import { generatePrivateKey, privateKeyToPEM, CertificatePurpose, createSelfSignedCertificate } from 'node-opcua-crypto';

async function demonstratePrivateKeyAndSelfSignedCertificateCreation() {
    // Create a new private key
    const privateKey = await generatePrivateKey();

    // Convert the private key to a PEM format for storage or display
    const { privPem } = await privateKeyToPEM(privateKey);

    console.log('Generated Private Key (PEM format):\n', privPem);

    // Create a self-signed X.509 certificate
    const { cert } = await createSelfSignedCertificate({
        privateKey,
        notAfter: new Date(Date.now() + 365 * 24 * 60 * 60 * 1000), // Valid for 1 year from now
        notBefore: new Date(),
        subject: 'CN=Test Server, O=MyCompany, L=City, ST=State, C=US',
        dns: ['localhost', 'my-server.example.com'],
        ip: ['127.0.0.1'],
        applicationUri: 'urn:TestServer:MyApplication',
        purpose: CertificatePurpose.ForApplication
    });
    console.log('\nGenerated Self-Signed Certificate (PEM format):\n', cert);
}

demonstratePrivateKeyAndSelfSignedCertificateCreation().catch(console.error);

view raw JSON →