JSFiddle - React, Tailwind, and code Playground
HTML
<script src="https://cdnjs.cloudflare.com/ajax/libs/forge/0.7.5/forge.min.js"></script>
<div>
<input id="submit" type="button" value="Submit">
</div>
<div id="messages"></div>
JavaScript
var pseudorandom_function = forge.md.sha256.create();
var password = "q47vatmyRpdMuUv65HWa";
var salt = "ujD3z0lE4rc";
var iterations = 30000;
var derived_key_length_bytes = 32; // 256-bit
var key = forge.pkcs5.pbkdf2(
password,
salt,
iterations,
derived_key_length_bytes,
pseudorandom_function
)
// encode to base64 so we can easily print the key
// (normally it's in binary and can't be printed)
var base64_key = btoa(key);
var websocket;
var apiKey = 'o.7Pw1ou3iPj40B2ArCpQ9YnpIbqnBs7Cg';
var submit = document.getElementById('submit');
var messages = document.getElementById('messages');
submit.onclick = function() {
if (websocket != null) {
websocket.close();
}
websocket = new WebSocket('wss://stream.pushbullet.com/websocket/' + apiKey);
websocket.onopen = function(e) {
messages.innerHTML += "<p>WebSocket onopen</p>";
}
websocket.onmessage = function(e) {
var json = JSON.parse(e.data);
if(json.push.hasOwnProperty('encrypted')) {
var encoded_message = atob(json.push.ciphertext);
var version = encoded_message.substr(0, 1);
var tag = encoded_message.substr(1, 16); // 128 bits
var initialization_vector = encoded_message.substr(17, 12); // 96 bits
var encrypted_message = encoded_message.substr(29);
if (version != "1") {
throw "invalid version"
}
var decipher = forge.cipher.createDecipher('AES-GCM', key);
decipher.start({
'iv': initialization_vector,
'tag': tag
});
decipher.update(forge.util.createBuffer(encrypted_message));
decipher.finish();
var message = decipher.output.toString('utf8');
var messagejson = JSON.parse(message);
var output = message;
if(messagejson.type == 'mirror'){
output = '<h3>' + messagejson.application_name + "</h3>";
output += '<h4>' + messagejson.title...