hasher: Add cryptographic hashing functions.

For finding exact matches.
This commit is contained in:
Stuart Longland 2018-11-28 06:10:51 +10:00
parent 008da849b8
commit ebd10eee22
Signed by: stuartl
GPG Key ID: 6AA32EFB18079BAA
2 changed files with 16 additions and 8 deletions

View File

@ -7,6 +7,7 @@ from PIL import Image
from sys import exc_info
from io import BytesIO
import binascii
import hashlib
class ImageHasher(object):
@ -22,7 +23,8 @@ class ImageHasher(object):
log = self._log.getChild('avatar[%d]' % avatar.avatar_id)
future = Future()
if not hasattr(imagehash, algorithm):
if not (hasattr(imagehash, algorithm) or \
hasattr(hashlib, algorithm)):
raise ValueError('unknown algorithm %s' % algorithm)
# Handing the value back to the coroutine
@ -37,14 +39,19 @@ class ImageHasher(object):
# What to do in the thread pool
def _do_hash(image_data, algorithm):
try:
log.audit('Opening image')
image = Image.open(BytesIO(image_data))
if hasattr(hashlib, algorithm):
algofunc = getattr(hashlib, algorithm)
self._io_loop.add_callback(_on_done,
algofunc(image_data).digest())
else:
log.audit('Opening image')
image = Image.open(BytesIO(image_data))
algofunc = getattr(imagehash, algorithm)
res = algofunc(image)
algofunc = getattr(imagehash, algorithm)
res = algofunc(image)
self._io_loop.add_callback(_on_done,
binascii.a2b_hex(str(res)))
self._io_loop.add_callback(_on_done,
binascii.a2b_hex(str(res)))
except:
log.exception('Failed to hash')
self._io_loop.add_callback(_on_done, exc_info())

View File

@ -818,7 +818,8 @@ class HADSHApp(Application):
(r"/", RootHandler),
(r"/login", LoginHandler),
(r"/avatar/([0-9]+)", AvatarHandler),
(r"/avatar/([a-z_]+hash)/([0-9]+)", AvatarHashHandler),
(r"/avatar/(average_hash|dhash|phash|whash|sha512)/([0-9]+)", \
AvatarHashHandler),
(r"/user/([0-9]+)", UserHandler),
(r"/word/([0-9]+)", WordHandler),
(r"/wordadj/([0-9]+)", WordAdjacencyHandler),