[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 socketserver
import http.server import http.server
import base64
from subprocess import check_output from subprocess import check_output
import ruamel.yaml as yaml import ruamel.yaml as yaml
from docopt import docopt from docopt import docopt
import base64
from path import Path from path import Path
from jinja2 import Environment, FileSystemLoader from jinja2 import Environment, FileSystemLoader
from .cache import CACHE from .cache import CACHE
from .utils import error, warning, okgreen from .utils import error, warning, okgreen, makeform, encrypt
DEFAULTS = { DEFAULTS = {
@ -448,8 +447,6 @@ def create_cover(gallery_name, gallery_settings, gallery_path):
def build_gallery(settings, gallery_settings, gallery_path, template): def build_gallery(settings, gallery_settings, gallery_path, template):
gallery_index_template = template.get_template("gallery-index.html") gallery_index_template = template.get_template("gallery-index.html")
page_template = template.get_template("page.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 # this should probably be a factory
Image.base_dir = Path(".").joinpath(gallery_path) 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) open(Path("build").joinpath(gallery_path, "index.html"), "wb").write(html)
if gallery_settings.get("password") or settings.get("password"): 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")) password = gallery_settings.get("password", settings.get("password"))
index_plain = Path("build").joinpath(gallery_path, "index.html") html = encrypt(password, template, gallery_path, settings, gallery_settings)
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")
open(Path("build").joinpath(gallery_path, "index.html"), "wb").write(html) 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) open(Path("build").joinpath(gallery_light_path, "index.html"), "wb").write(html)
if gallery_settings.get("password") or settings.get("password"): 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") from_template = light_templates.get_template("form.html")
form = base64.b64encode(from_template.render(gallery=gallery_settings, settings=settings).encode("Utf-8")) html = encrypt(password, light_templates, gallery_light_path, settings, gallery_settings)
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")
open(Path("build").joinpath(gallery_light_path, "index.html"), "wb").write(html) 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={}): def build_index(settings, galleries_cover, templates, gallery_path='', sub_index=False, gallery_settings={}):
index_template = templates.get_template("index.html") index_template = templates.get_template("index.html")
form_template = templates.get_template("form.html")
reverse = gallery_settings.get('reverse', settings["settings"].get('reverse', False)) reverse = gallery_settings.get('reverse', settings["settings"].get('reverse', False))
if reverse: 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) open(Path("build").joinpath(gallery_path, "index.html"), "wb").write(html)
if settings.get("password"): 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") password = settings.get("password")
index_plain = Path("build").joinpath(gallery_path, "index.html") html = encrypt(password, templates, gallery_path, settings, None)
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")
open(Path("build").joinpath(gallery_path, "index.html"), "wb").write(html) open(Path("build").joinpath(gallery_path, "index.html"), "wb").write(html)

View File

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

View File

@ -1,5 +1,4 @@
<body class="staticrypt-body"> <div class="staticrypt-page">
<div class="staticrypt-page">
<div class="staticrypt-form"> <div class="staticrypt-form">
<div class="staticrypt-instructions"> <div class="staticrypt-instructions">
<p class="staticrypt-title">{% if gallery %}{{ gallery.title }}{% else %}{{ settings.title }}{% endif %}</p> <p class="staticrypt-title">{% if gallery %}{{ gallery.title }}{% else %}{{ settings.title }}{% endif %}</p>
@ -19,7 +18,7 @@
</form> </form>
</div> </div>
</div> </div>
<footer> <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> <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> </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/encrypt.css" media="screen,projection"/>
<link type="text/css" rel="stylesheet" href="{{ pathstatic }}/static/css/materialize.css" media="screen,projection"/> <link type="text/css" rel="stylesheet" href="{{ pathstatic }}/static/css/materialize.css" media="screen,projection"/>
</head> </head>
<body class="staticrypt-body">
<script type="text/javascript" src="{{ pathstatic }}/static/js/crypto-js.min.js" charset="utf-8"></script> <script type="text/javascript" src="{{ pathstatic }}/static/js/crypto-js.min.js" charset="utf-8"></script>
<script> <script>
var form = '{{ form }}'; var form = '{{ form }}';

View File

@ -1,5 +1,4 @@
<body class="staticrypt-body"> <div class="staticrypt-page">
<div class="staticrypt-page">
<div class="staticrypt-form"> <div class="staticrypt-form">
<div class="staticrypt-instructions"> <div class="staticrypt-instructions">
<p class="staticrypt-title">{% if gallery %}{{ gallery.title }}{% else %}{{ settings.title }}{% endif %}</p> <p class="staticrypt-title">{% if gallery %}{{ gallery.title }}{% else %}{{ settings.title }}{% endif %}</p>
@ -19,12 +18,12 @@
</form> </form>
</div> </div>
</div> </div>
<footer id="footer-enc" class="page-footer blue-grey darken-1"> <footer id="footer-enc" class="page-footer blue-grey darken-1">
<div class="footer-copyright blue-grey darken-2"> <div class="footer-copyright blue-grey darken-2">
<div class="container center"> <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> 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>
</div> </div>
</footer> </footer>

View File

@ -1,4 +1,11 @@
import sys import sys
import base64
from subprocess import check_output
from path import Path
from jinja2 import Environment, FileSystemLoader
class bcolors: class bcolors:
@ -25,3 +32,20 @@ def warning(logging, warning_message):
def okgreen(logging, ok_message): def okgreen(logging, ok_message):
sys.stderr.write("%s%s: %s%s" % (bcolors.OKGREEN, logging, bcolors.ENDC, ok_message)) sys.stderr.write("%s%s: %s%s" % (bcolors.OKGREEN, logging, bcolors.ENDC, ok_message))
sys.stderr.write("\n") 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