CodeRigor

LESSON

Round-trip bytes through canonical hex and Base64

Preserve arbitrary bytes, reject malformed or alternate spellings and keep encoding separate from security guarantees.

Cryptographic APIs operate on bytes. A string is not automatically hex, Base64, UTF-8 or a key. Name the representation and decode it deliberately.

Our record format uses canonical standard Base64 for payload bytes and lowercase hex for tags. Canonical syntax gives one accepted textual spelling per byte sequence; it does not make that sequence authentic.

Representation surrounds unchanged bytes
start / 3
Decode text to exact bytes, then re-encode and compare the canonical spelling. Binary zero and non-UTF-8 bytes remain valid payload data.
Representation surrounds unchanged bytes
Decode text to exact bytes, then re-encode and compare the canonical spelling. Binary zero and non-UTF-8 bytes remain valid payload data.

Hex uses two digits per byte. Base64 uses a standard alphabet and padding where required. This lab does not silently accept URL-safe, unpadded, whitespace-containing or uppercase-hex alternatives.

Some library decoders accept more than the chosen wire format. Base64’s decoder can ignore line breaks; hex decoding accepts upper case. Re-encode and compare rather than assuming successful decoding implies canonical input.

Go
package main
import ("fmt";"encoding/hex";"bytes")
func main(){
data:=[]byte{0,1,255};raw:=hex.EncodeToString(data);decoded,_:=hex.DecodeString(raw);fmt.Println(raw,bytes.Equal(data,decoded))
}

Keep errors atomic: a decoding failure returns no partial bytes to the caller. Standard decoder APIs can return decoded prefixes together with an error, so the wrapper must discard that prefix.

Bound input and output. The exact 65,536-byte payload is valid, while one byte more is not. Empty data is also valid. Test these boundaries as well as all 256 possible byte values, malformed digits, bad padding and alternate spellings.

For authentication, compare decoded tag bytes with the library’s MAC comparison function. Canonical encoding prevents alternate wire spellings, while the MAC supplies the separate integrity check.

Two independent acceptance gates
start / 3
A record first needs a valid canonical encoding. Then its decoded tag must authenticate the framed payload and context.
Two independent acceptance gates
A record first needs a valid canonical encoding. Then its decoded tag must authenticate the framed payload and context.
Go
package main
import ("fmt";"encoding/base64")
func main(){
raw:=base64.StdEncoding.EncodeToString([]byte("界"));data,_:=base64.StdEncoding.DecodeString(raw);fmt.Println(raw,len(data),string(data))
}

Complete the decoder and test an exact-limit payload, empty data, binary zero, uppercase hex and Base64 line breaks. Do not describe any successful round trip as encryption.

Next we use SHA-256 as a checksum and demonstrate what it cannot prove.

Watch & read

Free lectures, talks and readings that cover this skill from another angle. All resources →

Optional expert discussion of how Go cryptography is engineered and tested. It supports the choice of maintained libraries; the course itself supplies the introductory explanations, vectors and complete lab.