Permalink
Find file
Fetching contributors…
Cannot retrieve contributors at this time
173 lines (156 sloc) 5.04 KB
<!doctype html>
<!--
This example file demonstrates how to call pygpghttpd with JavaScript.
For full API documentation please visit: https://github.com/qnrq/pygpghttpd/blob/master/DOCS/API.txt
-->
<head>
<title>pygpghttpd test suite</title>
</head>
<body>
<h1>pygpghttpd test suite</h1>
<div id="status"></div>
<div id="tests" style="display:none;">
<h2>Key generation</h2>
<strong>Algo:</strong>
<select id="type"><option value="RSA">RSA</option><option value="DSA">DSA</option></select>
<strong>Length:</strong>
<select id="length"><option value="2048">2048</option><option value="4096">4096</option></select>
<strong>Name:</strong> <input type="text" id="name" value="Alice" />
<strong>Email:</strong> <input type="text" id="email" value="alice@foo.com" />
<strong>Passphrase:</strong> <input type="text" id="passphrase" value="alice<3bob" />
<input type="button" id="keygen" value="Generate" onclick="keygen($('#type').val(), $('#length').val(), $('#name').val(), $('#email').val(), $('#passphrase').val());" />
<p id="generating" style="display:none;color:red;">Generating, please wait...</div>
<h2>Pubkeys</h2>
<table id="pubkeys">
<thead><th>Keyid</th><th>Fingerprint</th><th>Algo</th><th>Length</th><th>Delete</th></thead>
</table>
<h2>Privkeys</h2>
<table id="privkeys">
<thead><th>Keyid</th><th>Fingerprint</th><th>Algo</th><th>Length</th><th>Delete</th></thead>
</table>
<h2>Encrypt</h2>
<textarea id="encrypt_data">Secrets</textarea>
<input type="button" value="Encrypt" onclick="encrypt();" />
<h2>Decrypt</h2>
<textarea id="decrypt_data"></textarea>
<input type="button" value="Decrypt" onclick="decrypt();" />
</div>
<script src="./js/jquery-1.10.2.min.js"></script>
<script>
$("#status").html("Connecting to pygpghttpd");
$.post("https://localhost:11337/", function() {
$("#status").append(": success!");
$("#tests").show();
}).fail(function() {
$("#status").append(": fail!");
});
function keygen(type, length, name, email, passphrase) {
try {
$("#keygen").toggle();
$("#generating").toggle();
} catch(e) {
alert(e);
}
$.post("https://localhost:11337/", {
cmd: "keygen",
type: type,
length: length,
name: name,
email: email,
passphrase: passphrase
}, function(data) {
try {
$("#keygen").toggle();
$("#generating").toggle();
} catch(e) {
alert(e);
}
keylist();
keylist(true);
});
return true;
}
function keydel(fingerprint, private) {
if(typeof(private) == "undefined") {
private = false;
}
$.post("https://localhost:11337", {
cmd: "keydel",
fingerprint: fingerprint,
private: private
}, function(data) {
if(data != "ok") {
alert(data);
}
try {
keylist();
keylist(true);
} catch(e) {
alert(e);
}
});
return true;
}
function keylist(private) {
if(typeof(private) == "undefined") {
private = false;
}
$.post("https://localhost:11337/", {
cmd: "keylist",
private: private
}, function(data) {
obj = JSON.parse(data);
for(var o in obj) {
algo = escapeHtml(obj[o].algo);
fingerprint = escapeHtml(obj[o].fingerprint);
keyid = escapeHtml(obj[o].keyid);
length = escapeHtml(obj[o].length);
if(private) {
$("#privkeys").html("<thead><th>Keyid</th><th>Fingerprint</th><th>Algo</th><th>Length</th><th>Delete</th></thead>");
$("#privkeys").append("<tr><td>" + keyid + "</td>" + "<td>" +fingerprint + "</td><td>" + algo + "</td><td>" + length + "</td><td><a href='#' onclick='keydel(\"" + fingerprint + "\", true);'>X</a></td></tr>");
} else {
$("#pubkeys").html("<thead><th>Keyid</th><th>Fingerprint</th><th>Algo</th><th>Length</th><th>Delete</th><th></th></thead>");
$("#pubkeys").append("<tr><td>" + keyid + "</td><td>" + fingerprint + "</td><td>" + algo + "</td><td>" + length + "</td><td><a href='#' onclick='keydel(\"" + fingerprint + "\");'>X</a></td><td><a href=\"#\" onclick=\"exportPubkey('" + keyid + "')\">Export</a></td></tr>");
}
}
return true;
} );
}
function exportPubkey(id) {
$.post("https://localhost:11337", {
cmd: "keyexport",
id: id
}, function(data) {
alert(data);
});
}
function encrypt() {
$.post("https://localhost:11337", {
cmd: "encrypt",
data: $("#encrypt_data").val(),
recipients: "alice@foo.com"
}, function(data) {
$("#decrypt_data").val(data);
});
}
function decrypt() {
$.post("https://localhost:11337", {
cmd: "decrypt",
data: $("#decrypt_data").val(),
passphrase: $("#passphrase").val()
}, function(data) {
alert(data);
});
}
function escapeHtml(unsafe) {
return unsafe.replace(/&/g, "&amp;")
.replace(/</g, "&lt;")
.replace(/>/g, "&gt;")
.replace(/"/g, "&quot;")
.replace(/'/g, "&#039;");
}
keylist();
keylist(true);
</script>
</body>
</html>