Skip to content

Instantly share code, notes, and snippets.

@yagopajarino
Last active August 10, 2023 14:47
Show Gist options
  • Save yagopajarino/175b56b1ee8277530b0dfac9d392c2ba to your computer and use it in GitHub Desktop.
Save yagopajarino/175b56b1ee8277530b0dfac9d392c2ba to your computer and use it in GitHub Desktop.
base64

Cómo saber si a bunch of chars es un texto base64 encoded?

TLDR; tiene uno o varios '=' al final de la secuencia

Por contrucción, el string de base64 enconding tiene que tener una logitud múltiplo de 4. Si la logitud del texto enconded no es múltiplo de 3 se debe agregar padding para que la salida sea múltiplo de 4.

Ej extraido de wikipedia

Input Length Outout Length Padding
light work. 11 bGlnaHQgd29yay4= 16 1
light work 10 bGlnaHQgd29yaw== 16 2
light wor 9 bGlnaHQgd29y 12 0
light wo 8 bGlnaHQgd28= 12 1
light w 7 bGlnaHQgdw== 12 2
# Ejemplo de decodificador base64 en python
import base64
encoded_string = "bGlnaHQgd29yaw=="
decoded_bytes = base64.b64decode(encoded_string)
decoded_string = decoded_bytes.decode('utf-8')
print("Decoded String:", decoded_string)
# Decoded String: light work
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment