https://github.com/jwerle/hidden-machine
Compile JavaScript into a native addon for Node.js secured by libsodium
https://github.com/jwerle/hidden-machine
addon encrypted native node secretbox sodium
Last synced: 10 months ago
JSON representation
Compile JavaScript into a native addon for Node.js secured by libsodium
- Host: GitHub
- URL: https://github.com/jwerle/hidden-machine
- Owner: jwerle
- License: mit
- Created: 2019-03-15T19:07:24.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2019-03-20T22:26:27.000Z (almost 7 years ago)
- Last Synced: 2025-04-14T19:06:57.564Z (10 months ago)
- Topics: addon, encrypted, native, node, secretbox, sodium
- Language: JavaScript
- Size: 115 KB
- Stars: 39
- Watchers: 5
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
hidden-machine
==============
Compile JavaScript into a native addon for Node.js secured by libsodium
## Install
```sh
$ npm install hidden-machine
```
## Usage
In your package.json, add the following `"prepublish"` script:
```json
...
"main": "index.js",
"scripts": {
"install": "node-gyp-build",
"prepublish": "hidden-machine lib/index.js -o . --key=$SHARED_APP_KEY"
}
...
```
where `lib/` is the entry path to your module code `./` is the
output directory for a C file and a `binding.gyp` file, and `index.js`
exports look something like:
```js
module.exports = (key) => require('node-gyp-build')(__dirname).initialize(key)
```
and the calling code calls `require('your-hidden-module')(Buffer.from(sharedApplicationKey, 'hex'))`.
where `sharedApplicationKey` can be generated with by running the following:
```sh
$ hidden-machine --keygen ## use --json for JSON output
...
hidden-machine: publicKey= f4e6a97ae2a2d568de4f9b4144e736cd71ab338881cee6c44a48ced4cd66b504
hidden-machine: secretKey= 3f9632bc7dc125fe399ed2f7739233274428bc96bdb8ece9ba5937a988fb5b4df4e6a97ae2a2d568de4f9b4144e736cd71ab338881cee6c44a48ced4cd66b504
hidden-machine: secret= 211536a908a2e650f00c38867c9a4696a1d073323a0c2ac6a1a8f4012265f82a
hidden-machine: key= 3f9632bc7dc125fe399ed2f7739233274428bc96bdb8ece9ba5937a988fb5b4d
```
and can be used when publishing or packing your module:
```sh
$ SHARED_APP_KEY=3f9632bc7dc125fe399ed2f7739233274428bc96bdb8ece9ba5937a988fb5b4d npm publish # or pack
```
## Example
```js
const sharedApplicationKey = generateSharedApplicationKey()
const keyPair = getKeyPair()
require('hidden-machine')('/path/to/input.js', {
publicKey: keyPair.publicKey,
secretKey: keyPair.secretKey,
key: sharedApplicationKey,
}).then((result) => {
for (const step of result.steps) {
if ('Compiler' === step.name) {
console.log(step.output)
}
}
})
```
### Example Generated `binding.gyp`
```gyp
{ # auto generated: Tue Mar 19 2019 19:47:54 GMT-0400 (Eastern Daylight Time)
"targets": [{
"target_name": "module",
"sources": [
"module.c"
],
"variables": {
"target_arch%": ""
},
"cflags": [
"