2017-04-29 13:14:06 +00:00
<!doctype html>
< html >
< head >
< meta charset = "utf-8" >
2017-06-15 10:32:45 +00:00
< title > StatiCrypt: Password protect static HTML< / title >
2017-04-29 13:14:06 +00:00
< meta name = "description" content = "" >
< meta name = "viewport" content = "width=device-width, initial-scale=1" >
2017-06-17 18:44:00 +00:00
< link rel = "stylesheet"
type="text/css"
href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"
integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u"
crossorigin="anonymous">
2017-06-14 14:19:24 +00:00
< style >
a.no-style {
color: inherit;
text-decoration: inherit;
}
body {
font-size: 16px;
}
2017-06-15 15:48:20 +00:00
label.no-style {
font-weight: normal;
}
2017-06-14 14:19:24 +00:00
< / style >
2017-04-29 13:14:06 +00:00
< / head >
< body >
< div class = "container" >
< div class = "row" >
< div class = "col-xs-12" >
2017-06-14 14:19:24 +00:00
< h1 >
StatiCrypt
< div class = "pull-right" >
< iframe src = "https://ghbtns.com/github-btn.html?user=robinmoisson&repo=staticrypt&type=star&size=large"
frameborder="0" scrolling="0" width="80px" height="30px">< / iframe >
< iframe src = "https://ghbtns.com/github-btn.html?user=robinmoisson&repo=staticrypt&type=fork&size=large"
frameborder="0" scrolling="0" width="80px" height="30px">< / iframe >
< / div >
< br >
< small > Password protect a static HTML page< / small >
< / h1 >
< p >
2023-03-29 08:50:31 +00:00
StatiCrypt uses AES-256 with WebCrypto to encrypt your html string with your long password, in your browser (client side).
2017-06-14 14:19:24 +00:00
< / p >
2017-04-29 13:14:06 +00:00
< p >
2017-06-14 15:22:24 +00:00
Download your encrypted string in a HTML page with a password prompt you can upload anywhere (see < a
2022-12-21 09:01:21 +00:00
target="_blank" href="example/example_encrypted.html">example< / a > ).
2017-04-29 13:14:06 +00:00
< / p >
2018-01-14 12:03:45 +00:00
< p >
2022-02-27 18:55:03 +00:00
The tool is also available as < a href = "https://npmjs.com/package/staticrypt" > a CLI on NPM< / a > and is < a
href="https://github.com/robinmoisson/staticrypt">open source on GitHub< / a > .
2018-01-14 12:03:45 +00:00
< / p >
2017-06-14 14:19:24 +00:00
< br >
2017-06-14 15:22:24 +00:00
2022-02-10 08:22:32 +00:00
< h4 >
< a class = "no-style" id = "toggle-concept" href = "#" >
< span id = "toggle-concept-sign" > ►< / span > HOW IT WORKS
< / a >
< / h4 >
2017-06-14 14:19:24 +00:00
< div id = "concept" class = "hidden" >
2018-02-19 18:45:43 +00:00
< p >
2023-03-01 17:46:48 +00:00
< b class = "text-danger" > Disclaimer< / b > if you are an at-risk activist, or have extra sensitive
banking data, you should probably use something else!
2018-02-19 18:45:43 +00:00
< / p >
2017-06-14 14:19:24 +00:00
< p >
StatiCrypt generates a static, password protected page that can be decrypted in-browser:
2017-06-15 11:21:59 +00:00
just send or upload the generated page to a place serving static content (github pages, for example)
2017-06-14 14:19:24 +00:00
and you're done: the javascript will prompt users for password, decrypt the page and load your HTML.
< / p >
2023-03-29 08:50:31 +00:00
< p >
The page is encrypted with AES-256 in CBC mode (see why this mode is appropriate for StatiCrypt in
< a href = "https://github.com/robinmoisson/staticrypt/issues/19" > #19< / a > ). The password is hashed with
PBKDF2 (599k iterations with SHA-256, plus 1k with SHA-1 for legacy reasons (see
< a href = "https://github.com/robinmoisson/staticrypt/issues/159" > #159< / a > ), for the added
< a href = "https://cheatsheetseries.owasp.org/cheatsheets/Password_Storage_Cheat_Sheet.html#pbkdf2" > recommended
total< / a > of 600k) and used to encrypt the page.
< / p >
2017-06-14 14:19:24 +00:00
< p >
2018-02-19 18:45:43 +00:00
It basically encrypts your page and puts everything with a user-friendly way to use a password
2023-03-01 17:46:48 +00:00
in the new file. AES-256 is state of the art but < b > brute-force/dictionary attacks would be easy to
do at a really fast pace: use a long, unusual password!< / b >
< br / > => To be safe, we recommend 16+ alphanum characters, and using a password manager like the
open-source < a href = "http://bitwarden.com" > Bitwarden< / a > .
2017-06-14 14:19:24 +00:00
< / p >
< p >
2018-02-19 18:45:43 +00:00
Feel free to contribute or report any thought to the
2023-03-01 17:46:48 +00:00
< a href = "https://github.com/robinmoisson/staticrypt" > GitHub project< / a > .
2017-06-14 14:19:24 +00:00
< / p >
< / div >
2017-06-14 15:22:24 +00:00
< br >
2017-04-29 13:14:06 +00:00
< / div >
< / div >
< div class = "row" >
< div class = "col-xs-12" >
< form id = "encrypt_form" >
< div class = "form-group" >
2023-03-01 17:46:48 +00:00
< label for = "passphrase" > Password< / label >
2017-04-29 13:14:06 +00:00
< input type = "password" class = "form-control" id = "passphrase"
2023-03-01 17:46:48 +00:00
placeholder="Password (choose a long one!)">
2017-04-29 13:14:06 +00:00
< / div >
2022-02-10 08:22:32 +00:00
2017-04-29 13:14:06 +00:00
< div class = "form-group" >
2017-06-14 14:19:24 +00:00
< label for = "unencrypted_html" > HTML/string to encrypt< / label >
2022-02-10 08:22:32 +00:00
< textarea class = "form-control"
id="unencrypted_html"
placeholder="< html > < head > ..."
2017-06-14 15:22:24 +00:00
rows="5">< / textarea >
2017-04-29 13:14:06 +00:00
< / div >
2022-02-10 08:22:32 +00:00
< div class = "form-group" >
< label class = "no-style" >
< input type = "checkbox" id = "remember" checked >
2023-03-29 09:49:10 +00:00
Add "Remember me" checkbox (append < code > #staticrypt_logout< / code > to your URL to logout)
2022-02-10 08:22:32 +00:00
< small >
< abbr class = "text-muted"
title="The password will be stored in clear text in the browser's localStorage upon entry by the user. See " More options" to set the expiration (default: none)">
(?)
< / abbr >
< / small >
< / label >
< / div >
2017-06-14 14:19:24 +00:00
< p >
< a href = "#" id = "toggle-extra-option" > + More options< / a >
< / p >
< div id = "extra-options" class = "hidden" >
< div class = "form-group" >
< label for = "title" > Page title< / label >
< input type = "text" class = "form-control" id = "title" placeholder = "Default: 'Protected Page'" >
< / div >
2022-02-10 08:22:32 +00:00
2017-06-14 14:19:24 +00:00
< div class = "form-group" >
< label for = "instructions" > Instructions to display the user< / label >
< textarea class = "form-control" id = "instructions" placeholder = "Default: nothing." > < / textarea >
< / div >
2017-04-29 13:14:06 +00:00
2022-02-10 08:22:32 +00:00
< div class = "form-group" >
2022-11-20 14:30:48 +00:00
< label for = "passphrase_placeholder" > Passphrase input placeholder< / label >
2022-02-10 08:22:32 +00:00
< input type = "text" class = "form-control" id = "passphrase_placeholder"
placeholder="Default: 'Passphrase'">
< / div >
< div class = "form-group" >
2022-11-20 14:30:48 +00:00
< label for = "remember_me" > "Remember me" checkbox label< / label >
2022-02-10 08:22:32 +00:00
< input type = "text" class = "form-control" id = "remember_me" placeholder = "Default: 'Remember me'" >
< / div >
< div class = "form-group" >
2022-11-20 14:30:48 +00:00
< label for = "remember_in_days" > "Remember me" expiration in days< / label >
2022-02-10 08:22:32 +00:00
< input type = "number"
class="form-control"
id="remember_in_days"
step="any"
placeholder="Default: 0 (no expiration)">
< small class = "form-text text-muted" >
After this many days, the user will have to enter the passphrase again. Leave empty or set
to 0 for no expiration.
< / small >
< / div >
< div class = "form-group" >
2022-11-20 14:30:48 +00:00
< label for = "decrypt_button" > Decrypt button label< / label >
2022-02-10 08:22:32 +00:00
< input type = "text" class = "form-control" id = "decrypt_button" placeholder = "Default: 'DECRYPT'" >
< / div >
2017-06-15 15:48:20 +00:00
< / div >
2017-06-14 14:19:24 +00:00
< button class = "btn btn-primary pull-right" type = "submit" > Generate passphrase protected HTML< / button >
2017-04-29 13:14:06 +00:00
< / form >
< / div >
< / div >
2022-02-10 08:22:32 +00:00
< div class = "row mb-5" >
2017-04-29 13:14:06 +00:00
< div class = "col-xs-12" >
< h2 > Encrypted HTML< / h2 >
2017-06-15 10:27:47 +00:00
< p > < a class = "btn btn-success download"
download="encrypted.html"
id="download-link"
2017-06-14 14:19:24 +00:00
disabled="disabled">Download html file with password prompt< / a > < / p >
2017-04-29 13:14:06 +00:00
< pre id = "encrypted_html_display" >
2017-06-14 14:19:24 +00:00
Your encrypted string< / pre >
2017-04-29 13:14:06 +00:00
< / div >
< / div >
< / div >
2018-02-19 18:34:49 +00:00
<!--
Filename changed to circumvent adblockers that mistake it for a crypto miner (see https://github.com/robinmoisson/staticrypt/issues/107)
-->
2022-11-21 10:34:21 +00:00
< script src = "lib/kryptojs-3.1.9-1.min.js" > < / script >
2017-04-29 13:14:06 +00:00
2017-06-19 08:46:47 +00:00
< script src = "https://cdn.ckeditor.com/4.7.0/standard/ckeditor.js" > < / script >
2022-11-20 14:25:16 +00:00
< script id = "cryptoEngine" >
window.cryptoEngine = ((function(){
const exports = {};
2023-03-29 08:50:31 +00:00
const { subtle } = crypto;
const IV_BITS = 16 * 8;
const HEX_BITS = 4;
const ENCRYPTION_ALGO = "AES-CBC";
/**
* Translates between utf8 encoded hexadecimal strings
* and Uint8Array bytes.
*
* Mirrors the API of CryptoJS.enc.Hex
*/
const HexEncoder = {
/**
* hex string -> bytes
* @param {string} hexString
* @returns {Uint8Array}
*/
parse: function (hexString) {
if (hexString.length % 2 !== 0) throw "Invalid hexString";
const arrayBuffer = new Uint8Array(hexString.length / 2);
for (let i = 0; i < hexString.length ; i + = 2 ) {
const byteValue = parseInt(hexString.substring(i, i + 2), 16);
if (isNaN(byteValue)) {
throw "Invalid hexString";
}
arrayBuffer[i / 2] = byteValue;
}
return arrayBuffer;
},
/**
* bytes -> hex string
* @param {Uint8Array} bytes
* @returns {string}
*/
stringify: function (bytes) {
const hexBytes = [];
for (let i = 0; i < bytes.length ; + + i ) {
let byteString = bytes[i].toString(16);
if (byteString.length < 2 ) {
byteString = "0" + byteString;
}
hexBytes.push(byteString);
}
return hexBytes.join("");
},
};
/**
* Translates between utf8 strings and Uint8Array bytes.
*/
const UTF8Encoder = {
parse: function (str) {
return new TextEncoder().encode(str);
},
stringify: function (bytes) {
return new TextDecoder().decode(bytes);
},
};
2022-11-20 14:25:16 +00:00
/**
* Salt and encrypt a msg with a password.
*/
2023-03-29 08:50:31 +00:00
async function encrypt(msg, hashedPassphrase) {
// Must be 16 bytes, unpredictable, and preferably cryptographically random. However, it need not be secret.
// https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/encrypt#parameters
const iv = crypto.getRandomValues(new Uint8Array(IV_BITS / 8));
const key = await subtle.importKey(
"raw",
HexEncoder.parse(hashedPassphrase),
ENCRYPTION_ALGO,
false,
["encrypt"]
);
const encrypted = await subtle.encrypt(
{
name: ENCRYPTION_ALGO,
iv: iv,
},
key,
UTF8Encoder.parse(msg)
);
// iv will be 32 hex characters, we prepend it to the ciphertext for use in decryption
return HexEncoder.stringify(iv) + HexEncoder.stringify(new Uint8Array(encrypted));
2022-11-20 14:25:16 +00:00
}
exports.encrypt = encrypt;
/**
* Decrypt a salted msg using a password.
*
* @param {string} encryptedMsg
* @param {string} hashedPassphrase
2023-03-29 08:50:31 +00:00
* @returns {Promise< string > }
2022-11-20 14:25:16 +00:00
*/
2023-03-29 08:50:31 +00:00
async function decrypt(encryptedMsg, hashedPassphrase) {
const ivLength = IV_BITS / HEX_BITS;
const iv = HexEncoder.parse(encryptedMsg.substring(0, ivLength));
const encrypted = encryptedMsg.substring(ivLength);
const key = await subtle.importKey(
"raw",
HexEncoder.parse(hashedPassphrase),
ENCRYPTION_ALGO,
false,
["decrypt"]
);
const outBuffer = await subtle.decrypt(
{
name: ENCRYPTION_ALGO,
iv: iv,
},
key,
HexEncoder.parse(encrypted)
);
return UTF8Encoder.stringify(new Uint8Array(outBuffer));
2022-11-20 14:25:16 +00:00
}
exports.decrypt = decrypt;
/**
* Salt and hash the passphrase so it can be stored in localStorage without opening a password reuse vulnerability.
*
* @param {string} passphrase
* @param {string} salt
2023-03-29 08:50:31 +00:00
* @returns {Promise< string > }
2022-11-20 14:25:16 +00:00
*/
2023-03-29 08:50:31 +00:00
async function hashPassphrase(passphrase, salt) {
// we hash the passphrase in multiple steps, each adding more iterations. This is because we used to allow less
// iterations, so for backward compatibility reasons, we need to support going from that to more iterations.
let hashedPassphrase = await hashLegacyRound(passphrase, salt);
hashedPassphrase = await hashSecondRound(hashedPassphrase, salt);
2023-03-01 17:46:48 +00:00
2023-03-29 08:50:31 +00:00
return hashThirdRound(hashedPassphrase, salt);
2023-03-01 17:46:48 +00:00
}
exports.hashPassphrase = hashPassphrase;
/**
* This hashes the passphrase with 1k iterations. This is a low number, we need this function to support backwards
* compatibility.
*
* @param {string} passphrase
* @param {string} salt
2023-03-29 08:50:31 +00:00
* @returns {Promise< string > }
2023-03-01 17:46:48 +00:00
*/
function hashLegacyRound(passphrase, salt) {
2023-03-29 08:50:31 +00:00
return pbkdf2(passphrase, salt, 1000, "SHA-1");
2023-03-01 17:46:48 +00:00
}
exports.hashLegacyRound = hashLegacyRound;
2022-11-20 14:25:16 +00:00
2023-03-01 17:46:48 +00:00
/**
* Add a second round of iterations. This is because we used to use 1k, so for backwards compatibility with
* remember-me/autodecrypt links, we need to support going from that to more iterations.
*
* @param hashedPassphrase
* @param salt
2023-03-29 08:50:31 +00:00
* @returns {Promise< string > }
2023-03-01 17:46:48 +00:00
*/
function hashSecondRound(hashedPassphrase, salt) {
2023-03-29 08:50:31 +00:00
return pbkdf2(hashedPassphrase, salt, 14000, "SHA-256");
2022-11-20 14:25:16 +00:00
}
2023-03-01 17:46:48 +00:00
exports.hashSecondRound = hashSecondRound;
2022-11-20 14:25:16 +00:00
2023-03-29 08:50:31 +00:00
/**
* Add a third round of iterations to bring total number to 600k. This is because we used to use 1k, then 15k, so for
* backwards compatibility with remember-me/autodecrypt links, we need to support going from that to more iterations.
*
* @param hashedPassphrase
* @param salt
* @returns {Promise< string > }
*/
function hashThirdRound(hashedPassphrase, salt) {
return pbkdf2(hashedPassphrase, salt, 585000, "SHA-256");
}
exports.hashThirdRound = hashThirdRound;
/**
* Salt and hash the passphrase so it can be stored in localStorage without opening a password reuse vulnerability.
*
* @param {string} passphrase
* @param {string} salt
* @param {int} iterations
* @param {string} hashAlgorithm
* @returns {Promise< string > }
*/
async function pbkdf2(passphrase, salt, iterations, hashAlgorithm) {
const key = await subtle.importKey(
"raw",
UTF8Encoder.parse(passphrase),
"PBKDF2",
false,
["deriveBits"]
);
const keyBytes = await subtle.deriveBits(
{
name: "PBKDF2",
hash: hashAlgorithm,
iterations,
salt: UTF8Encoder.parse(salt),
},
key,
256
);
return HexEncoder.stringify(new Uint8Array(keyBytes));
}
2022-11-20 14:25:16 +00:00
function generateRandomSalt() {
2023-03-29 08:50:31 +00:00
const bytes = crypto.getRandomValues(new Uint8Array(128 / 8));
return HexEncoder.stringify(new Uint8Array(bytes));
2022-11-20 14:25:16 +00:00
}
exports.generateRandomSalt = generateRandomSalt;
2023-03-29 08:50:31 +00:00
async function signMessage(hashedPassphrase, message) {
const key = await subtle.importKey(
"raw",
HexEncoder.parse(hashedPassphrase),
{
name: "HMAC",
hash: "SHA-256",
},
false,
["sign"]
);
const signature = await subtle.sign("HMAC", key, UTF8Encoder.parse(message));
return HexEncoder.stringify(new Uint8Array(signature));
}
exports.signMessage = signMessage;
2023-03-01 17:46:48 +00:00
function getRandomAlphanum() {
2023-03-29 08:50:31 +00:00
const possibleCharacters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
2023-03-01 17:46:48 +00:00
2023-03-29 08:50:31 +00:00
let byteArray;
let parsedInt;
2023-03-01 17:46:48 +00:00
// Keep generating new random bytes until we get a value that falls
// within a range that can be evenly divided by possibleCharacters.length
do {
2023-03-29 08:50:31 +00:00
byteArray = crypto.getRandomValues(new Uint8Array(1));
2023-03-01 17:46:48 +00:00
// extract the lowest byte to get an int from 0 to 255 (probably unnecessary, since we're only generating 1 byte)
2023-03-29 08:50:31 +00:00
parsedInt = byteArray[0] & 0xff;
2023-03-01 17:46:48 +00:00
} while (parsedInt >= 256 - (256 % possibleCharacters.length));
// Take the modulo of the parsed integer to get a random number between 0 and totalLength - 1
2023-03-29 08:50:31 +00:00
const randomIndex = parsedInt % possibleCharacters.length;
2023-03-01 17:46:48 +00:00
return possibleCharacters[randomIndex];
}
/**
* Generate a random string of a given length.
*
* @param {int} length
* @returns {string}
*/
function generateRandomString(length) {
2023-03-29 08:50:31 +00:00
let randomString = '';
2023-03-01 17:46:48 +00:00
2023-03-29 08:50:31 +00:00
for (let i = 0; i < length ; i + + ) {
randomString += getRandomAlphanum();
2023-03-01 17:46:48 +00:00
}
return randomString;
}
exports.generateRandomString = generateRandomString;
2022-11-20 14:25:16 +00:00
return exports;
})())
< / script >
< script id = "codec" >
window.codec = ((function(){
const exports = {};
/**
* Initialize the codec with the provided cryptoEngine - this return functions to encode and decode messages.
*
* @param cryptoEngine - the engine to use for encryption / decryption
*/
function init(cryptoEngine) {
const exports = {};
2023-03-01 17:46:48 +00:00
2022-11-20 14:25:16 +00:00
/**
* Top-level function for encoding a message.
2023-03-01 17:46:48 +00:00
* Includes password hashing, encryption, and signing.
2022-11-20 14:25:16 +00:00
*
* @param {string} msg
2023-03-01 17:46:48 +00:00
* @param {string} password
2022-11-20 14:25:16 +00:00
* @param {string} salt
*
* @returns {string} The encoded text
*/
2023-03-29 08:50:31 +00:00
async function encode(msg, password, salt) {
const hashedPassphrase = await cryptoEngine.hashPassphrase(password, salt);
const encrypted = await cryptoEngine.encrypt(msg, hashedPassphrase);
2023-03-01 17:46:48 +00:00
// we use the hashed password in the HMAC because this is effectively what will be used a password (so we can store
// it in localStorage safely, we don't use the clear text password)
2023-03-29 08:50:31 +00:00
const hmac = await cryptoEngine.signMessage(hashedPassphrase, encrypted);
2022-11-20 14:25:16 +00:00
return hmac + encrypted;
}
exports.encode = encode;
/**
* Top-level function for decoding a message.
2023-03-01 17:46:48 +00:00
* Includes signature check and decryption.
2022-11-20 14:25:16 +00:00
*
* @param {string} signedMsg
* @param {string} hashedPassphrase
2023-03-29 08:50:31 +00:00
* @param {string} salt
* @param {int} backwardCompatibleAttempt
* @param {string} originalPassphrase
2022-11-20 14:25:16 +00:00
*
* @returns {Object} {success: true, decoded: string} | {success: false, message: string}
*/
2023-03-29 08:50:31 +00:00
async function decode(
signedMsg,
hashedPassphrase,
salt,
backwardCompatibleAttempt = 0,
originalPassphrase = ''
) {
2022-11-20 14:25:16 +00:00
const encryptedHMAC = signedMsg.substring(0, 64);
const encryptedMsg = signedMsg.substring(64);
2023-03-29 08:50:31 +00:00
const decryptedHMAC = await cryptoEngine.signMessage(hashedPassphrase, encryptedMsg);
2022-11-20 14:25:16 +00:00
if (decryptedHMAC !== encryptedHMAC) {
2023-03-29 08:50:31 +00:00
// we have been raising the number of iterations in the hashing algorithm multiple times, so to support the old
// remember-me/autodecrypt links we need to try bringing the old hashes up to speed.
originalPassphrase = originalPassphrase || hashedPassphrase;
if (backwardCompatibleAttempt === 0) {
const updatedHashedPassphrase = await cryptoEngine.hashThirdRound(originalPassphrase, salt);
2023-03-01 17:46:48 +00:00
2023-03-29 08:50:31 +00:00
return decode(signedMsg, updatedHashedPassphrase, salt, backwardCompatibleAttempt + 1, originalPassphrase);
}
if (backwardCompatibleAttempt === 1) {
let updatedHashedPassphrase = await cryptoEngine.hashSecondRound(originalPassphrase, salt);
updatedHashedPassphrase = await cryptoEngine.hashThirdRound(updatedHashedPassphrase, salt);
2023-03-01 17:46:48 +00:00
2023-03-29 08:50:31 +00:00
return decode(signedMsg, updatedHashedPassphrase, salt, backwardCompatibleAttempt + 1, originalPassphrase);
2023-03-01 17:46:48 +00:00
}
2023-03-29 08:50:31 +00:00
return { success: false, message: "Signature mismatch" };
2023-03-01 17:46:48 +00:00
}
2022-11-20 14:25:16 +00:00
return {
success: true,
2023-03-29 08:50:31 +00:00
decoded: await cryptoEngine.decrypt(encryptedMsg, hashedPassphrase),
2022-11-20 14:25:16 +00:00
};
}
exports.decode = decode;
return exports;
}
exports.init = init;
return exports;
})())
< / script >
< script id = "formater" >
window.formater = ((function(){
const exports = {};
2022-11-23 22:20:46 +00:00
/**
2022-11-20 14:25:16 +00:00
* Replace the placeholder tags (between '{tag}') in the template string with provided data.
*
* @param {string} templateString
* @param {Object} data
*
* @returns string
*/
function renderTemplate(templateString, data) {
2022-12-21 09:01:21 +00:00
return templateString.replace(/{\s*(\w+)\s*}/g, function (_, key) {
2022-11-20 14:25:16 +00:00
if (data & & data[key] !== undefined) {
return data[key];
}
return "";
});
}
exports.renderTemplate = renderTemplate;
return exports;
})())
< / script >
2017-04-29 13:14:06 +00:00
< script >
2023-03-29 08:50:31 +00:00
const encode = codec.init(cryptoEngine).encode;
2022-11-20 14:25:16 +00:00
2017-06-19 08:46:47 +00:00
// enable CKEDIRTOR
2022-02-10 08:22:32 +00:00
CKEDITOR.replace('instructions');
2017-06-19 08:46:47 +00:00
2023-03-29 08:50:31 +00:00
let htmlToDownload;
2017-06-15 10:20:19 +00:00
2022-11-20 14:25:16 +00:00
/**
* Extract js code from < script > t a g a n d r e t u r n i t a s a s t r i n g
*
* @param id
* @returns
*/
2023-03-29 08:50:31 +00:00
function getScriptAsString(id) {
2022-11-20 14:25:16 +00:00
return document.getElementById(id)
.innerText.replace(/window\.\w+ = /, '');
}
2017-04-29 13:14:06 +00:00
2022-11-22 14:44:18 +00:00
/**
* Register something happened - this uses a simple Supabase function to implement a counter, and allows use to drop
2023-03-29 09:49:10 +00:00
* google analytics.
2022-11-22 14:44:18 +00:00
*
* @param action
*/
function trackEvent(action) {
2023-03-29 08:50:31 +00:00
const xhr = new XMLHttpRequest();
2022-11-22 14:44:18 +00:00
xhr.open('POST', 'https://zlgpaemmniviswibzuwt.supabase.co/rest/v1/rpc/increment_analytics', true);
xhr.setRequestHeader('Content-type', 'application/json; charset=UTF-8')
xhr.setRequestHeader('apikey', 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6InpsZ3BhZW1tbml2aXN3aWJ6dXd0Iiwicm9sZSI6ImFub24iLCJpYXQiOjE2NjkxMjM0OTcsImV4cCI6MTk4NDY5OTQ5N30.wNoVDHG7F6INx-IPotMs3fL1nudfaF2qvQDgG-1PhNI')
xhr.setRequestHeader('Authorization', 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6InpsZ3BhZW1tbml2aXN3aWJ6dXd0Iiwicm9sZSI6ImFub24iLCJpYXQiOjE2NjkxMjM0OTcsImV4cCI6MTk4NDY5OTQ5N30.wNoVDHG7F6INx-IPotMs3fL1nudfaF2qvQDgG-1PhNI')
xhr.send(
JSON.stringify({
action_input: action
}
));
}
2017-06-15 15:48:20 +00:00
/**
* Fill the password prompt template with data provided.
* @param data
*/
2023-03-29 08:50:31 +00:00
function setFileToDownload (data) {
const request = new XMLHttpRequest();
2022-11-05 10:58:21 +00:00
request.open('GET', 'lib/password_template.html', true);
2022-02-10 08:22:32 +00:00
request.onload = function () {
2023-03-29 08:50:31 +00:00
const renderedTmpl = formater.renderTemplate(request.responseText, data);
2017-04-29 13:14:06 +00:00
2023-03-29 08:50:31 +00:00
const downloadLink = document.querySelector('a.download');
2017-06-15 10:27:47 +00:00
downloadLink.href = 'data:text/html,' + encodeURIComponent(renderedTmpl);
2017-06-15 05:36:09 +00:00
downloadLink.removeAttribute('disabled');
2017-06-15 10:20:19 +00:00
htmlToDownload = renderedTmpl;
2017-04-29 13:14:06 +00:00
};
2017-06-15 05:36:09 +00:00
request.send();
2023-03-29 08:50:31 +00:00
}
2017-06-15 15:48:20 +00:00
2022-11-22 14:44:18 +00:00
// register page load
window.onload = function () {
trackEvent('show_index');
};
2017-06-15 15:48:20 +00:00
/**
* Handle form submission.
*/
2023-03-29 08:50:31 +00:00
document.getElementById('encrypt_form').addEventListener('submit', async function (e) {
2017-06-15 05:36:09 +00:00
e.preventDefault();
2017-04-29 13:14:06 +00:00
2022-11-22 14:44:18 +00:00
trackEvent('generate_encrypted');
2017-06-19 08:46:47 +00:00
// update instruction textarea value with CKEDITOR content
// (see https://stackoverflow.com/questions/3147670/ckeditor-update-textarea)
CKEDITOR.instances['instructions'].updateElement();
2023-03-29 08:50:31 +00:00
const unencrypted = document.getElementById('unencrypted_html').value,
2022-02-10 08:22:32 +00:00
passphrase = document.getElementById('passphrase').value;
2017-04-29 13:14:06 +00:00
2023-03-29 08:50:31 +00:00
const salt = cryptoEngine.generateRandomSalt();
const encryptedMsg = await encode(unencrypted, passphrase, salt);
2022-02-10 08:22:32 +00:00
2023-03-29 08:50:31 +00:00
const decryptButton = document.getElementById('decrypt_button').value,
2022-02-10 08:22:32 +00:00
instructions = document.getElementById('instructions').value,
isRememberEnabled = document.getElementById('remember').checked,
pageTitle = document.getElementById('title').value.trim(),
passphrasePlaceholder = document.getElementById('passphrase_placeholder').value.trim(),
rememberDurationInDays = document.getElementById('remember_in_days').value || 0,
rememberMe = document.getElementById('remember_me').value;
2017-04-29 13:14:06 +00:00
2023-03-29 08:50:31 +00:00
const data = {
2022-02-10 08:22:32 +00:00
crypto_tag: '< script src = "https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.9-1/crypto-js.min.js" integrity = "sha384-lp4k1VRKPU9eBnPePjnJ9M2RF3i7PC30gXs70+elCVfgwLwx1tv5+ctxdtwxqZa7" crossorigin = "anonymous" > < / s c r ' + ' i p t > ' ,
decrypt_button: decryptButton ? decryptButton : 'DECRYPT',
2017-06-15 15:48:20 +00:00
encrypted: encryptedMsg,
2022-02-10 08:22:32 +00:00
instructions: instructions ? instructions : '',
is_remember_enabled: isRememberEnabled ? 'true' : 'false',
2022-11-20 14:25:16 +00:00
js_codec: getScriptAsString('codec'),
js_crypto_engine: getScriptAsString('cryptoEngine'),
2022-02-10 08:22:32 +00:00
passphrase_placeholder: passphrasePlaceholder ? passphrasePlaceholder : 'Passphrase',
remember_duration_in_days: rememberDurationInDays.toString(),
remember_me: rememberMe ? rememberMe : 'Remember me',
2022-11-20 14:25:16 +00:00
salt: salt,
2022-02-10 08:22:32 +00:00
title: pageTitle ? pageTitle : 'Protected Page',
2017-06-15 05:36:09 +00:00
};
2017-04-29 13:14:06 +00:00
2022-11-20 14:25:16 +00:00
document.getElementById('encrypted_html_display').textContent = encryptedMsg;
2017-06-14 14:19:24 +00:00
2023-03-29 08:50:31 +00:00
setFileToDownload(data);
2017-06-15 05:36:09 +00:00
});
2017-06-14 14:19:24 +00:00
2017-06-15 05:36:09 +00:00
document.getElementById('toggle-extra-option')
2022-02-10 08:22:32 +00:00
.addEventListener('click', function (e) {
e.preventDefault();
document.getElementById('extra-options').classList.toggle('hidden');
});
2017-06-14 14:19:24 +00:00
2023-03-29 08:50:31 +00:00
let isConceptShown = false;
2017-06-15 05:36:09 +00:00
document.getElementById('toggle-concept')
2022-02-10 08:22:32 +00:00
.addEventListener('click', function (e) {
e.preventDefault();
isConceptShown = !isConceptShown;
document.getElementById('toggle-concept-sign').innerText = isConceptShown ? '▼' : '►';
document.getElementById('concept').classList.toggle('hidden');
});
2017-06-15 10:20:19 +00:00
2017-06-15 15:48:20 +00:00
/**
* Browser specific download code.
*/
2017-06-15 10:27:47 +00:00
document.getElementById('download-link')
2017-06-15 10:20:19 +00:00
.addEventListener('click', function (e) {
2022-11-22 14:44:18 +00:00
// only register the click event if there is actually a generated file
if (htmlToDownload) {
trackEvent('download_encrypted');
}
2017-06-15 10:20:19 +00:00
2023-03-29 08:50:31 +00:00
const isIE = (navigator.userAgent.indexOf("MSIE") !== -1) || (!!document.documentMode === true); // >= 10
const isEdge = navigator.userAgent.indexOf("Edge") !== -1;
2017-06-15 10:20:19 +00:00
2022-02-10 08:22:32 +00:00
// download with MS specific feature
if (htmlToDownload & & (isIE || isEdge)) {
e.preventDefault();
2023-03-29 08:50:31 +00:00
const blobObject = new Blob([htmlToDownload]);
2022-02-10 08:22:32 +00:00
window.navigator.msSaveOrOpenBlob(blobObject, 'encrypted.html');
}
2017-06-15 10:20:19 +00:00
2022-02-10 08:22:32 +00:00
return true;
})
2017-04-29 13:14:06 +00:00
< / script >
< / body >
< / html >