aboutsummaryrefslogtreecommitdiff
path: root/common/misc.js
diff options
context:
space:
mode:
Diffstat (limited to 'common/misc.js')
-rw-r--r--common/misc.js31
1 files changed, 18 insertions, 13 deletions
diff --git a/common/misc.js b/common/misc.js
index ba14a33..ed8f400 100644
--- a/common/misc.js
+++ b/common/misc.js
@@ -45,25 +45,30 @@
#FROM common/browser.js IMPORT browser
#FROM common/stored_types.js IMPORT TYPE_NAME, TYPE_PREFIX
+/* uint8_to_hex is a separate function used in cryptographic functions. */
+const uint8_to_hex =
+ array => [...array].map(b => ("0" + b.toString(16)).slice(-2)).join("");
+
/*
- * generating unique, per-site value that can be computed synchronously
- * and is impossible to guess for a malicious website
+ * Asynchronously compute hex string representation of a sha256 digest of a
+ * UTF-8 string.
*/
-
-/* Uint8toHex is a separate function not exported as (a) it's useful and (b) it will be used in crypto.subtle-based digests */
-function Uint8toHex(data)
-{
- let returnValue = '';
- for (let byte of data)
- returnValue += ('00' + byte.toString(16)).slice(-2);
- return returnValue;
+async function sha256_async(string) {
+ const input_ab = new TextEncoder("utf-8").encode(string);
+ const digest_ab = await crypto.subtle.digest("SHA-256", input_ab);
+ return uint8_to_hex(new Uint8Array(digest_ab));
}
+#EXPORT sha256_async
+/*
+ * Generate a unique value that can be computed synchronously and is impossible
+ * to guess for a malicious website.
+ */
function gen_nonce(length=16)
{
- let randomData = new Uint8Array(length);
- crypto.getRandomValues(randomData);
- return Uint8toHex(randomData);
+ const random_data = new Uint8Array(length);
+ crypto.getRandomValues(random_data);
+ return uint8_to_hex(random_data);
}
#EXPORT gen_nonce