2/3/2025
Cryptopals #1 - Hex and Base64
I started working my way through the cryptopals crypto challenges today. The cryptopals challenges are a series of programming challenges related to cryptography, starting from some first principles going up through more sophisticated cryptographic techniques. I'm psyched to learn more about cryptography and I'm using this as a chance to hone my Go skills.
The first challenge is simply: "Convert hex to base64". I never really considered what hex and Base64 encoding actually are. Simply put, hex (hexadecimal) encoding is a base 16 (using 0-9 and a-f) representation of binary data. In software engineering, a hexadecimal digit represents 4 bits, which means a byte is represented by two hex digits. On the other hand, Base64 is a way to encode binary data using 64 unique characters, each character representing 6 bits of data. It has traditionally been used for SMTP email attachments. Base64 winds up taking about 33% more space than the binary data. For example, a 3MB file becomes ~4MB when Base64-encoded, but this overhead is often an acceptable tradeoff for compatibility. However, Base64's true value lies in its 64-character alphabet (A-Z, a-z, 0-9, +, /) that avoids problematic bytes. Raw binary data can contain values (0-31) representing control characters like NULL (0x00), BEL (0x07), or ESC (0x1B), which might terminate strings prematurely or trigger unexpected behavior in legacy systems.
The Go standard library makes it relatively straightforward to decode hex and encode into Base64. You can see my solution here