Skip to content

Commit

Permalink
Fix OverflowError in crypto_recognizer (#1377)
Browse files Browse the repository at this point in the history
  • Loading branch information
SharonHart authored May 9, 2024
1 parent ff31243 commit 2348fff
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def validate_result(self, pattern_text: str) -> bool:
if pattern_text.startswith("1") or pattern_text.startswith("3"):
# P2PKH or P2SH address validation
try:
bcbytes = self.__decode_base58(pattern_text, 25)
bcbytes = self.__decode_base58(str.encode(pattern_text))
checksum = sha256(sha256(bcbytes[:-4]).digest()).digest()[:4]
return bcbytes[-4:] == checksum
except ValueError:
Expand All @@ -71,12 +71,15 @@ def validate_result(self, pattern_text: str) -> bool:
return False

@staticmethod
def __decode_base58(bc: str, length: int) -> bytes:
digits58 = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
def __decode_base58(bc: bytes) -> bytes:
digits58 = b"123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
origlen = len(bc)
bc = bc.lstrip(digits58[0:1])

n = 0
for char in bc:
n = n * 58 + digits58.index(char)
return n.to_bytes(length, "big")
return n.to_bytes(origlen - len(bc) + (n.bit_length() + 7) // 8, "big")

@staticmethod
def bech32_polymod(values):
Expand Down
1 change: 1 addition & 0 deletions presidio-analyzer/tests/test_crypto_recognizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ def entities():
("my wallet address is: 16Yeky6GMjeNkAiNcBY7ZhrLoMSgg1BoyZ2", 0, ()),
# Test with empty string
("", 0, (),),
("8f953371d3e85eddb89b05ed6b9e680791055315c73e1025ab5dba7bb2aee189", 0, (),),
# fmt: on
],
)
Expand Down

0 comments on commit 2348fff

Please sign in to comment.