Devessier / ssl

Clone of openssl in C for 42 School
GNU General Public License v3.0
0 stars 0 forks source link

Implement Base64 command #24

Closed Devessier closed 3 years ago

Devessier commented 3 years ago

We have to create a command named base64 that will encode or decode strings into/from Base64.

The command must have to following flags:

If -i flag is not specified, read from stdin. If -o flag is not specified, write to sdout.

Parameters can be overridden: if -i flag is specified two times, we keep the value of the last one. This should also apply to -e and -d flags. -e sets the mode to encoding, while -d sets the mode to decoding. One way to implement that could be to have a field of enumeration type.

Base64 specifications

The Base64 algorithm uses a dictionary of 64 characters: https://en.wikipedia.org/wiki/Base64#Base64_table

Encoding

The objective of this algorithm is to transform binary data into printable text data. To do that, the algorithm expands three 8-bit words into four 6-bit words. The relation is defined by 3 8 = 4 6. As resulting words can not physically be stored on 6-bit words, they are put in 8-bit words. The message bit length has then been multiplied by 4/3.

If the last input group can not contain three bytes, the third sextet will be padded with two 0 and the last output byte will be the padding character =. If the last input group can only contain the first byte, the second sextet will be padded with four 0 and the two last output bytes will be the padding character =.

According to -b (b for break) argument, we may need to add newlines

Decoding

See https://en.wikipedia.org/wiki/Base64#Decoding_Base64_with_padding

As macOS base64 command we will handle only decoding with padding:

Capture d’écran 2021-03-16 à 16 50 03

Space characters are simply discarded. Characters not present in Base64 dictionary throw an error.

Capture d’écran 2021-03-16 à 17 03 39