forked from petercunha/Pad
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cryptography - Use AES-GCM-256 for encryption - Use PBKDF2 with SHA-256 and 100000 iterations for key derivation Behavior - A human-readable password will be automatically be generated when not set - The password will be appended to the URI as fragment #password - The IV (12 bytes) is prepended to the ciphertext - Everything is base64 encoded (33% overhead) - Update is disabled when encryption fails to avoid overwriting Fixes petercunha#12
- Loading branch information
1 parent
e7be2df
commit 5c8546a
Showing
3 changed files
with
65 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
const unambiguousChars = '23456789abcdefghjkmnpqrstwxyz'; // easily human-readable | ||
async function generatePassword(length) { | ||
let password = window.crypto.getRandomValues(new Uint32Array(length)); | ||
return password.reduce((str,chr)=>str+unambiguousChars[chr%unambiguousChars.length],''); // chars from unambiguousChars list | ||
} | ||
async function deriveKey(password, salt) { | ||
let keyMaterial = await window.crypto.subtle.importKey("raw", new TextEncoder().encode(password), "PBKDF2", false, ["deriveBits", "deriveKey"]); | ||
return window.crypto.subtle.deriveKey( | ||
{ name: "PBKDF2", salt: salt, iterations: 100000, hash: "SHA-256" }, | ||
keyMaterial, | ||
{ name: "AES-GCM", length: 256}, | ||
true, | ||
[ "encrypt", "decrypt" ] | ||
); | ||
} | ||
const IV_LENGTH = 12; /// 96 bits | ||
async function encrypt(plaintext, key) { | ||
let iv = window.crypto.getRandomValues(new Uint8Array(IV_LENGTH)); | ||
let ciphertext = await window.crypto.subtle.encrypt({ name: "AES-GCM", iv: iv }, key, new TextEncoder().encode(plaintext)); | ||
return btoa(String.fromCharCode(...iv,...new Uint8Array(ciphertext))); // base64 | ||
} | ||
async function decrypt(bufferEncoded, key) { | ||
let buffer = Uint8Array.from(atob(bufferEncoded), c => c.charCodeAt(0)) //base64 | ||
return new TextDecoder().decode(await window.crypto.subtle.decrypt({ name: "AES-GCM", iv: buffer.subarray(0, IV_LENGTH) }, key, buffer.subarray(IV_LENGTH))); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters