summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEmmanuel Gil Peyrot <linkmauve@linkmauve.fr>2017-02-24 17:05:29 +0000
committerEmmanuel Gil Peyrot <linkmauve@linkmauve.fr>2017-02-24 17:05:29 +0000
commit23927711c1579c17f885a7a40a0ba595e08930a4 (patch)
tree52538e8757da1a26334f81e60bb06381d8efaff6
parent5ef524d3b13194e44e8674da05dc211aa2c31036 (diff)
downloadpoezio-23927711c1579c17f885a7a40a0ba595e08930a4.tar.gz
poezio-23927711c1579c17f885a7a40a0ba595e08930a4.tar.bz2
poezio-23927711c1579c17f885a7a40a0ba595e08930a4.tar.xz
poezio-23927711c1579c17f885a7a40a0ba595e08930a4.zip
Stop using SHA-1 for generated filenames.
-rw-r--r--poezio/xhtml.py11
1 files changed, 8 insertions, 3 deletions
diff --git a/poezio/xhtml.py b/poezio/xhtml.py
index 6ab48733..8b191ad2 100644
--- a/poezio/xhtml.py
+++ b/poezio/xhtml.py
@@ -12,10 +12,10 @@ xhtml code to shell colors,
poezio colors to xhtml code
"""
-import base64
import curses
import hashlib
import re
+from base64 import b64encode, b64decode
from os import path
from slixmpp.xmlstream import ET
from urllib.parse import unquote
@@ -292,6 +292,11 @@ def parse_css(css):
def trim(string):
return re.sub(whitespace_re, ' ', string)
+def get_hash(data: bytes) -> bytes:
+ # Currently using SHA-256, this might change in the future.
+ # base64 gives shorter hashes than hex, so use that.
+ return b64encode(hashlib.sha256(data).digest()).rstrip(b'=')
+
class XHTMLHandler(sax.ContentHandler):
def __init__(self, force_ns=False, tmp_dir=None, extract_images=False):
self.builder = []
@@ -349,8 +354,8 @@ class XHTMLHandler(sax.ContentHandler):
elif name == 'img':
if re.match(xhtml_data_re, attrs['src']) and self.extract_images:
type_, data = [i for i in re.split(xhtml_data_re, attrs['src']) if i]
- bin_data = base64.b64decode(unquote(data))
- filename = hashlib.sha1(bin_data).hexdigest() + '.' + type_
+ bin_data = b64decode(unquote(data))
+ filename = get_hash(bin_data) + '.' + type_
filepath = path.join(self.tmp_dir, filename)
if not path.exists(filepath):
try: