Skip to content

Commit

Permalink
Implemented encryption
Browse files Browse the repository at this point in the history
  • Loading branch information
ivocarbajo committed Mar 19, 2020
1 parent 868f0ee commit 33fd00b
Show file tree
Hide file tree
Showing 9 changed files with 284 additions and 19 deletions.
2 changes: 1 addition & 1 deletion content-script.js → decrypt.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ var privkey;
var privkeyPassword;

//Get private key and credentials
chrome.storage.sync.get(['privateKey', 'privateKeyPassword'], result => {
chrome.storage.local.get(['privateKey', 'privateKeyPassword'], result => {
if (typeof result.privateKey !== 'undefined'){
privkey = result.privateKey.join("\n");
} else {
Expand Down
31 changes: 31 additions & 0 deletions encrypt.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
async function encryptText(text){
if(typeof text === 'string' && text.length !== 0){
chrome.storage.local.get(['encryptFor'], async result => {
if (result.encryptFor.length != 0){
var publicKeys = await Promise.all(result.encryptFor.map(async (key) => {
return (await openpgp.key.readArmored(key)).keys[0];
}));

openpgp.encrypt({
message: openpgp.message.fromText(text),
publicKeys: publicKeys,
}).then(encrypted => {
var encryptedText = encrypted.data;
encryptedText = encryptedText.replace(encryptedText.slice(27, 88), '');
navigator.clipboard.writeText(encryptedText);
M.toast({html: `LivePGP: The selected text was encrypted, it's on your clipboard!`});
});
} else {
M.toast({html: `LivePGP: No public keys were selected, so nothing was encrypted. ¯\\_(ツ)_/¯`});
}
});
} else {
M.toast({html: `LivePGP: Nothing was selected, so nothing was encrypted. ¯\\_(ツ)_/¯`});
}
}

document.addEventListener("keydown", function (e) {
if (e.ctrlKey && e.shiftKey && (e.key === "e" || e.key === "E")) {
encryptText(window.getSelection().toString());
}
});
3 changes: 2 additions & 1 deletion livepgp.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ var livepgp = {
checkDecryptable: span => {
return !span.hasAttribute("decrypted") &&
span.innerHTML.includes("-----BEGIN PGP MESSAGE-----") &&
span.innerHTML.includes("-----END PGP MESSAGE-----");
span.innerHTML.includes("-----END PGP MESSAGE-----") &&
span.getAttribute("data-text") != true;
}
}
13 changes: 8 additions & 5 deletions manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
"version": "1.0",
"description": "Decrypt PGP encrypted messages on the web automatically",
"browser_action": {
"default_title": "Open options",
"default_popup": "options.html"
"default_title": "Open menu",
"default_popup": "menu.html"
},
"content_scripts": [
{
Expand All @@ -15,15 +15,18 @@
],
"js": [
"vendor/openpgp.min.js",
"vendor/materialize.min.js",
"livepgp.js",
"content-script.js"
"decrypt.js",
"encrypt.js"
],
"css": [
"style.css"
"style.css",
"vendor/toaststyle.css"
],
"run_at": "document_end"
}
],
"options_page": "options.html",
"permissions": ["storage", "activeTab"]
"permissions": ["storage", "activeTab", "clipboardWrite", "unlimitedStorage"]
}
74 changes: 74 additions & 0 deletions menu.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<!DOCTYPE html>
<html>
<head>
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css">
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<style>
body {
min-width: 500px;
}

.materialize-textarea {
white-space: pre-wrap;
}
</style>
</head>

<body>
<nav>
<div class="nav-wrapper blue lighten-4">
<ul class="left">
<li class="active"><a href="menu.html">Public keys</a></li>
<li><a href="options.html">Private key</a></li>
</ul>
</div>
</nav>
<div class="">
<ul class="collapsible" style="height: 90vh; width: 100vw; margin:0px; box-shadow: none;">
<li class="active">
<div class="collapsible-header"><i class="material-icons">vpn_key</i>Select public keys</div>
<div class="collapsible-body">
<form id="use-public-key-form">
<div id="select-pub-keys">

</div>
<span class="blue-text text-lighten-3">
Note: To encrypt a message, select the text you want to encrypt and press CTRL + SHIFT + E,
then paste the encrypted text anywhere, it's in your clipboard!
</span>
<button class="btn waves-effect waves-light">Submit</button>
</form>
</div>
</li>
<li>
<div class="collapsible-header"><i class="material-icons">save_alt</i>Import a public key</div>
<div class="collapsible-body">
<form id="import-public-key-form">
<div class="input-field">
<input type="text" id="public-key-nickname-ifield">
<label for="public-key-nickname-ifield">Public key nickname</label>
</div>

<div class="input-field">
<textarea id="public-key-ifield" class="materialize-textarea"></textarea>
<label for="public-key-ifield">Public key</label>
</div>
<p>
<span class="blue-text text-lighten-3">
Note: At the moment you can only import one public key at a time, importing more than one key will
result in only one key being imported, or none at all. This also applies to ASCII armored
keys bundled up in one single key block, only one key from the block will be imported.
</span>
</p>
<button class="btn waves-effect waves-light" type="submit">Submit</button>
</form>
</div>
</li>
</ul>
</div>

<script src="/vendor/materialize.min.js"></script>
<script src="menu.js"></script>
</body>
</html>
76 changes: 76 additions & 0 deletions menu.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
M.AutoInit();

var elems = document.querySelectorAll('.collapsible');
var instances = M.Collapsible.init(elems);

chrome.storage.local.get(['publicKeys', "encryptFor"], (result) => {
if (typeof result.publicKeys === 'undefined' || result.publicKeys == null) {
chrome.storage.local.set({
publicKeys: [],
encryptFor: typeof encryptFor === 'undefined' ? [] : encryptFor
}, (result) => {

});
} else if(result.publicKeys.length == 0) {
M.toast({html: 'Set public keys on the "Import a public key" section'});
} else {
result.publicKeys.forEach(publicKey => {
var label = document.createElement("label");
var checkboxWrapper = document.createElement("p");
var checkbox = document.createElement("input");
checkbox.classList.add("pubkeyCheckbox");
if(result.encryptFor.includes(publicKey.value)) checkbox.setAttribute("checked", "");
checkbox.setAttribute("type", "checkbox");
checkbox.setAttribute("key", publicKey.value);
var span = document.createElement("span");
span.innerHTML = publicKey.nickname;
label.appendChild(checkbox);
label.appendChild(span);
checkboxWrapper.appendChild(label);
document.getElementById("select-pub-keys").appendChild(checkboxWrapper);
});
}
});

document.getElementById("import-public-key-form").addEventListener("submit" ,(e) => {
e.preventDefault();
chrome.storage.local.get(['publicKeys'], (result) => {
result.publicKeys.push({
value: document.getElementById('public-key-ifield').value,
nickname: document.getElementById('public-key-nickname-ifield').value
});

chrome.storage.local.set({
publicKeys: result.publicKeys
}, (result) => {
M.toast({html: 'Public key added! Reloading...'});
setTimeout(() => {
location.reload();
}, 1000);
});
});
});

document.getElementById("use-public-key-form").addEventListener("submit" ,(e) => {
e.preventDefault();
var pubkeys = [];
document.querySelectorAll(".pubkeyCheckbox").forEach(checkbox => {
if (checkbox.checked){
pubkeys.push(checkbox.getAttribute("key"));
}
});

chrome.storage.local.set({
encryptFor: pubkeys
}, () => {
M.toast({html: 'Public keys set!'})
});
});

chrome.storage.local.get(['privateKey'], (result) => {
if (typeof result.privateKey !== 'undefined' || result.privateKey == null || result.privateKey == ''){
document.querySelectorAll('a[href="options.html"]').forEach((element) => {
element.classList.add("pulse");
});
}
});
9 changes: 8 additions & 1 deletion options.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,14 @@
</head>

<body>

<nav>
<div class="nav-wrapper blue lighten-4">
<ul class="left">
<li><a href="menu.html">Public keys</a></li>
<li class="active"><a href="options.html">Private key</a></li>
</ul>
</div>
</nav>
<div class="container">
<h4>Live PGP configuration</h4>
<div class="row">
Expand Down
16 changes: 5 additions & 11 deletions options.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
function message(messageText) {
document.getElementById("message-text").innerText = messageText;
document.getElementById("message-container").removeAttribute("hidden");
setTimeout(() => {
document.getElementById("message-container").setAttribute("hidden", '');
}, 5000);
}

function saveKeys (){
chrome.storage.sync.set({
chrome.storage.local.set({
privateKeyPassword: document.getElementById("private-key-password").value,
privateKey: document.getElementById("private-key-textarea").value.split('\n')
}, function() {
message("PGP Private key updated");
M.toast({html: 'Private key set!'})
console.log(document.getElementById("private-key-textarea").value);
});
}
Expand All @@ -26,7 +18,7 @@ document.getElementById("pgp-key-form").addEventListener("submit", (e) => {
saveKeys();
});

chrome.storage.sync.get(['privateKey'], function(result) {
chrome.storage.local.get(['privateKey'], function(result) {
if (result != '' || typeof result != undefined) {
document.querySelector('label[for="private-key-textarea"]').setAttribute("class", "active");
document.getElementById("private-key-textarea").setAttribute("style", "height: 591px;");
Expand All @@ -36,4 +28,6 @@ chrome.storage.sync.get(['privateKey'], function(result) {
} else {
console.log("Live PGP: No PGP private key present");
}

M.textareaAutoResize(document.getElementById("private-key-textarea"));
});
79 changes: 79 additions & 0 deletions vendor/toaststyle.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#toast-container {
display: block;
position: fixed;
z-index: 10000;
}

@media only screen and (max-width: 600px) {
#toast-container {
min-width: 100%;
bottom: 0%;
}
}

@media only screen and (min-width: 601px) and (max-width: 992px) {
#toast-container {
left: 5%;
bottom: 7%;
max-width: 90%;
}
}

@media only screen and (min-width: 993px) {
#toast-container {
top: 10%;
right: 7%;
max-width: 86%;
}
}

.toast {
font-family: monospace, monospace;
-webkit-box-shadow: 0 2px 2px 0 rgba(0, 0, 0, 0.14), 0 3px 1px -2px rgba(0, 0, 0, 0.12), 0 1px 5px 0 rgba(0, 0, 0, 0.2);
box-shadow: 0 2px 2px 0 rgba(0, 0, 0, 0.14), 0 3px 1px -2px rgba(0, 0, 0, 0.12), 0 1px 5px 0 rgba(0, 0, 0, 0.2);
border-radius: 2px;
top: 35px;
width: auto;
margin-top: 10px;
position: relative;
max-width: 100%;
height: auto;
min-height: 48px;
line-height: 1.5em;
background-color: #323232;
padding: 10px 25px;
font-size: 1.1rem;
font-weight: 300;
color: #fff;
display: -webkit-box;
display: -webkit-flex;
display: -ms-flexbox;
display: flex;
-webkit-box-align: center;
-webkit-align-items: center;
-ms-flex-align: center;
align-items: center;
-webkit-box-pack: justify;
-webkit-justify-content: space-between;
-ms-flex-pack: justify;
justify-content: space-between;
cursor: default;
}

.toast .toast-action {
color: #eeff41;
font-weight: 500;
margin-right: -25px;
margin-left: 3rem;
}

.toast.rounded {
border-radius: 24px;
}

@media only screen and (max-width: 600px) {
.toast {
width: 100%;
border-radius: 0;
}
}

0 comments on commit 33fd00b

Please sign in to comment.