[fix] refactor encryption function

This commit is contained in:
Adrien Beudin 2017-10-04 00:44:47 +02:00
parent f53b175939
commit 080e50f318
6 changed files with 79 additions and 86 deletions

View File

@ -17,19 +17,18 @@ import shutil
import socketserver
import http.server
import base64
from subprocess import check_output
import ruamel.yaml as yaml
from docopt import docopt
import base64
from path import Path
from jinja2 import Environment, FileSystemLoader
from .cache import CACHE
from .utils import error, warning, okgreen
from .utils import error, warning, okgreen, makeform, encrypt
DEFAULTS = {
@ -448,8 +447,6 @@ def create_cover(gallery_name, gallery_settings, gallery_path):
def build_gallery(settings, gallery_settings, gallery_path, template):
gallery_index_template = template.get_template("gallery-index.html")
page_template = template.get_template("page.html")
encrypted_template = template.get_template("encrypted.html")
from_template = template.get_template("form.html")
# this should probably be a factory
Image.base_dir = Path(".").joinpath(gallery_path)
@ -480,16 +477,8 @@ def build_gallery(settings, gallery_settings, gallery_path, template):
open(Path("build").joinpath(gallery_path, "index.html"), "wb").write(html)
if gallery_settings.get("password") or settings.get("password"):
form = base64.b64encode(from_template.render(gallery=gallery_settings, settings=settings).encode("Utf-8"))
password = gallery_settings.get("password", settings.get("password"))
index_plain = Path("build").joinpath(gallery_path, "index.html")
encrypted = check_output('cat %s | openssl enc -e -base64 -A -aes-256-cbc -pass pass:"%s"' % (index_plain, password), shell=True)
html = encrypted_template.render(
settings=settings,
gallery=gallery_settings,
form=str(form, 'utf-8'),
ciphertext=str(encrypted, 'utf-8'),
).encode("Utf-8")
html = encrypt(password, template, gallery_path, settings, gallery_settings)
open(Path("build").joinpath(gallery_path, "index.html"), "wb").write(html)
@ -529,26 +518,14 @@ def build_gallery(settings, gallery_settings, gallery_path, template):
open(Path("build").joinpath(gallery_light_path, "index.html"), "wb").write(html)
if gallery_settings.get("password") or settings.get("password"):
light_template_to_render = light_templates.get_template("encrypted.html")
from_template = light_templates.get_template("form.html")
form = base64.b64encode(from_template.render(gallery=gallery_settings, settings=settings).encode("Utf-8"))
template_to_render = encrypted_template
password = gallery_settings.get("password", settings.get("password"))
index_plain = Path("build").joinpath(gallery_light_path, "index.html")
encrypted = check_output('cat %s | openssl enc -e -base64 -A -aes-256-cbc -pass pass:"%s"' % (index_plain, password), shell=True)
html = light_template_to_render.render(
settings=settings,
gallery=gallery_settings,
form=str(form, 'utf-8'),
ciphertext=str(encrypted, 'utf-8'),
).encode("Utf-8")
html = encrypt(password, light_templates, gallery_light_path, settings, gallery_settings)
open(Path("build").joinpath(gallery_light_path, "index.html"), "wb").write(html)
def build_index(settings, galleries_cover, templates, gallery_path='', sub_index=False, gallery_settings={}):
index_template = templates.get_template("index.html")
form_template = templates.get_template("form.html")
reverse = gallery_settings.get('reverse', settings["settings"].get('reverse', False))
if reverse:
@ -574,16 +551,8 @@ def build_index(settings, galleries_cover, templates, gallery_path='', sub_index
open(Path("build").joinpath(gallery_path, "index.html"), "wb").write(html)
if settings.get("password"):
form = base64.b64encode(form_template.render(settings=settings).encode("Utf-8"))
index_template_to_render = templates.get_template("encrypted.html")
password = settings.get("password")
index_plain = Path("build").joinpath(gallery_path, "index.html")
encrypted = check_output('cat %s | openssl enc -e -base64 -A -aes-256-cbc -pass pass:"%s"' % (index_plain, password), shell=True)
html = index_template_to_render.render(
settings=settings,
form=str(form, 'utf-8'),
ciphertext=str(encrypted, 'utf-8')
).encode("Utf-8")
html = encrypt(password, templates, gallery_path, settings, None)
open(Path("build").joinpath(gallery_path, "index.html"), "wb").write(html)

View File

@ -18,6 +18,7 @@
<meta http-equiv="pragma" content="no-cache"/>
<link type="text/css" rel="stylesheet" href="{{ pathstatic }}/static/css/style-page.css" media="screen,projection"/>
</head>
<body class="staticrypt-body">
<script type="text/javascript" src="{{ pathstatic }}/static/js/crypto-js.min.js" charset="utf-8"></script>
<script>
var form = '{{ form }}';

View File

@ -1,25 +1,24 @@
<body class="staticrypt-body">
<div class="staticrypt-page">
<div class="staticrypt-form">
<div class="staticrypt-instructions">
<p class="staticrypt-title">{% if gallery %}{{ gallery.title }}{% else %}{{ settings.title }}{% endif %}</p>
</div>
<hr class="staticrypt-hr">
<form id="staticrypt-form" action="#" method="post">
<div id="error" style="color: red; padding-bottom: 10px; height: 20px;"></div>
<input id="staticrypt-password"
type="password"
name="password"
placeholder="passphrase"
autofocus/>
<input type="submit" class="staticrypt-decrypt-button" value="ENTER"/>
</form>
<div class="staticrypt-page">
<div class="staticrypt-form">
<div class="staticrypt-instructions">
<p class="staticrypt-title">{% if gallery %}{{ gallery.title }}{% else %}{{ settings.title }}{% endif %}</p>
</div>
<hr class="staticrypt-hr">
<form id="staticrypt-form" action="#" method="post">
<div id="error" style="color: red; padding-bottom: 10px; height: 20px;"></div>
<input id="staticrypt-password"
type="password"
name="password"
placeholder="passphrase"
autofocus/>
<input type="submit" class="staticrypt-decrypt-button" value="ENTER"/>
</form>
</div>
<footer>
<p>Generated using <a href="https://github.com/psycojoker/prosopopee">Prosopopée</a> · content under <a href="https://creativecommons.org/licenses/by-sa/3.0/">CC-BY-SA</a> · atom logo by <a href="https://thenounproject.com/jjjon/">Jonathan Li</a> under <a href="https://creativecommons.org/licenses/by/3.0/">CC-BY</a></p>
</footer>
</div>
<footer>
<p>Generated using <a href="https://github.com/psycojoker/prosopopee">Prosopopée</a> · content under <a href="https://creativecommons.org/licenses/by-sa/3.0/">CC-BY-SA</a> · atom logo by <a href="https://thenounproject.com/jjjon/">Jonathan Li</a> under <a href="https://creativecommons.org/licenses/by/3.0/">CC-BY</a></p>
</footer>

View File

@ -19,6 +19,7 @@
<link type="text/css" rel="stylesheet" href="{{ pathstatic }}/static/css/encrypt.css" media="screen,projection"/>
<link type="text/css" rel="stylesheet" href="{{ pathstatic }}/static/css/materialize.css" media="screen,projection"/>
</head>
<body class="staticrypt-body">
<script type="text/javascript" src="{{ pathstatic }}/static/js/crypto-js.min.js" charset="utf-8"></script>
<script>
var form = '{{ form }}';

View File

@ -1,30 +1,29 @@
<body class="staticrypt-body">
<div class="staticrypt-page">
<div class="staticrypt-form">
<div class="staticrypt-instructions">
<p class="staticrypt-title">{% if gallery %}{{ gallery.title }}{% else %}{{ settings.title }}{% endif %}</p>
</div>
<hr class="staticrypt-hr">
<form id="staticrypt-form" action="#" method="post">
<div id="error" style="color: red; padding-bottom: 10px; height: 20px;"></div>
<input id="staticrypt-password"
type="password"
name="password"
placeholder="passphrase"
autofocus/>
<button class="btn waves-effect waves-light" type="submit" name="action">ENTER
</button>
</form>
<div class="staticrypt-page">
<div class="staticrypt-form">
<div class="staticrypt-instructions">
<p class="staticrypt-title">{% if gallery %}{{ gallery.title }}{% else %}{{ settings.title }}{% endif %}</p>
</div>
<hr class="staticrypt-hr">
<form id="staticrypt-form" action="#" method="post">
<div id="error" style="color: red; padding-bottom: 10px; height: 20px;"></div>
<input id="staticrypt-password"
type="password"
name="password"
placeholder="passphrase"
autofocus/>
<button class="btn waves-effect waves-light" type="submit" name="action">ENTER
</button>
</form>
</div>
<footer id="footer-enc" class="page-footer blue-grey darken-1">
<div class="footer-copyright blue-grey darken-2">
<div class="container center">
Generated using <a href="https://github.com/psycojoker/prosopopee">Prosopopée</a> · content under <a href="https://creativecommons.org/licenses/by-sa/3.0/">CC-BY-SA</a>
</div>
</div>
</footer>
</div>
<footer id="footer-enc" class="page-footer blue-grey darken-1">
<div class="footer-copyright blue-grey darken-2">
<div class="container center">
Generated using <a href="https://github.com/psycojoker/prosopopee">Prosopopée</a> · content under <a href="https://creativecommons.org/licenses/by-sa/3.0/">CC-BY-SA</a>
</div>
</div>
</footer>

View File

@ -1,4 +1,11 @@
import sys
import base64
from subprocess import check_output
from path import Path
from jinja2 import Environment, FileSystemLoader
class bcolors:
@ -25,3 +32,20 @@ def warning(logging, warning_message):
def okgreen(logging, ok_message):
sys.stderr.write("%s%s: %s%s" % (bcolors.OKGREEN, logging, bcolors.ENDC, ok_message))
sys.stderr.write("\n")
def makeform(template, settings, *gallery_settings):
from_template = template.get_template("form.html")
form = base64.b64encode(from_template.render(settings=settings, gallery=gallery_settings).encode("Utf-8"))
return str(form, 'utf-8')
def encrypt(password, template, gallery_path, settings, *gallery_settings):
encrypted_template = template.get_template("encrypted.html")
index_plain = Path("build").joinpath(gallery_path, "index.html")
encrypted = check_output('cat %s | openssl enc -e -base64 -A -aes-256-cbc -pass pass:"%s"' % (index_plain, password), shell=True)
html = encrypted_template.render(
settings=settings,
form=makeform(template, settings, gallery_settings),
ciphertext=str(encrypted, 'utf-8'),
gallery=gallery_settings,
).encode("Utf-8")
return html