Add WKD Local Part utility

This commit is contained in:
Yarmo Mackenbach 2020-06-29 20:58:34 +02:00
parent c32c847204
commit 32b8d58bd6
3 changed files with 113 additions and 2 deletions

View file

@ -504,12 +504,50 @@ async function fetchKeys(opts) {
return output;
}
function encodeZBase32(data) {
// Source: https://github.com/openpgpjs/openpgpjs/blob/master/src/util.js
if (data.length === 0) {
return "";
}
const ALPHABET = "ybndrfg8ejkmcpqxot1uwisza345h769";
const SHIFT = 5;
const MASK = 31;
let buffer = data[0];
let index = 1;
let bitsLeft = 8;
let result = '';
while (bitsLeft > 0 || index < data.length) {
if (bitsLeft < SHIFT) {
if (index < data.length) {
buffer <<= 8;
buffer |= data[index++] & 0xff;
bitsLeft += 8;
} else {
const pad = SHIFT - bitsLeft;
buffer <<= pad;
bitsLeft += pad;
}
}
bitsLeft -= SHIFT;
result += ALPHABET[MASK & (buffer >> bitsLeft)];
}
return result;
}
async function computeWKDLocalPart(message) {
const encoder = new TextEncoder();
const data = encoder.encode(message);
const hash = await crypto.subtle.digest('SHA-1', data);
return encodeZBase32(new Uint8Array(hash));
}
// General purpose
let elFormVerify = document.body.querySelector("#form-verify"),
elFormEncrypt = document.body.querySelector("#form-encrypt"),
elFormProofs = document.body.querySelector("#form-proofs"),
elProfileUid = document.body.querySelector("#profileUid"),
elProfileMode = document.body.querySelector("#profileMode");
elProfileMode = document.body.querySelector("#profileMode"),
elUtilWKD = document.body.querySelector("#form-util-wkd");
if (elFormVerify) {
elFormVerify.onsubmit = function (evt) {
@ -628,3 +666,20 @@ if (elProfileUid) {
}
displayProfile(opts);
}
if (elUtilWKD) {
elUtilWKD.onsubmit = function (evt) {
evt.preventDefault();
}
const elInput = document.body.querySelector("#input");
const elOutput = document.body.querySelector("#output");
elInput.addEventListener("input", async function(evt) {
if (evt.target.value) {
elOutput.value = await computeWKDLocalPart(evt.target.value);
} else {
elOutput.value = "";
}
});
}

View file

@ -26,6 +26,7 @@ $router->map('GET', '/encrypt/[:uid]', function() {}, 'encryptUid');
$router->map('GET', '/proofs/[:uid]', function() {}, 'proofsUid');
$router->map('GET', '/hkp/[**:uid]', function() {}, 'profileHKP');
$router->map('GET', '/wkd/[**:uid]', function() {}, 'profileWKD');
$router->map('GET', '/util/[:id]', function() {}, 'util');
$router->map('GET', '/[**:uid]', function() {}, 'profile');
// Router matching
@ -132,8 +133,13 @@ if(is_array($match) && is_callable($match['target'])) {
echo($content);
break;
case 'util':
$id = $match['params']['id'];
readfile("pages/util/$id.html");
break;
case 'faq':
readfile('pages/faq.html');
readfile("pages/faq.html");
break;
}
} else {

50
pages/util/wkd.html Normal file
View file

@ -0,0 +1,50 @@
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="shortcut icon" href="/favicon.png">
<title>WKD utilities - Keyoxide</title>
<link rel="stylesheet" href="/assets/styles.css">
</head>
<body>
<header>
<div class="container">
<a href="/">Keyoxide</a>
<div class="spacer"></div>
<nav>
<a href="/verify">verify</a>
<a href="/encrypt">encrypt</a>
<a href="/proofs">proofs</a>
</nav>
</div>
</header>
<div class="container">
<h1>WKD Local Part</h1>
<div class="content">
<form id="form-util-wkd" method="post">
<h3>Name</h3>
<input type="text" name="input" id="input" placeholder="Name">
<h3>Result</h3>
<input type="text" name="output" id="output" readonly>
</form>
</div>
<footer>
<p>
Sitemap:
<a href="/">index</a> -
<a href="/encrypt">encrypt</a> -
<a href="/verify">verify</a> -
<a href="/proofs">proofs</a> -
<a href="/guides">guides</a> -
<a href="/faq">faq</a>
</p>
</footer>
</div>
</body>
<script type="text/javascript" src="/assets/scripts.js" charset="utf-8"></script>
</html>