faster is_valid_blobhash

This commit is contained in:
Jack Robison 2019-02-08 19:54:59 -05:00
parent aeb2891baa
commit 45404b6ad4
No known key found for this signature in database
GPG key ID: DF25C68FE0239BB2

View file

@ -1,4 +1,5 @@
import os
import re
import asyncio
import binascii
import logging
@ -21,6 +22,9 @@ def is_valid_hashcharacter(char: str) -> bool:
return char in "0123456789abcdef"
_hexmatch = re.compile("^[a-f,0-9]+$")
def is_valid_blobhash(blobhash: str) -> bool:
"""Checks whether the blobhash is the correct length and contains only
valid characters (0-9, a-f)
@ -29,8 +33,7 @@ def is_valid_blobhash(blobhash: str) -> bool:
@return: True/False
"""
return len(blobhash) == blobhash_length and all(is_valid_hashcharacter(l) for l in blobhash)
return len(blobhash) == blobhash_length and _hexmatch.match(blobhash)
def encrypt_blob_bytes(key: bytes, iv: bytes, unencrypted: bytes) -> typing.Tuple[bytes, str]:
cipher = Cipher(AES(key), modes.CBC(iv), backend=backend)