Created
November 24, 2023 03:11
-
-
Save shearichard/43e58acd3a48b975634394fcbb5453a4 to your computer and use it in GitHub Desktop.
Using Python to guess whether a string in Base64
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Found at https://stackoverflow.com/a/45928164/364088 | |
# | |
# Not flawless | |
# | |
import base64 | |
# | |
def isBase64(sb): | |
try: | |
if isinstance(sb, str): | |
# If there's any unicode here, an exception will be | |
#thrown and the function will return false | |
sb_bytes = bytes(sb, 'ascii') | |
elif isinstance(sb, bytes): | |
sb_bytes = sb | |
else: | |
raise ValueError("Argument must be string or bytes") | |
# | |
return base64.b64encode(base64.b64decode(sb_bytes)) == sb_bytes | |
except Exception: | |
return False | |
def main(): | |
print(isBase64('hello')) | |
if __name__ == "__main__": | |
main() | |
~ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment