Cryptopals WriteUp - Set 1 Challenge 1 - Converting hex to base64
Objective: To provide the reader with a detailed writeup on my solution for the Cryptopals Set 1 Challenge 1 as specified in the following link - https://cryptopals.com/sets/1/challenges/1
NOTE : The solution outlined below is not necessarily an optimized version and does not claim to be. The writeup merely serves to present the method that was undertaken to achieve the required outcome and thus forms the basis of the foundational solution.
The code for this solution can be found on my Github.
This writeup is also available in PDF form: Click to View
Table of contents
Challenge Requirement
Challenge 1 of Set 1 requires us to take a specified hex string and convert it to base64.
The challenge however does specify that all operations should be done on raw bytes and not on encoded strings.
Solution WriteUp
This section will aim to outline how we achieved the aforementioned requirements and the necessary logic and tools used to do so.
Solution Workflow
Given the challenge requirements, we can broadly assume that the solution workflow will be as follows:
In the workflow above, the solution should:
- Accept the provided Hex encoded string (and verify that it is encoded in hexadecimal)
- Decode the hex string to a raw unencoded byte form, and
- Subsequently, encode the raw byte string to a base64 encoded string
For the sake of convenience, the solution will output to screen the result of each step in the workflow.
Code Walkthrough
Language Selected: GoLang
Packages Used:
- encoding/hex
- encoding/base64
- fmt
Walkthrough:
Given that there is only a singular hex string to be decoded, we create a variable and assign the provided hex string value to the variable as follows:
|
|
We then proceed to lay the foundation for decoding the variable hex_string to an unencoded form. We do this through the code segment below:
|
|
In the code segment above, a new variable byte_string is created and is assigned the value of the decoded hex_string variable using the hex.DecodeString function.
In addition, it also includes an error handling (which was included in these circumstance to ensure the solution worked as intended) which can be omitted if not required.
Upon decoding, the code above outputs the results to screen using the Println function within the fmt package by formatting the unencoded value within the byte_string into a string.
Running the code up to this point (and assuming no errors) should render the following results:
|
|
Given that the output above looks correct thus far (read: It actually forms a readable sentence), we can assume that we can proceed to the next step, which is to re-encode it to base64 (or radix-64 representation).
We achieve it through the following line of code (which is explained below):
|
|
The line of code above does the following:
- Declares an empty slice of type byte called base64_encode
- Calls the base64 package and employs the StdEncoding variable and the EncodeToString function to encode the value raw value held by the byte_string variable
- Stores the result of the encoding in the base64_encode slice
The solution then outputs the value of the base64_encode slice to screen for verification, and this is achieved again through the Println function within the fmt package using the line of code below:
|
|
Running the code now (and assuming no errors) should render the following results:
|
|
Upon inspection, the value on screen does match the required outcome, thus confirming that the solution works.
#QED
Author |
Brian Ritchie |
---|---|
Last Modified |
Tuesday, August 8, 2017 |
License |
|