Revision 7218849a
Added by koszko over 1 year ago
common/misc.js | ||
---|---|---|
45 | 45 |
#FROM common/browser.js IMPORT browser |
46 | 46 |
#FROM common/stored_types.js IMPORT TYPE_NAME, TYPE_PREFIX |
47 | 47 |
|
48 |
/* uint8_to_hex is a separate function used in cryptographic functions. */ |
|
49 |
const uint8_to_hex = |
|
50 |
array => [...array].map(b => ("0" + b.toString(16)).slice(-2)).join(""); |
|
51 |
|
|
48 | 52 |
/* |
49 |
* generating unique, per-site value that can be computed synchronously
|
|
50 |
* and is impossible to guess for a malicious website
|
|
53 |
* Asynchronously compute hex string representation of a sha256 digest of a
|
|
54 |
* UTF-8 string.
|
|
51 | 55 |
*/ |
52 |
|
|
53 |
/* Uint8toHex is a separate function not exported as (a) it's useful and (b) it will be used in crypto.subtle-based digests */ |
|
54 |
function Uint8toHex(data) |
|
55 |
{ |
|
56 |
let returnValue = ''; |
|
57 |
for (let byte of data) |
|
58 |
returnValue += ('00' + byte.toString(16)).slice(-2); |
|
59 |
return returnValue; |
|
56 |
async function sha256_async(string) { |
|
57 |
const input_ab = new TextEncoder("utf-8").encode(string); |
|
58 |
const digest_ab = await crypto.subtle.digest("SHA-256", input_ab); |
|
59 |
return uint8_to_hex(new Uint8Array(digest_ab)); |
|
60 | 60 |
} |
61 |
#EXPORT sha256_async |
|
61 | 62 |
|
63 |
/* |
|
64 |
* Generate a unique value that can be computed synchronously and is impossible |
|
65 |
* to guess for a malicious website. |
|
66 |
*/ |
|
62 | 67 |
function gen_nonce(length=16) |
63 | 68 |
{ |
64 |
let randomData = new Uint8Array(length);
|
|
65 |
crypto.getRandomValues(randomData);
|
|
66 |
return Uint8toHex(randomData);
|
|
69 |
const random_data = new Uint8Array(length);
|
|
70 |
crypto.getRandomValues(random_data);
|
|
71 |
return uint8_to_hex(random_data);
|
|
67 | 72 |
} |
68 | 73 |
#EXPORT gen_nonce |
69 | 74 |
|
Also available in: Unified diff
add a mapping/resources installation dialog