From 45404b6ad4d562c256105f65dd3d60b61c97a162 Mon Sep 17 00:00:00 2001 From: Jack Robison Date: Fri, 8 Feb 2019 19:54:59 -0500 Subject: [PATCH] faster is_valid_blobhash --- lbrynet/blob/blob_file.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/lbrynet/blob/blob_file.py b/lbrynet/blob/blob_file.py index 9d6246d09..69790f261 100644 --- a/lbrynet/blob/blob_file.py +++ b/lbrynet/blob/blob_file.py @@ -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)