What is selfsigned?
The selfsigned npm package is used to generate self-signed SSL/TLS certificates. It is primarily used for development purposes where trusted certificates are not required. The package allows users to create certificates with various options, such as setting the validity period, adding extensions, and defining subject fields.
What are selfsigned's main functionalities?
Generate a self-signed certificate
This feature allows the generation of a self-signed certificate with a common name of 'contoso.com' and a validity of 365 days. The 'pems' object contains the private key, certificate, and other related PEM-encoded data.
const selfsigned = require('selfsigned');
const attrs = [{ name: 'commonName', value: 'contoso.com' }];
const options = { days: 365 };
selfsigned.generate(attrs, options, function(err, pems) {
console.log(pems);
});
Generate a certificate with custom extensions
This feature allows the generation of a self-signed certificate with custom extensions, such as marking the certificate as a CA (Certificate Authority). This is useful for creating more complex certificate chains or for specific use cases requiring certain extensions.
const selfsigned = require('selfsigned');
const attrs = [{ name: 'commonName', value: 'contoso.com' }];
const extensions = [{ name: 'basicConstraints', cA: true }];
const options = { days: 365, extensions: extensions };
selfsigned.generate(attrs, options, function(err, pems) {
console.log(pems);
});
Other packages similar to selfsigned
pem
The 'pem' package is used to generate and manage SSL certificates and keys. It can create private keys, CSRs (Certificate Signing Requests), and certificates. It also supports additional features like creating PKCS12 files and converting certificates to different formats. Compared to 'selfsigned', 'pem' offers a broader range of functionalities for managing certificates beyond just self-signing.
forge
The 'node-forge' package is a native JavaScript implementation of various networking cryptographic operations. It includes the ability to create self-signed certificates, but it also provides a wide range of cryptographic functions such as encryption, decryption, signing, and verification. 'node-forge' is more comprehensive than 'selfsigned' in terms of overall cryptographic capabilities.
Generate a self signed x509 certificate from node.js.
Install
npm install selfsigned
Note: this runs openssl CLI so you need to have openssl in your path. It works for windows as well.
Usage
var selfsigned = require('selfsigned');
selfsigned.generate({ subj: '/CN=contoso.com', days: 365 }, function (err, result) {
console.log('Private Key:', result.privateKey);
console.log('Public Key:', result.publicKey);
});
Will print something like this:
Private Key: -----BEGIN PRIVATE KEY-----
MIIEwAIBADANBgkqhkiG9w0BAQEFAASCBKowggSmAgEAAoIBAQDaj7DZpBhrUxcj
WxI/n3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