Skip to content

Instantly share code, notes, and snippets.

@shearichard
Created November 24, 2023 03:11
Show Gist options
  • Save shearichard/43e58acd3a48b975634394fcbb5453a4 to your computer and use it in GitHub Desktop.
Save shearichard/43e58acd3a48b975634394fcbb5453a4 to your computer and use it in GitHub Desktop.
Using Python to guess whether a string in Base64
# 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