Skip to content

Instantly share code, notes, and snippets.

@mafintosh
Last active November 26, 2022 00:03
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mafintosh/63af4513181513d375b3751b9740e870 to your computer and use it in GitHub Desktop.
Save mafintosh/63af4513181513d375b3751b9740e870 to your computer and use it in GitHub Desktop.
Showcasing bundled prebuilds with different runtimes
var electron = require('electron')
electron.app.on('ready', function () {
var win = new electron.BrowserWindow({
width: 800,
height: 600
})
win.loadURL('file://' + __dirname + '/index.html')
win.toggleDevTools()
})
<html>
<head>
</head>
<body>
<script>
require('./index.js')
</script>
</body>
</html>
console.log('Requiring sodium-native, a native module')
var sodium = require('sodium-native')
var message = new Buffer('Hello, World!')
var key = new Buffer(sodium.crypto_secretbox_KEYBYTES)
var nonce = new Buffer(sodium.crypto_secretbox_NONCEBYTES)
var cipher = new Buffer(sodium.crypto_secretbox_MACBYTES + message.length)
sodium.randombytes_buf(key)
sodium.randombytes_buf(nonce)
sodium.crypto_secretbox_easy(cipher, message, nonce, key)
console.log('Encrypted message: ' + cipher.toString('hex'))
var plainText = new Buffer(cipher.length - sodium.crypto_secretbox_MACBYTES)
sodium.crypto_secretbox_open_easy(plainText, cipher, nonce, key)
console.log('Decrypted message: ' + plainText.toString())
{
"name": "prebuild-example",
"version": "0.0.0",
"description": "Example that show cases bundled prebuilds",
"dependencies": {
"electron": "^1.4.15",
"sodium-native": "^1.3.0"
},
"devDependencies": {},
"scripts": {
"node": "node index.js",
"electron": "electron index.js",
"electron-renderer": "electron app.js"
},
"author": "Mathias Buus",
"license": "MIT"
}
# run it with node
npm run node
# run it with electron
npm run electron
# run it inside the electron render process
npm run electron-renderer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment