1
0
Fork 0
mirror of https://github.com/Luzifer/wasm-openssl.git synced 2024-11-09 15:20:06 +00:00
WASM wrapper around go-openssl to be used in Javascript projects
Find a file
2020-06-13 16:32:54 +02:00
example Update example / demo for new API 2020-06-13 16:23:29 +02:00
.gitignore Cleanup main dir 2018-09-18 18:42:46 +02:00
.repo-runner.yaml Disable tests during build 2020-06-13 16:32:00 +02:00
go.mod Upgrade to go-openssl v4 2020-06-13 15:44:15 +02:00
go.sum Upgrade to go-openssl v4 2020-06-13 15:44:15 +02:00
History.md prepare release v0.4.0 2020-06-13 16:32:54 +02:00
LICENSE Update META 2018-09-18 18:52:37 +02:00
main.go Extend API for the user to configure key derivation 2020-06-13 16:17:36 +02:00
Makefile Add "publish" target 2020-06-13 16:24:14 +02:00
README.md Extend API for the user to configure key derivation 2020-06-13 16:17:36 +02:00

Go Report Card

Luzifer / wasm-openssl

wasm-openssl is a WASM wrapper around go-openssl to be used in Javascript projects.

A word of warning: This relies on the experimental WASM implementation in Golang. It is working but most likely will not have its final state. When the Golang implementation of WASM changes this likely will change too. As long as the WASM implementation in Go is experimental this only serves as a proof-of-concept and maybe shouldn't be used in production!

Usage

You will need to have wasm_exec.js installed in your project to load the binary. This file can be found in golang/go repository. (Make sure the version of the file matches the version of Go used to compile the WASM file.

For an embedding example see the example folder in this repo.

If you have a top-level function opensslLoaded() defined, this will be called in the initialization of the openssl.wasm. This serves as a notification you do have now access to the library functions:

OpenSSL.decrypt = (ciphertext, passphrase, callback, hashAlgo = OpenSSL.SHA256, usePBKDF = true, iterations = 10000) => {...}
OpenSSL.encrypt = (plaintext, passphrase, callback, hashAlgo = OpenSSL.SHA256, usePBKDF = true, iterations = 10000) => {...}

The functions will not return anything in the moment as in the current state Go WASM support does not have return values. Instead the callback function you've provided will be called and always have two arguments: function callback(result, error) - The result will be the plaintext on decrypt and the ciphertext on encrypt. The error will either be null or a string containing details about the error. When an error occurred the result is null.

Examples

// Encryption with default parameters
OpenSSL.encrypt('test', 'test', (res, err) => console.log(res, err))
// U2FsdGVkX18r5Lf94A7Ng1iO03jPtKeM1hq8cZdrrww= null

// Decryption with default parameters
OpenSSL.decrypt('U2FsdGVkX18r5Lf94A7Ng1iO03jPtKeM1hq8cZdrrww=', 'test', (res, err) => console.log(res, err))
// test null

// Encryption with custom parameters for key derivation
OpenSSL.encrypt('test', 'test', (res, err) => console.log(res, err), OpenSSL.SHA1, true, 25000)
// U2FsdGVkX19HCUnnaJefLxuljrhoDpCdzDOuvIqiB9Q= null

// Decryption with the same custom parameters
OpenSSL.decrypt('U2FsdGVkX19HCUnnaJefLxuljrhoDpCdzDOuvIqiB9Q=', 'test', (res, err) => console.log(res, err), OpenSSL.SHA1, true, 25000)
// test null

// Decryption with non-matching custom parameters (leads to an error)
OpenSSL.decrypt('U2FsdGVkX19HCUnnaJefLxuljrhoDpCdzDOuvIqiB9Q=', 'test', (res, err) => console.log(res, err), OpenSSL.SHA1, true)
// null "decrypt failed: invalid padding"