Last active
March 20, 2018 10:14
-
-
Save hbi-wallix/2229fec260f8670de5657628ef28e522 to your computer and use it in GitHub Desktop.
Hello, DataPeps
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var DataPeps = require("datapeps-sdk"); | |
global["TextEncoder"] = require('text-encoding').TextEncoder; | |
global["TextDecoder"] = require('text-encoding').TextDecoder; | |
global["XMLHttpRequest"] = require('xhr2'); | |
global["WebSocket"] = require('ws'); | |
global["btoa"] = require('btoa'); | |
global["atob"] = require('atob'); | |
DataPeps.configure("https://" + process.env.PEPSCRYPTO_HOST); | |
let aliceLogin = "aliceliddell@peps.test", | |
bobLogin = "bobmorane@peps.test"; | |
let alicePassword = "aliceP@ssw0rd", | |
bobPassword = "bobP@ssw0rd"; | |
async function aliceSend() { | |
let aliceSession = await DataPeps.login(aliceLogin, alicePassword); | |
let resource = await aliceSession.Resource.create("text", "", [bobLogin]); | |
let messageToBob = "Hello, Bob!"; | |
let encryptedMessage = resource.encrypt(new TextEncoder().encode(messageToBob)); | |
console.log("Alice sent a message: \"" + messageToBob + "\""); | |
return [resource.id, encryptedMessage]; | |
} | |
async function bobReceive(resourceId, encryptedMessage) { | |
let bobSession = await DataPeps.login(bobLogin, bobPassword); | |
let resource = await bobSession.Resource.get(resourceId); | |
let decryptedMessage = resource.decrypt(encryptedMessage); | |
let messageToBob = new TextDecoder().decode(decryptedMessage); | |
console.log("Bob received a message: \"" + messageToBob + "\""); | |
} | |
async function main() { | |
var [resourceId, encryptedMessage] = await aliceSend(); | |
await bobReceive(resourceId, encryptedMessage); | |
} | |
main().catch((_) => process.stdout.write("An error has occurred\n")); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "hello-datapeps", | |
"version": "0.0.1", | |
"main": "hello-datapeps.js", | |
"scripts": { | |
"start": "node hello-datapeps.js" | |
}, | |
"dependencies": { | |
"datapeps-sdk": "^0.0.5", | |
"@types/text-encoding": "0.0.31", | |
"atob": "^2.0.3", | |
"btoa": "^1.1.2", | |
"text-encoding": "^0.6.4", | |
"ws": "^3.3.2", | |
"xhr2": "^0.1.4" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment