Skip to content

Commit cfff8c9

Browse files
committed
introduce loading as esm and crypto-es alternative
1 parent bd8cd20 commit cfff8c9

File tree

2 files changed

+47
-17
lines changed

2 files changed

+47
-17
lines changed

encryptcontent/decrypt-form.tpl.html

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,24 @@ <h1>{{ summary }}</h1>
2525
{% if inject_something %}var inject_something = {{ inject_something }};{% endif -%}
2626
{% if delete_something %}var delete_something = "{{ delete_something }}";{%- endif %}
2727
</script>
28-
{% for library in js_libraries %}
28+
{%- if esm %}
29+
<script type="module">
30+
if (!window.hasOwnProperty("init_decryptor")) {
31+
{%- if not webcrypto %}
32+
import("{{ js_libraries[0] }}").then((module) => {
33+
window["CryptoJS"] = module.default;
34+
{%- endif %}
35+
import("{{ base_path }}assets/javascripts/decrypt-contents.js");
36+
{%- if not webcrypto %}
37+
});
38+
{%- endif %}
39+
} else {
40+
init_decryptor();
41+
}
42+
</script>
43+
{%- else %}
44+
{%- for library in js_libraries %}
2945
<script type="text/javascript" src="{{ library }}"></script>
30-
{%- endfor %}
46+
{%- endfor %}
3147
<script type="text/javascript" src="{{ base_path }}assets/javascripts/decrypt-contents.js" defer></script>
48+
{%- endif %}

encryptcontent/plugin.py

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,18 @@
2929
except ImportError:
3030
string_types = str
3131

32-
JS_LIBRARIES = [
33-
['//cdnjs.cloudflare.com/ajax/libs/crypto-js/4.1.1/core.js','b55ae8027253d4d54c4f1ef3b6254646'],
34-
['//cdnjs.cloudflare.com/ajax/libs/crypto-js/4.1.1/enc-base64.js','f551ce1340a86e5edbfef4a6aefa798f'],
35-
['//cdnjs.cloudflare.com/ajax/libs/crypto-js/4.1.1/cipher-core.js','dfddc0e33faf7a794e0c3c140544490e'],
36-
['//cdnjs.cloudflare.com/ajax/libs/crypto-js/4.1.1/sha256.js','561d24c90633fb34c13537a330d12786'],
37-
['//cdnjs.cloudflare.com/ajax/libs/crypto-js/4.1.1/hmac.js','ee162ca0ed3b55dd9b2fe74a3464bb74'],
38-
['//cdnjs.cloudflare.com/ajax/libs/crypto-js/4.1.1/pbkdf2.js','b9511c07dfe692c2fd7a9ecd3f27650e'],
39-
['//cdnjs.cloudflare.com/ajax/libs/crypto-js/4.1.1/aes.js','da81b91b1b57c279c29b3469649d9b86'],
32+
CRYPTO_ES_LIBRARIES = [
33+
['//cdn.jsdelivr.net/npm/[email protected]/+esm','fd3628cef78b155ff3da3554537e2d76','crypto-es.mjs'],
34+
]
35+
36+
CRYPTO_JS_LIBRARIES = [
37+
['//cdnjs.cloudflare.com/ajax/libs/crypto-js/4.2.0/core.js','b55ae8027253d4d54c4f1ef3b6254646','core.js'],
38+
['//cdnjs.cloudflare.com/ajax/libs/crypto-js/4.2.0/enc-base64.js','f551ce1340a86e5edbfef4a6aefa798f','enc-base64.js'],
39+
['//cdnjs.cloudflare.com/ajax/libs/crypto-js/4.2.0/cipher-core.js','b9c2f3c51e3ffe719444390f47c51627','cipher-core.js'],
40+
['//cdnjs.cloudflare.com/ajax/libs/crypto-js/4.2.0/sha256.js','561d24c90633fb34c13537a330d12786','sha256.js'],
41+
['//cdnjs.cloudflare.com/ajax/libs/crypto-js/4.2.0/hmac.js','ee162ca0ed3b55dd9b2fe74a3464bb74','hmac.js'],
42+
['//cdnjs.cloudflare.com/ajax/libs/crypto-js/4.2.0/pbkdf2.js','3f2876e100b991885f606065d1342984','pbkdf2.js'],
43+
['//cdnjs.cloudflare.com/ajax/libs/crypto-js/4.2.0/aes.js','da81b91b1b57c279c29b3469649d9b86','aes.js'],
4044
]
4145

4246
PLUGIN_DIR = Path(__file__).parent.absolute()
@@ -110,6 +114,7 @@ class encryptContentPlugin(BasePlugin):
110114
('sign_files', config_options.Type(string_types, default=None)),
111115
('sign_key', config_options.Type(string_types, default='encryptcontent.key')),
112116
('webcrypto', config_options.Type(bool, default=False)),
117+
('esm', config_options.Type(bool, default=False)),
113118
('insecure_test', config_options.Type(bool, default=False)), # insecure test build
114119
# legacy features
115120
)
@@ -272,9 +277,9 @@ def __encrypt_content__(self, content, base_path, encryptcontent_path, encryptco
272277
# optionally selfhost cryptojs
273278
js_libraries = []
274279
if not self.config['webcrypto']:
275-
for jsurl in JS_LIBRARIES:
280+
for jsurl in self.setup['js_libraries']:
276281
if self.config["selfhost"]:
277-
js_libraries.append(base_path + 'assets/javascripts/cryptojs/' + jsurl[0].rsplit('/',1)[1])
282+
js_libraries.append(base_path + 'assets/javascripts/cryptojs/' + jsurl[2])
278283
else:
279284
js_libraries.append(jsurl[0])
280285

@@ -344,6 +349,8 @@ def __encrypt_content__(self, content, base_path, encryptcontent_path, encryptco
344349
'encryptcontent_keystore': json.dumps(encryptcontent_keystore),
345350
'inject_something': inject_something,
346351
'delete_something': delete_something,
352+
'webcrypto' : self.config['webcrypto'],
353+
'esm' : self.config['esm'],
347354
# add extra vars
348355
'extra': self.config['html_extra_vars']
349356
})
@@ -367,6 +374,7 @@ def __generate_decrypt_js__(self):
367374
'site_path': self.setup['site_path'],
368375
'kdf_iterations' : self.setup['kdf_iterations'],
369376
'webcrypto' : self.config['webcrypto'],
377+
'esm' : self.config['esm'],
370378
'remember_prefix': quote(self.config['remember_prefix'], safe='~()*!\''),
371379
'sharelinks' : self.config['sharelinks'],
372380
'sharelinks_incomplete' : self.config['sharelinks_incomplete'],
@@ -675,6 +683,11 @@ def on_config(self, config, **kwargs):
675683
logger.warning('INSECURE test build active. DON\'T upload the site anywhere else than "localhost".')
676684
logger.warning('---------------------------------------------------------------------------------')
677685

686+
if self.config['esm']:
687+
self.setup['js_libraries'] = CRYPTO_ES_LIBRARIES
688+
else:
689+
self.setup['js_libraries'] = CRYPTO_JS_LIBRARIES
690+
678691
def on_pre_build(self, config, **kwargs):
679692
"""
680693
The pre_build event does not alter any variables. Use this event to call pre-build scripts.
@@ -713,9 +726,9 @@ def on_pre_build(self, config, **kwargs):
713726
else:
714727
dlpath = Path(config.data['docs_dir'] + '/assets/javascripts/cryptojs/')
715728
dlpath.mkdir(parents=True, exist_ok=True)
716-
for jsurl in JS_LIBRARIES:
729+
for jsurl in self.setup['js_libraries']:
717730
dlurl = "https:" + jsurl[0]
718-
filepath = dlpath.joinpath(jsurl[0].rsplit('/',1)[1])
731+
filepath = dlpath.joinpath(jsurl[2])
719732
self.__download_and_check__(filepath, dlurl, jsurl[1])
720733

721734
@plugins.event_priority(-200)
@@ -1105,11 +1118,11 @@ def on_post_build(self, config, **kwargs):
11051118
new_entry['url'] = config.data["site_url"] + 'assets/javascripts/decrypt-contents.js'
11061119
self.setup['files_to_sign'].append(new_entry)
11071120
if not self.config['webcrypto']:
1108-
for jsurl in JS_LIBRARIES:
1121+
for jsurl in self.setup['js_libraries']:
11091122
new_entry = {}
11101123
if self.config['selfhost']:
1111-
new_entry['file'] = Path(config.data["site_dir"]).joinpath('assets/javascripts/cryptojs/' + jsurl[0].rsplit('/',1)[1])
1112-
new_entry['url'] = config.data["site_url"] + 'assets/javascripts/cryptojs/' + jsurl[0].rsplit('/',1)[1]
1124+
new_entry['file'] = Path(config.data["site_dir"]).joinpath('assets/javascripts/cryptojs/' + jsurl[2])
1125+
new_entry['url'] = config.data["site_url"] + 'assets/javascripts/cryptojs/' + jsurl[2]
11131126
else:
11141127
new_entry['file'] = ""
11151128
new_entry['url'] = "https:" + jsurl[0]

0 commit comments

Comments
 (0)