WebAssembly port of OpenSSL
npm install openssl.jsQuick Start
=
#### ES6 Module (main)
``javascript
import { OpenSSL } from "../dist/openssl.js";
import { fileURLToPath } from "url";
import { dirname } from "path";
import path from "path";
import fs from "fs";
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
(async function() {
let rootDir = path.resolve(__dirname, "./sandbox");
let openSSL = new OpenSSL({ fs, rootDir });
let result1 = await openSSL.runCommand("genrsa -out /private.pem");
let result2 = await openSSL.runCommand("rsa -in /private.pem -pubout");
})();
`
Installation
=
OpenSSL.js is available through npm
``
npm install openssl.js
Usage
=
The main export exposes the OpenSSL class; the constructor takes two arguments in an args object:
- fs: Use either the node builtin fs or an API compatible version like @wasmer/wasmfs (which uses memfs), or BrowserFSrootDir
- : The path in the file system to map to the root (/) of the OpenSSL.js instance to use for file IO
The instance exposes a single method, runCommand, which accepts a string containing commands to be run against the OpenSSL command line interface.
Examples
=
javascript
const { OpenSSL } = require('../dist/openssl.cjs');
const { resolve } = require('path');
const fs = require('fs');(async function () {
let rootDir = resolve(__dirname, "./sandbox");
let openSSL = new OpenSSL({ fs, rootDir });
let result1 = await openSSL.runCommand("genrsa -out /private.pem");
let result2 = await openSSL.runCommand("rsa -in /private.pem -pubout");
})();
`$3
`javascript
import { OpenSSL } from "../dist/browser.openssl.js";
/* @wasmer/wasmfs /
import WasmFs from "./wasmfs.esm.js";const wasmFs = new WasmFs();
(async function() {
let openSSL = new OpenSSL({
fs: wasmFs.fs,
rootDir: "/",
wasmBinaryPath: "../dist/openssl.wasm"
});
let result1 = await openSSL.runCommand(
"genpkey -algorithm Ed25519 -out /private.pem"
);
let pK = wasmFs.fs.readFileSync("/private.pem", { encoding: "utf8" });
console.log(pK);
document.body.innerText = pK;
})();
``