openssl-browser
v2.1.0
Published
OpenSSL in the Browser and Node.js through WebAssembly, with ESM and UMD entrypoints.
Maintainers
Readme
openssl-browser
openssl-browser brings OpenSSL to the Browser and Node.js through WebAssembly, with ESM and UMD entrypoints.
OpenSSL 3.5.2 5 Aug 2025 (Library: OpenSSL 3.5.2 5 Aug 2025)
For ESM
import {opensslmod} from 'https://libs.ext-code.com/js/crypto/openssl/v2.0.0/openssl.mjs';
// var {opensslmod} = await import('https://libs.ext-code.com/js/crypto/openssl/v2.0.0/openssl.mjs');
var openssl = opensslmod();
await openssl.run('-v');
For Script
<script src='https://libs.ext-code.com/js/crypto/openssl/v2.0.0/openssl.js'></script>
<script>
(async()=>{
var openssl = opensslmod();
await openssl.run('-v');
})();
</script>
Tools
See what is exposed in both the script src and esm modules :
global state diff [ ext-code.com ]
Experiment with the package :
nodejs terminal [ ext-code.com ]
NodeJS Environment
npm i openssl-browser
(async()=>{
var opensslmod = require('openssl-browser');
var openssl = opensslmod();
await openssl.run('-v');
})();
import {opensslmod} from 'openssl-browser';
var openssl = opensslmod();
await openssl.run('-v');
Locally Hosted ( via web server )
The scripts can be hosted locally.
npm i openssl-browser
The scripts can then either be copied to a prefered location
or referenced via default install location, such as :
import {opensslmod} from '/node_modules/openssl-browser/openssl.mjs';
// var {opensslmod} = await import('/node_modules/openssl-browser/openssl.mjs');
var openssl = opensslmod();
await openssl.run('-v');
When hosting locally, you may need to specify the paths for openssl.wasm and openssl.wasm.js.
If omitted, the module defaults to loading the files from a remote server. Specifying only
openssl.wasm.js assumes that openssl.wasm is located in the same directory.
<script src='/node_modules/openssl-browser/openssl.js'></script>
<script>
(async()=>{
var wasmJs = '/node_modules/openssl-browser/openssl.wasm.js';
var openssl = opensslmod({wasmJs});
await openssl.run('-v');
})();
</script>
API
opensslmod
constructor function
opensslmod(params)params : object
std : defined : if std is defined on params, stdout and stderr are assumed to be truethy
stdout : function stdout(txt) : stdout output, optional, truethy => stdout logs to console
stderr : function stderr(txt) : stderr output, optional, truethy => stderr logs to console
wasmJs : string : location of wamsJs file, optional
wasmBinaryFile : string : location of wasm binary, optional
wasmBinary : [arrayBuffer, buffer, uint8] : provide the wasm binary itself, optional
echo : boolean : whether to echo output to console, optional, default=true
df : boolean : enable or disable debugging, optional, default=false
return
- object : openssl api
examples
var openssl = opensslmod({std:1,echo:false});
await openssl.run('-v');function stdout(){console.log.apply(console,arguments)
function stdout(){console.log.apply(console,arguments)
var openssl = opensslmod({stdout,stderr,echo:false});
await openssl.run('-v');openssl.df
enable or disable debugging
openssl.df : true|falseexamples
openssl.df = true;openssl.run
run openssl command/s, multiple arguments can be provided
async openssl.run(stringN)stringX : 'openssl [arg0] [arg1] ...'
stringX : ' [arg0] [arg1] ...'
return : {Module,code,error}
- Module : emscripten module
- code : number : error code
- error : string : error description
examples
var {Module,code,error} = await openssl.run('-v','openssl genrsa 2048 -out -');note
openssl in the command is optional
hash functions : sha1, sha256, sha512, md5, blake2b512, blake2s256, sha3-256, sha3-512
generate the hash of value
async hash(value,{src='digest.txt',dest='tmp.bin'}={})- value : uint8|string|blob : the data to be hashed
- src : string : the input filename
- dest : string : the output filename
return {digest,hash,error};
- digest / hash : string : the hash value
- error : string : error description
examples
var {digest,error} = await openssl.sha256('helloworld');
openssl.log(' sha256 :',digest);
var {digest,error} = await openssl.md5('helloworld');
openssl.log(' md5 :',digest);
var {digest,error} = await openssl['sha3-512']('helloworld');
openssl.log('sha3-512 : ',digest);
var hex = opensslmod.md5('helloworld',true);
openssl.log(hex);openssl.keypair
generate a private and public keypair in pkcs8 form
async openssl.keypair({keybits=4096,key_name='private.key',pub_name='public.key'}={})- keybits : number : the size of the key in bits, default=4096
- key_name : string : the output filename of the private key
- pub_name : string : the output filename of the public key
return {privateKey,publicKey,error}
- privateKey : string : the private key in pem form
- publicKey : string : the public key in pem form
- error : string : error description
examples
var {privateKey,publicKey,error} = await openssl.keypair();openssl.evpBytesToKey
- EVP_BytesToKey
- evpBytesToKey
- evpbytestokey
The insecure key derivation algorithm from OpenSSL.
async openssl.evpBytesToKey({password,salt=true,keylen=32,ivlen=16})- password : string : the password
- salt : true|falsey|uint8 :
- true : auto generate a salt
- falsey : do not use a salted algorithm
- uint8 : a buffer for use as the salt
- keylen :
- ivlen :
return {key,iv,salt,error}
- key : uint8 : the derived key
- iv : uint8 : the iv
- salt : uint8 : the salt, useful when auto generating a salt value
- error : string : error description
examples
var {key,salt,iv,error} = await openssl.evp_bytes_to_key({password:'123456'});
openssl.log(' key :',key,openssl.to_b64(key));
openssl.log('salt :',salt,openssl.to_b64(salt));
openssl.log(' iv :',iv,openssl.to_b64(iv));openssl.pbkdf2
pbkdf2 key derivatation
PBKDF2 (Password-Based Key Derivation Function 2) is a cryptographic algorithm used to stretch simple passwords into secure, hard-to-guess keys. It achieves this by combining a password with a random string (salt) and hashing the result tens or hundreds of thousands of times to prevent brute-force attacks.
async openssl.pbkdf2(password,salt=true,iterations=200_000,keylen=32)- password : string : the password
- salt : true | falsey | uint8
- true : auto generate salt
- falsey : use no salt algorithm
- uint8 : provide a salt
- iterations : number : number of iterations to use
- keylen : number : size of key
return {key,salt,error}
- key : uint8 : generated key
- salt : uint8 : generated salt
- error : string : error description
examples
var {key,salt,error} = await openssl.pbkdf2({password:'123456'});
openssl.log(' key :',key,openssl.to_b64(key));
openssl.log('salt :',salt,openssl.to_b64(salt));openssl.hkdf
hkdf key derivation algorithm
HKDF (HMAC-based Extract-and-Expand Key Derivation Function) is a cryptographic algorithm designed to convert a weak or generic master secret into one or more cryptographically strong, distinct keys.
async openssl.hkdf({secret,salt=true,info=false,keylen=32})- secret : string : The raw secret bytes HKDF consumes as its input.
- salt : true | falsey | uint8 : , default=true
- true : auto generate salt
- falsey : use no salt algorithm
- uint8 : provide a salt
- info : boolean : , default=false
- keylen : number : size of the key, default=32
return {key,salt,info,error}
- key : uint8 : the derived key
- salt : uint8 : the salt
- info : uint8 : the used info
- error : string : error description
examples
var {key,salt,error} = await openssl.hkdf({secret:'123456'});
openssl.log(' key :',key,openssl.to_b64(key));
openssl.log('salt :',salt,openssl.to_b64(salt));openssl.configToIni
- config_ini
convert a config json structure to an openssl ini text file
openssl.configToIni(json)- json : object|string : the configuration object
- string [ ca | server | client | codeSigning | email | ocspResponder | timestamp | ipsec | device ] : predefined default values for dev / testing
return
- string : the ini file text
examples
var config = opensslmod.config.server();
openssl.log.json(config);
var ini = openssl.config_ini(config);
openssl.log(ini);openssl.sign
create a signature for the data using a private key, privateKey and value are optional if these already exist within the file system
async openssl.sign({
privateKey,value,
key_name='private.key',file_name='file.bin',sig_name='sig.bin'
})- privateKey : string : the privateKey in pem form, optional
- value : uint8|string|blob|buffer : the data to be signed, optional
- key_name : string : the private key filename, optional, default='private.key'
- file_name : string : the name of the file on the file system, optional, default='file.bin'
- sig_name : string : the name of the signature file, optional, default='sig.bin'
return {sig,signature,error}
- sig|signature : string : the signature
- error : string : error description
examples
var js = `
console.log('helloworld');
`;
var js2 = `
console.log('worldhello');
`;
var {privateKey,publicKey} = await openssl.keypair();
var {sig,error} = await openssl.sign({privateKey,value:js});
var {result,error} = await openssl.sign.verify({publicKey,sig,value:js});
openssl.log('result',result);
var {result,error} = await openssl.sign.verify({publicKey,sig,value:js2});
openssl.log('result',result);openssl.sign.verify
verify the signature for the data using the public key, publicKey,signature,value are optional if these already exist within the filesystem
async openssl.sign.verify({
publicKey,value,signature,sig,
pub_name='public.key',file_name='file.bin',sig_name='sig.bin'
})- publicKey : string : the public key in pem format, optional
- signature|sig : string : the signature, optional
- value : uint8|string|blob : the data, optional
- pub_name : string : the filename to write the public key, optional
- file_name : string : the filename to write the data, optional
- sig_name : string : the filename of the signature file, optional
return {result,error}
- result : boolean : the result of the verification
- error : string : error description
examples
var js = `
console.log('helloworld');
`;
var js2 = `
console.log('worldhello');
`;
var {privateKey,publicKey} = await openssl.keypair();
var {sig,error} = await openssl.sign({privateKey,value:js});
var {result,error} = await openssl.sign.verify({publicKey,sig,value:js});
openssl.log('result',result);
var {result,error} = await openssl.sign.verify({publicKey,sig,value:js2});
openssl.log('result',result);openssl.cert
generate an x509 certificate
async openssl.cert({
caKey,caCert,privateKey,config,
days=365,
key_name='private.key',cert_name='cert.pem',csr_name='csr.pem',config_name='config.ini',ca_key_name='ca.key',ca_cert_name='ca.cert'
})- caKey : string : the pem encoded signing key, optional
- caCert : string : the pem encoded signing certificate, optional
- privateKey : string : the pem encoded private key for the certificate, optional
- config : string : the configuration for the certificate, optional
- days : number : the number of days the certificate is valid for, optional
- key_name : string : the filename for the private key, optional, default='private.key'
- cert_name : string : the filename for the certificate, optional, default='cert.pem'
- csr_name : string : the filename for the intermediate csr, optional, default='csr.pem'
- config_name : string : the filename for the config file, optional, default='config.ini'
- ca_key_name : string : the filename for the signing key, optional, default='ca.key'
- ca_cert_name : string : the filename of the signing cert, optional, default='ca.cert'
returns {cert,error}
- cert : string : pem encoded certificate
- error : string : error description
examples
var {privateKey,cert} = await openssl.selfsigned({config:'ca'});
var caKey = privateKey;
var caCert = cert;
var {privateKey,cert} = await openssl.cert({caKey,caCert,config:'server'});
var {result,error} = await openssl.cert.verify({caCert,cert});
openssl.log('result',result);openssl.cert.verify
verify an x509 certificate
async openssl.cert.verify({
caCert,cert,chainPem,trustedPem,
cert_name='cert.pem',ca_cert_name='ca.cert',chain_name='chain.pem',trusted_name='trusted.pem'
})- caCert : string : the pem encoded issuing cert, optional
- cert : string : the pem encoded certificate to verify, optional
- chainPem : string : the pem encoded certificate chain, optional
- trustedPem : string : pem encoded root certificates, optional
- cert_name : string : the filename of the cert, optional, default='cert.pem'
- ca_cert_name : string : the filename of the issuing cert, optional, default='ca.cert'
- chain_name : string : the filename of the chain of certificates, optional, default='chain.pem'
- trusted_name : string : the filename of the trusted root certificates, optional, default='trusted.pem'
return {result,error}
- result : boolean : result of the verification
- error : string : error description
examples
var {privateKey,cert} = await openssl.selfsigned({config:'ca'});
var caKey = privateKey;
var caCert = cert;
var {privateKey,cert} = await openssl.cert({caKey,caCert,config:'server'});
var {result,error} = await openssl.cert.verify({caCert,cert});
openssl.log('result',result);openssl.selfsigned
- selfSigned
create a selfsigned certificate, parameters are optional when the files already exist
async openssl.selfsigned({
config,privateKey,
keybits=4096,days=365,
key_name='private.key',cert_name='cert.pem',config_name='config.ini'
})- config : object : configuration information for the certificate, optional
- privateKey : string : pem encoded private key, optional
- keybits : number : the number of bits in the private key, optional, default=4096
- days : number : the number of days to issue the certificate, optional, default=365
- key_name : string : the filename of the private key, optional, default='private.key'
- cert_name : string : the filename of the certificate, optional, default='cert.pem'
- config_name : string : the filename of the ini file, optional, default='config.ini'
return {privateKey,cert,error}
- privateKey : string : pem encoded private key
- cert : string : pem encoded certificate
- error : string : error description
examples
var {privateKey,cert} = await openssl.selfsigned({config:'server'});
openssl.log(privateKey);
openssl.log(cert);openssl.encrypt
encrypt data using a private key
async openssl.encrypt({
key,value,iv=null,
cipher='aes-256-ctr',ivlen=16,
file_name='file.bin',enc_name='enc.bin'
})- key : uint8 : key buffer, only valid keylength is 32 bits, optional will auto generate
- value : uint8 | string | blob : data to be encrypted
- iv : uint8 | falsey : iv for the encryption, falsey the iv is generated, default=falsey
- cipher : string : the algorithm to use for encryption, default=aes-256-ctr
- ivlen : number : the length of the iv, default=16
- file_name : string : the name of the file to write the data, default='file.bin'
- enc_name : string : the name of the encrypted output file, default='enc.bin'
return {encrypted,iv,error}
- encrypted : uint8 : the encrypted buffer
- iv : uint8 : the iv to used
- key : uint8 : the key used
- error : string : error description
examples
var key = openssl.rndbytes(32);
var {encrypted,iv,error} = await openssl.encrypt({key,value:'helloworld'});
openssl.log('encrypted :',openssl.to_b64(encrypted));
openssl.log(' iv :',openssl.to_b64(iv));
var {decrypted,error} = await openssl.decrypt({key,iv,value:encrypted});
openssl.log.hex(decrypted);
var txt = openssl.text.decode(decrypted);
openssl.log(txt);openssl.decrypt
decrypt an encrypted buffer
async openssl.decrypt({
key,iv,value,
cipher='aes-256-ctr',
enc_name='enc.bin',dec_name='dec.bin'
})- key : uint8 : the decryption key
- iv : unit8 : the iv used to encrypt
- value : uint8 : the encrypted buffer
- cipher : string ( default=aes-256-ctr ) : the encryption algorithm used
- enc_name : string ( optional ) : the name of the encrypted file
- dec_name : string ( optional ) : the name of the decrypted file
return {decrypted,error}
- decrypted : uint8 : the decrypted buffer
- error : string : error deescription
examples
var key = openssl.rndbytes(32);
var {encrypted,iv,error} = await openssl.encrypt({key,value:'helloworld'});
openssl.log('encrypted :',openssl.to_b64(encrypted));
openssl.log(' iv :',openssl.to_b64(iv));
var {decrypted,error} = await openssl.decrypt({key,iv,value:encrypted});
openssl.log.hex(decrypted);
var txt = openssl.text.decode(decrypted);
openssl.log(txt);openssl.encrypt.password
encrypt data using a password
async openssl.encrypt.password({
password,value,
file_name='file.bin',enc_name='enc.bin'
})- password : string : the password to encrypt the data with
- value : uint8 | blob | string : the data to be encrypted
- file_name : string : the filename to write the data, default='file.bin'
- enc_name : string : the filename of the encrypted data, default='enc.bin'
return {encrypted,error}
- encrypted : uint8 : the encrypted buffer
- error : string : error description
examples
var {encrypted,error} = await openssl.encrypt.password({password:'123456',value:'hello'});
var b64 = openssl.to_b64(encrypted);
openssl.log(b64);
var {decrypted,error} = await openssl.decrypt.password({password:'123456',value:encrypted});
openssl.datatype(decrypted,true);
var txt = openssl.text.decode(decrypted);
openssl.log(txt);openssl.decrypt.password
decrypt data using a password
async openssl.decrypt.password({
password,value,
enc_name='enc.bin',dec_name='dec.bin'
})- password : string : the password used to encrypt the data
- value : uint8 | blob | string : the encrypted data
- enc_name : string : the filename of the encrypted data, default='enc.bin'
- dec_name : string : the filename of the decrypted data, default='dec.bin'
return {decrypted,error}
- decrypted : uint8 : the decrypted data
- error : string : error description
examples
var {encrypted,error} = await openssl.encrypt.password({password:'123456',value:'hello'});
var b64 = openssl.to_b64(encrypted);
openssl.log(b64);
var {decrypted,error} = await openssl.decrypt.password({password:'123456',value:encrypted});
openssl.datatype(decrypted,true);
var txt = openssl.text.decode(decrypted);
openssl.log(txt);openssl.encrypt.rsa
encrypt data using a public key
async openssl.encrypt.rsa({
publicKey,value,
pub_name='public.key',file_name='file.bin',enc_name='enc.bin'
})- publicKey : string : pem encoded public key
- value : uint8 | blob | string : the data to be encrypted
- pub_name : string : the filename of the publicKey, default='public.key'
- file_name : string : the filename of the data, default='file.bin'
- enc_name : string : the filename of the encrypted data, default='enc.bin'
return {encrypted,error}
- encrypted : uint8 : the encrypted data
- error : string : error description
examples
var {privateKey,publicKey} = await openssl.keypair();
var {encrypted,error} = await openssl.encrypt.rsa({publicKey,value:'hello'});
var b64 = openssl.to_b64(encrypted);
openssl.log(b64);
var {decrypted,error} = await openssl.decrypt.rsa({privateKey,value:encrypted});
openssl.datatype(decrypted,true);
var txt = openssl.text.decode(decrypted);
openssl.log(txt);openssl.decrypt.rsa
decrypt data using a privateKey
async openssl.decrypt.rsa({
privateKey,value,
priv_name='private.key',enc_name='enc.bin',dec_name='dec.bin'
})- privateKey : string : pem encoded privateKey, optional
- value : uint8 | blob | string : the data to be decrypted, optional
- priv_name : string : the filename of the privateKey, default='private.key'
- enc_name : string : the filename of the encrypted data, default='enc.bin'
- dec_name : string : the filename of the decrypted data, default='dec.bin'
return {decrypted,error}
- decrypted : uint8 : the decrypted data
- error : string : error description
examples
var {privateKey,publicKey} = await openssl.keypair();
var {encrypted,error} = await openssl.encrypt.rsa({publicKey,value:'hello'});
var b64 = openssl.to_b64(encrypted);
openssl.log(b64);
var {decrypted,error} = await openssl.decrypt.rsa({privateKey,value:encrypted});
openssl.datatype(decrypted,true);
var txt = openssl.text.decode(decrypted);
openssl.log(txt);openssl.encrypt.pkcs1
encrypt data using pkcs1. Legacy command — PKCS#1 v1.5 (old, deprecated, unsafe)
async openssl.encrypt.pkcs1({
publicKey,value,
pub_name='public.key',file_name='file.bin',enc_name='enc.bin'
})- publicKey : string : pem encoded publicKey, optional
- value : uint8 | blob | string : data to be encrypted, optional
- pub_name : string : filename of publicKey, default='public,key'
- file_name : string : filename of data, default='file.bin'
- enc_name : string : filename of encrypted data, default='enc.bin'
return {encrypted,error}
- encrypted : uint8 : the encrypted data
- error : string : error description
examples
var {privateKey,publicKey} = await openssl.keypair();
var {encrypted,error} = await openssl.encrypt.pkcs1({publicKey,value:'hello'});
var b64 = openssl.to_b64(encrypted);
openssl.log(b64);
var {decrypted,error} = await openssl.decrypt.pkcs1({privateKey,value:encrypted});
openssl.datatype(decrypted,true);
var txt = openssl.text.decode(decrypted);
openssl.log(txt);openssl.decrypt.pkcs1
decrypt data using pkcs1
async openssl.decrypt.pkcs1({
privateKey,value,
priv_name='private.key',enc_name='enc.bin',dec_name='dec.bin'
})- privateKey : string : pem encoded private key, optional
- value : uint8 | blob | string : the data to be decrypted, optional
- priv_name : string : the filename for the privateKey, default='private.key'
- enc_name : string : the filename for the encrypted data, default='enc.bin'
- dec_name : string : the filename for the decrypted data, default='dec.bin'
return {decrypted,error}
- decrypted : uint8 : the decrypted data
- error : string : error description
examples
var {privateKey,publicKey} = await openssl.keypair();
var {encrypted,error} = await openssl.encrypt.pkcs1({publicKey,value:'hello'});
var b64 = openssl.to_b64(encrypted);
openssl.log(b64);
var {decrypted,error} = await openssl.decrypt.pkcs1({privateKey,value:encrypted});
openssl.datatype(decrypted,true);
var txt = openssl.text.decode(decrypted);
openssl.log(txt);openssl.encrypt.elipticCurve
- ec
encrypt data using eliptic curve
async openssl.encrypt.elipticCurve({
cert,value,
cert_name='cert.pem',file_name='file.bin',enc_name='enc.bin'
})- cert : string : pem encoded certificate, optional
- value : uint8 | blob | string : the data to be encrypted, optional
- cert_name : sting : the filename for the cert
- file_name : string : the filename for the data
- enc_name : string : the filename of the encrypted data
return {encrypted,error}
- encrypted : uint8 : the encrypted data
- error : string : error description
examples
var {privateKey,cert} = await openssl.selfSigned({config:'client'});
var {encrypted,error} = await openssl.encrypt.elipticCurve({cert,value:'hello'});
var b64 = openssl.to_b64(encrypted);
openssl.log(b64);
var {decrypted,error} = await openssl.decrypt.elipticCurve({privateKey,value:encrypted});
openssl.datatype(decrypted,true);
var txt = openssl.text.decode(decrypted);
openssl.log(txt);openssl.decrypt.elipticCurve
- openssl.decrypt.ec
decrypt data using eliptic curve
async openssl.decrypt.elipticCurve({
privateKey,value,
priv_name='pivate.key',enc_name='enc.bin',dec_name='dec.bin'
})- privateKey : string : pem encoded private key, optional
- value : uint8 | blob | string : the data to be decrypted, optional
- priv_name : stirng : the filename of the private key
- enc_name : string : the filename of the encrypted data
- dec_name : string : the filename of the decrypted data
return {decrypted,error}
- decrypted : uint8 : the decrypted data
- error : string : error description
examples
var {privateKey,cert} = await openssl.selfSigned({config:'client'});
var {encrypted,error} = await openssl.encrypt.elipticCurve({cert,value:'hello'});
var b64 = openssl.to_b64(encrypted);
openssl.log(b64);
var {decrypted,error} = await openssl.decrypt.elipticCurve({privateKey,value:encrypted});
openssl.datatype(decrypted,true);
var txt = openssl.text.decode(decrypted);
openssl.log(txt);openssl.encrypt.smime
encrypt data using smime ( cert / privateKey )
async openssl.encrypt.smime({
cert,value,
outform='PEM',
cert_name='cert.pem',file_name='file.bin',enc_name='enc.bin'
})- cert : uint8 | blob | string : the certificate, optional
- value : uint8 | blob | string : the data to be encrypted, optional
- outform : string : the output format
- cert_name : string : the filename of the certificate
- file_name : string : the filename of the data
- enc_name : string : the filename of the encrypted data
return {encrypted,error}
- encrypted : uint8 : the encrypted data
- error : string : error description
examples
var {privateKey,cert} = await openssl.selfSigned({config:'client'});
var {encrypted,error} = await openssl.encrypt.smime({cert,value:'hello'});
var b64 = openssl.to_b64(encrypted);
openssl.log(b64);
var {decrypted,error} = await openssl.decrypt.smime({privateKey,value:encrypted});
openssl.datatype(decrypted,true);
var txt = openssl.text.decode(decrypted);
openssl.log(txt);async openssl.decrypt.smime
decrypt data using smime ( cert / privateKey )
async openssl.decrypt.smime({
privateKey,value,
inform='PEM',
priv_name='private.key',enc_name='enc.bin',dec_name='dec.bin'
})- privateKey : uint8 | blob | string : pem encoded private key, optional
- value : uint8 | blob | string : the data to be decrypted, optional
- inform : string [ PEM | DER ] : the output format
- priv_name : string : the filename for the private key
- enc_name : string : the filename for the encrypted data
- dec_name : string : the filename for the decrypted data
return {decrypted,error}
- decrypted : uint8 : the decrypted data
- error : string : error description
examples
var {privateKey,cert} = await openssl.selfSigned({config:'client'});
var {encrypted,error} = await openssl.encrypt.smime({cert,value:'hello'});
var b64 = openssl.to_b64(encrypted);
openssl.log(b64);
var {decrypted,error} = await openssl.decrypt.smime({privateKey,value:encrypted});
openssl.datatype(decrypted,true);
var txt = openssl.text.decode(decrypted);
openssl.log(txt);openssl.readfile
- openssl.readFile
read a file from the emscripten filesystem
openssl.readfile(name,{Module}={})- name : string : filename
- Module : emscripten module, optional
return {uint8,error}
- uint8 : uint8array : the file data
- error : string : a string describing the error
examples
var {uint8,error} = openssl.readfile('private.key');openssl.readfile.uint8
read a file from the emscripten filesystem
openssl.readfile.uint8(name,{Module}={})- name : string : filename
- Module : emscripten module, optional
return {uint8,error}
- uint8 : uint8array : the file data
- error : string : a string describing the error
examples
var {uint8,error} = openssl.readfile.uint8('private.key');openssl.readfile.txt
read a file from the emscripten filesystem and automatically return the text form
openssl.readfile.txt(name,{Module}={})- name : string : filename
- Module : emscripten module, optional
return {txt,error}
- txt : string : the file data
- error : string : a string describing the error
examples
var {txt,error} = openssl.readfile.txt('private.key');openssl.readfile.blob
read a file from the emscripten filesystem and automatically return the blob form
openssl.readfile.blob(name,{Module}={})- name : string : filename
- Module : emscripten module, optional
return {blob,error}
- blob : blob : the file data
- error : string : a string describing the error
examples
var {blob,error} = openssl.readfile.blob('private.key');openssl.writefile
- writeFile
write a file to the emscripten filesystem
async openssl.writefile(name,value,{Module}={})- name : string : filename
- value : uint8|string|blob|buffer : the file data
return {ok,error}
- ok : string : ok
- error : string : error description
examples
var {ok,error} = await openssl.writefile('private.key','----- BEGIN PRIVATE KEY -----');openssl.writefile.uint8
write a file to the emscripten filesystem, providing the data in uint8 form
openssl.writefile.uint8(name,uint8,{Module}={})- name : string : filename
- uint8 : uint8array : file data
return {ok,error}
- ok : string : ok
- error : string : error description
examples
var uint8 = new Uint8Array();
var {ok,error} = openssl.writefile.uint8('private.key',uint8);openssl.writefile.blob
write a file to the emscripten filesystem, providing the data in blob form
async openssl.writefile.blob(name,blob,{Module}={})- name : string : filename
- blob : blob : file data
return {ok,error}
- ok : string : ok
- error : string : error description
examples
var blob = new Blob(['----- BEGIN RIVATE KEY -----');
var {ok,error} = openssl.writefile.blob('private.key',blob);openssl.writefile.txt
write a file to the emscripten filesystem, providing the data in blob form
openssl.writefile.txt(name,txt,{Module}={})- name : string : filename
- txt : string : file data
- Module : emscripten module, optional
return {ok,error}
- ok : string : ok
- error : string : error description
examples
var {ok,error} = openssl.writefile.txt('private.key','----- BEGIN PRIVATE KEY -----');openssl.writefile.config
convience function to write a config json structure to the file system, it converts it to an .ini file first
openssl.writefile.config(name,config,{Module}={})- name : string : filename
- config : json : the configuration information
- Module : emscripten module, optional
return {ok,error}
- ok : string : ok
- error : string : error description
examples
var config = openssl.config.server();
var {ok,error} = openssl.writefile.config('config.ini',config);openssl.download
helper function for browser to download blob from v with optional mime type
openssl.download(v,{type,name})- v : any : blob to be constructed for download
- type : string : mime type to be set for blob, optional
- name : string : the filename, optional
return
- undefined : no return value
examples
var {opensslmod} = await import('https://libs.ext-code.com/js/crypto/openssl/v2.0.0/openssl.mjs');
var openssl = opensslmod();
await openssl.init();
await openssl.selfsigned({config:'server',cert_name:'server.pem'});
var {txt,error} = openssl.readfile.txt('server.pem');
openssl.log(txt);
openssl.download(txt,{name:'server.pem'});openssl.download.file
helper funciton to download file from emscriptens file system
openssl.download.file(name)- name - string : path of the file to be downloaded
return {error}
- error : string : error description
examples
var {opensslmod} = await import('https://libs.ext-code.com/js/crypto/openssl/v2.0.0/openssl.mjs');
var openssl = opensslmod();
await openssl.init();
await openssl.selfsigned({config:'server',cert_name:'server.pem'});
openssl.download.file('server.pem');openssl.download.blob
helper function to download blob
openssl.download.blob(blob)- blob : blob : the blob to be downloaded
return undefined
- undefined : no return value
examples
var {opensslmod} = await import('https://libs.ext-code.com/js/crypto/openssl/v2.0.0/openssl.mjs');
var openssl = opensslmod();
await openssl.init();
var {privateKey,publicKey,error} = await openssl.keypair();
var blob = new Blob([privateKey]);
openssl.download.blob(blob,'private.key');openssl.FS
exposes emscripten file system API :
File System API [emscripten.org]
note
not to be confused with openssl.fs ( lowercase )
openssl.fs.snapshot
create a snapshot of the file system
openssl.fs.snapshot({path='/',Module}={})- path : string : where to take the snapshot from
- Module : emscripten Module : the emscripten Module to take the snapshot from, optional
return
- snapshot : snapshot of the file system
examples
var {opensslmod} = await import('https://libs.ext-code.com/js/crypto/openssl/v2.0.0/openssl.mjs');
var openssl = opensslmod();
await openssl.init();
await openssl.writefile('hello.txt','hello');
var snapshot = openssl.fs.snapshot();
console.log('snapshot',snapshot);openssl.fs.restore
restore a snapshot
openssl.fs.restore({path='/',snapshot,Module})- path : string : where to restore the snapshot
- snapshot : snapshot : the snapshot to restore
- Module : emscripten Module : the module to restore the snapshot on, uses the current module if not specified, optional
return no return value
- undefined
examples
var {opensslmod} = await import('https://libs.ext-code.com/js/crypto/openssl/v2.0.0/openssl.mjs');
var openssl = opensslmod();
await openssl.init();
await openssl.writefile('hello.txt','hello');
var snapshot = openssl.fs.snapshot();
console.log('snapshot',snapshot);
var opensslmod2 = openssl.new();
var openssl2 = opensslmod2();
await openssl.init();
openssl.fs.restore({snapshot});
var {txt,error} = openssl.readfile.txt('hello.txt');
console.log(txt);openssl.fs.diff
determine the difference of files between snapshot1 and snapshot 2
openssl.fs.diff(snaphot1,snapshot2)- snapshot1 a filesystem snapshot
- snapshot2 a file system snapshot
return
- object : keys are file paths, values are file data
examples
var {opensslmod} = await import('https://libs.ext-code.com/js/crypto/openssl/v2.0.0/openssl.mjs');
var openssl = opensslmod();
await openssl.init();
await openssl.writefile('hello.txt','hello');
var snap1 = openssl.fs.snapshot();
await openssl.writefile('world.txt','world');
var snap2 = openssl.fs.snapshot();
var list = openssl.fs.diff(snap1,snap2);
Object.keys(list).forEach((key,i)=>console.log(i,key,openssl.datatype(list[key])));openssl.fs.save
save the current snapshot
openssl.fs.save()- no parameters
return snapshot
- object
examples
var {opensslmod} = await import('https://libs.ext-code.com/js/crypto/openssl/v2.0.0/openssl.mjs');
var openssl = opensslmod();
await openssl.writefile('hello.txt','hello');
openssl.fs.save();openssl.fs.set
set the initial snapshot
openssl.fs.set(snapshot)- snapshot : object : the snapshot to set, optional, use the current snapshot if not set
return snapshot
- object
examples
var {opensslmod} = await import('https://libs.ext-code.com/js/crypto/openssl/v2.0.0/openssl.mjs');
var openssl = opensslmod();
var {privateKey,publicKey,error} = await openssl.keypair();
openssl.fs.set();
var list = openssl.fs.complete();
Object.keys(list).forEach(key=>console.log(key,openssl.datatype(list[key])));openssl.fs.complete
determine the difference of files between initial snapshot and current snapshot
openssl.fs.complete()return
- object : keys are file paths, values are file data
examples
var {opensslmod} = await import('https://libs.ext-code.com/js/crypto/openssl/v2.0.0/openssl.mjs');
var openssl = opensslmod();
var {privateKey,publicKey,error} = await openssl.keypair();
var list = openssl.fs.complete();
Object.keys(list).forEach(key=>console.log(key,openssl.datatype(list[key])));openssl.fs.clear
clear the snapshot system
openssl.fs.clear()return no return value
- undefined
examples
var {opensslmod} = await import('https://libs.ext-code.com/js/crypto/openssl/v2.0.0/openssl.mjs');
var openssl = opensslmod();
var {privateKey,publicKey,error} = await openssl.keypair();
openssl.fs.clear();
var list = openssl.fs.complete();
Object.keys(list).forEach(key=>console.log(key,openssl.datatype(list[key])));openssl.text.encode
encode text into buffer
openssl.text.encode(v)- v : string : the text to be encoded
return
- uint8 : the encoded text
examples
var uint8 = openssl.text.encode('helloworld');openssl.text.decode
decode a buffer into text
openssl.text.decode(v)- v : uint8 | arrayBuffer : the text to be decoded
return
- string : the decoded text
examples
await openssl.keypair();
var {uint8:privateKey,error} = openssl.readfile('private.key');
openssl.log.hex(privateKey);
var txt = openssl.text.decode(privateKey);
openssl.log(txt);openssl.to_uint8
convert value to uint8array
async openssl.to_uint8(v,type)- v : uint8 | string | arraybuffer : the value to be converted to uint8array
- type : string : used to specify the datatype of the value to be converted ( 'bin' for binary string ), optional
- bin : binary string
return
- uint8array : the converted value
examples
var uint8 = await openssl.to_uint8('hello');
openssl.datatype(uint8,true);openssl.from_uint8
convert uint8array to another type
openssl.from_uint8(uint8,type)- uint8 : uint8 : the source uint8array
- type : string [ 'blob' | 'string' ] : the type to convert the uint8array to
return
- blob : converted blob
- string : converted string
examples
var txt = 'hello';
var uint8 = openssl.text.encode(txt);
var txt = openssl.from_uint8(uint8,'string');
openssl.log(txt);openssl.to_hex
convert buffer to hex string
openssl.to_hex(uint8)- uint8 : uint8 : the buffer to be hex encoded
return
- string : the hex encoded string
examples
var txt = 'hello';
var uint8 = openssl.text.encode(txt);
openssl.log.hex(uint8);
var hex = openssl.to_hex(uint8);
openssl.log(hex);
var uint8 = openssl.from_hex(hex);
openssl.log.hex(uint8);
var txt = openssl.text.decode(uint8);
openssl.log(txt);openssl.from_hex
decode hex string to buffer
openssl.from_hex(hex)- hex : string : the hex encoded string
return
- uint8 : the hex decoded buffer
examples
var txt = 'hello';
var uint8 = openssl.text.encode(txt);
var hex = openssl.to_hex(uint8);
openssl.log(hex);
var uint8 = openssl.from_hex(hex);
var txt = openssl.text.decode(uint8);
openssl.log(txt);openssl.to_blob
convert value to blob
openssl.to_blob(v)- v : uint8 | string | blob | arrayBuffer : the value to be converted
- log_flag : truethy | true : automatically output to stdout and console.log if echo is set, true just console.log regardless of echo
return blob
- blob : the converted blob
examples
var v = 'helloworld';
var blob = openssl.to_blob(v);
openssl.datatype(blob,true);openssl.from_blob
convert blob to another value
async openssl.from_blob(blob,type='uint8')- blob : blob : the blob to be converted
- type : string [ 'string' | 'uint8' ] : the type for the blob to be converted to
return
the converted value
examples
var blob = new Blob(['helloworld']);
var txt = await openssl.from_blob(blob,'string');
openssl.log(txt);openssl.to_b64
encode buffer as base64
openssl.to_b64(v,type)- v : uint8 : the buffer to be encoded
return
- string : the base64 text
examples
var txt = 'helloworld';
var uint8 = openssl.text.encode(txt);
openssl.log.hex(uint8);
var b64 = openssl.to_b64(uint8);
openssl.log(b64);
var uint8 = openssl.from_b64(b64);
openssl.log.hex(uint8);openssl.from_b64
decode base64 to buffer
openssl.from_b64(b64,type='uint8')- b64 : string : the base64
- type : string : the return type
return
- uint8 : the decoded base64
exaples
var txt = 'helloworld';
var uint8 = openssl.text.encode(txt);
openssl.log.hex(uint8);
var b64 = openssl.to_b64(uint8);
openssl.log(b64);
var uint8 = openssl.from_b64(b64);
openssl.log.hex(uint8);openssl.normalise_pem
- openssl.norm_pem(pem)
helper function to normalise a pem certificate contained in a string, helps with embedding pem encoding certs in code.
Outputs a string that tries to be recognisable by openssl.
openssl.normalise_pem(pem)- pem : string : pem encoded key / cert
return : string
- string : normalised pem
examples
var {opensslmod} = await import('https://libs.ext-code.com/js/crypto/openssl/v2.0.0/openssl.mjs');
var openssl = opensslmod();
(()=>{
var pem = `
-----BEGIN CERTIFICATE-----
MIIGBDCCA+ygAwIBAgIUUvpdPtk+/sKOzb388cnfFYgcLOQwDQYJKoZIhvcNAQEL
...
QoHwT4dP9Bc=
-----END CERTIFICATE-----
`;
pem = openssl.norm_pem(pem);
openssl.log(pem);
})(); openssl.datatype
helper function to return the datatype of v
openssl.datatype(v)- v - any : the parameter whose datatype needs to be determined
return
- string : the datatype
examples
var {opensslmod} = await import('https://libs.ext-code.com/js/crypto/openssl/v2.0.0/openssl.mjs');
var openssl = opensslmod();
var a = [];
var type = openssl.datatype(a);
console.log(type);openssl.rndbytes
generate a buffer of random values
openssl.rndbytes(n=32)- n : number : the number of bytes
return
- uint8array : the random bytes
examples
var uint8 = openssl.rndbytes();openssl.configname
- name = [ ca | server | client | codeSigning | email | ocspResponder | timestmap | ipsec | device ]
generate a template config object
openssl.config[name]()return
- object : configuration object
examples
var config = openssl.config.ca();
openssl.log.json(config);openssl.log
helper function to log to the console and stdout, uses echo flag and stdout paramseter
openssl.log(argN)- argN : any : sequence of arguments to be logged
return no return value
- undefined
examples
openssl.log('helloworld');openssl.log.json
helper function to perform JSON.stringify then log
openssl.log.json(v)- v : any : the value to be logged
return no return value
- undefined
examples
openssl.log.json([1,2,3]);openssl.log.hex
helper function to log a hex dump of value
openssl.log.hex(v)- v : uint8 : the value to be hex dumped
return no return value
- undefined
examples
var uint8 = [1,2,3];
openssl.log.hex(uint8);Further Reading
https://docs.openssl.org/master/
https://emscripten.org/docs/api_reference/Filesystem-API.html
See Also
https://www.npmjs.com/package/node-forge-browser
https://ext-code.com/utils/x509/index.html
https://ext-code.com/utils/x509/generate-https-certificate/generate-https-certificate.html
https://ext-code.com/utils/x509/certificate-info/certificate-info.html
Examples
Browser
<script type=module>
import {opensslmod} from 'https://libs.ext-code.com/js/crypto/openssl/v2.0.0/openssl.mjs';
var stdout = console.log.bind(console,'[ stdout ]');
var stderr = console.log.bind(console,'[ stderr ]');
var openssl = opensslmod({stdout,stderr,echo:false});
await openssl.run('openssl -v');
await openssl.run('openssl genrsa -out test.key 2048');
var key = openssl.FS.readFile('test.key',{encoding:'utf8'});
console.log(key);
await openssl.run('req -new -x509 -key test.key -out test.pem -subj /CN=webcontainer-test');
var cert = openssl.FS.readFile('test.pem',{encoding:'utf8'});
console.log(cert);
var result = openssl.fs.complete();
console.log(result);
// openssl.fs.download.file('test.pem');
// openssl.fs.download(cert,'cert.pem');
</script>
Browser
<script type=module>
var {opensslmod} = await import('https://libs.ext-code.com/js/crypto/openssl/v2.0.0/openssl.mjs');
var stdout = console.log.bind(console,'[ stdout ]');
var stderr = console.log.bind(console,'[ stderr ]');
var openssl = opensslmod({stdout,stderr,echo:false});
await openssl.run('openssl -v');
await openssl.run(
'openssl genrsa -out test.key 2048',
'openssl req -new -x509 -key test.key -out test.pem -subj /CN=webcontainer-test'
);
var key = openssl.FS.readFile('test.key',{encoding:'utf8'});
console.log(key);
var cert = openssl.FS.readFile('test.pem',{encoding:'utf8'});
console.log(cert);
var result = openssl.fs.complete();
console.log(result);
// openssl.fs.download.file('test.pem');
// openssl.fs.download(cert,'cert.pem');
</script>
Browser
<script src='https://libs.ext-code.com/js/crypto/openssl/v2.0.0/openssl.js'></script>
<script>
(async()=>{
var openssl = opensslmod();
await openssl.run('openssl -v');
await openssl.run('openssl genrsa -out test.key 2048');
var key = openssl.FS.readFile('test.key',{encoding:'utf8'});
console.log(key);
await openssl.run('req -new -x509 -key test.key -out test.pem -subj /CN=webcontainer-test');
var cert = openssl.FS.readFile('test.pem',{encoding:'utf8'});
console.log(cert);
var result = openssl.fs.complete();
console.log(result);
// openssl.fs.download.file('test.pem');
// openssl.fs.download(cert,'cert.pem');
})();
</script>
NodeJS
npm i openssl-browser
import {opensslmod} from 'openssl-browser';
console.log('test.node.mjs');
var openssl = opensslmod({echo:true,df:false});
await openssl.run('-v');
var {hash,error} = await openssl.sha512('helloworld');
console.log('sha512 : ');
console.log(hash);
NodeJS
npm i openssl-browser
(async()=>{
console.log('test.node.js');
var opensslmod = require('openssl-browser');
var openssl = opensslmod({echo:true});
await openssl.run('-v');
var {privateKey,publicKey} = await openssl.generateKeyPair();
console.log('privateKey :');
console.log(privateKey);
console.log('publicKey :');
console.log(publicKey);
})();
Browser
<style>#output{font-family:monospace}</style>
<h3>openssl-browser</h3>
<pre id=output></pre>
<script type=module>
console.clear();
console.log('http-local.html');
var {opensslmod} = await import('https://libs.ext-code.com/js/crypto/openssl/v2.0.0/openssl.mjs');
var stdout = txt=>output.append('[ stdout ] ',txt,'\n');
var stderr = txt=>output.append('[ stderr ] ',txt,'\n');
var openssl = opensslmod({stdout,stderr});
openssl.df = true;
// create the emscripten filesystem for this example
await openssl.init();
var txt = `
[ req ]
distinguished_name = req_distinguished_name
x509_extensions = v3_ca
prompt = no
[ req_distinguished_name ]
C = GB
ST = England
L = Sheffield
O = My Organisation
OU = Certificate Authority
CN = My Test CA
emailAddress = [email protected]
[ v3_ca ]
basicConstraints = critical, CA:TRUE
keyUsage = critical, keyCertSign, cRLSign
subjectKeyIdentifier = hash
nameConstraints = critical, @nc
[ nc ]
permitted;DNS.1 = example.com
permitted;DNS.2 = .example.com
permitted;DNS.3 = .internal.local
excluded;DNS.1 = .malicious.com
permitted;IP.1 = 192.168.0.0/255.255.0.0
permitted;IP.2 = 10.0.0.0/255.0.0.0
`;
openssl.FS.writeFile('ca-ext.ini',txt);
await openssl.run(
'openssl genrsa -out ca.key 2048',
'openssl req -new -x509 -key ca.key -out ca.cert -days 365 -config ca-ext.ini -extensions v3_ca',
'openssl x509 -in ca.cert -text -noout'
);
var txt = openssl.FS.readFile('ca.key',{encoding:'utf8'});
stdout('ca.key');
stdout(txt);
var txt = openssl.FS.readFile('ca.cert',{encoding:'utf8'});
stdout('ca.cert');
stdout(txt);
var txt = `
[ req ]
distinguished_name = req_distinguished_name
req_extensions = v3_req
prompt = no
[ req_distinguished_name ]
C = GB
ST = England
L = Sheffield
O = My Organisation
OU = Web Services
CN = example.com
[ v3_req ]
basicConstraints = CA:FALSE
keyUsage = digitalSignature, keyEncipherment
extendedKeyUsage = serverAuth, clientAuth
subjectAltName = @san
[ san ]
DNS.1 = example.com
DNS.2 = www.example.com
DNS.2 = api.example.com
IP.1 = 192.168.1.10
IP.2 = 10.0.0.5
`;
openssl.FS.writeFile('ext.ini',txt);
await openssl.run(
'openssl genrsa -out server.key 2048',
'openssl req -new -key server.key -out server.csr -config ext.ini',
'openssl x509 -req -in server.csr -CA ca.cert -CAkey ca.key -CAcreateserial -out server.cert -days 365 -extensions v3_req -extfile ext.ini',
'openssl x509 -in server.cert -text -noout'
);
var txt = openssl.FS.readFile('server.key',{encoding:'utf8'});
stdout('server.key');
stdout(txt);
var txt = openssl.FS.readFile('server.cert',{encoding:'utf8'});
stdout('server.cert');
stdout(txt);
stdout('verify');
await openssl.run(
'openssl verify -CAfile ca.cert server.cert'
);
</script>
note : locally hosted
npm i openssl-browser
then run a local web server
<style>#output{font-family:monospace}</style>
<h3>openssl-browser</h3>
<pre id=output></pre>
<script type=module>
console.clear();
console.log('http-local.html');
import {opensslmod} from '/node_modules/openssl-browser/openssl.mjs';
var stdout = txt=>output.append('[ stdout ] ',txt,'\n');
var stderr = txt=>output.append('[ stderr ] ',txt,'\n');
var wasmJs = '/node_modules/openssl-browser/openssl.wasm.js';
var openssl = opensslmod({stdout,stderr,wasmJs});
openssl.df = true;
// create the emscripten filesystem for this example
await openssl.init();
var txt = `
[ req ]
distinguished_name = req_distinguished_name
x509_extensions = v3_ca
prompt = no
[ req_distinguished_name ]
C = GB
ST = England
L = Sheffield
O = My Organisation
OU = Certificate Authority
CN = My Test CA
emailAddress = [email protected]
[ v3_ca ]
basicConstraints = critical, CA:TRUE
keyUsage = critical, keyCertSign, cRLSign
subjectKeyIdentifier = hash
nameConstraints = critical, @nc
[ nc ]
permitted;DNS.1 = example.com
permitted;DNS.2 = .example.com
permitted;DNS.3 = .internal.local
excluded;DNS.1 = .malicious.com
permitted;IP.1 = 192.168.0.0/255.255.0.0
permitted;IP.2 = 10.0.0.0/255.0.0.0
`;
openssl.FS.writeFile('ca-ext.ini',txt);
await openssl.run(
'openssl genrsa -out ca.key 2048',
'openssl req -new -x509 -key ca.key -out ca.cert -days 365 -config ca-ext.ini -extensions v3_ca',
'openssl x509 -in ca.cert -text -noout'
);
var txt = openssl.FS.readFile('ca.k