diff --git a/Crypto/Base64/Base64.cpp b/Crypto/Base64/Base64.cpp new file mode 100644 index 0000000..59d060e --- /dev/null +++ b/Crypto/Base64/Base64.cpp @@ -0,0 +1,78 @@ +#include "Base64.h" + + +static const std::string base64_chars = + "ABCDEFGHIJKLMNOPQRSTUVWXYZ" "abcdefghijklmnopqrstuvwxyz" "0123456789+/"; + + +bool Base64::Encode(const std::vector& input_data, std::string& encoded_string) +{ + std::string ret; + unsigned char const* bytes_to_encode = input_data.data(); + unsigned int in_len = input_data.size(); + + int i = 0; + int j = 0; + unsigned char char_array_3[3]; + unsigned char char_array_4[4]; + + while (in_len--) { + char_array_3[i++] = *(bytes_to_encode++); + if (i == 3) { + char_array_4[0] = (char_array_3[0] & 0xfc) >> 2; + char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4); + char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6); + char_array_4[3] = char_array_3[2] & 0x3f; + + for(i = 0; (i <4) ; i++) + ret += base64_chars[char_array_4[i]]; + i = 0; + } + } + + if (i) + { + for(j = i; j < 3; j++) + char_array_3[j] = '\0'; + + char_array_4[0] = (char_array_3[0] & 0xfc) >> 2; + char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4); + char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6); + char_array_4[3] = char_array_3[2] & 0x3f; + + for (j = 0; (j < i + 1); j++) + ret += base64_chars[char_array_4[j]]; + + while((i++ < 3)) + ret += '='; + + } + encoded_string = ret; +} + + +bool Base64::Decode(const std::string& encoded_string, std::vector& decoded_data) +{ + static const uint8_t masks[6] = { 0xFF, 0x0F, 0x03, 0x30, 0x3C, 0xFF }; + size_t in_len = encoded_string.size(); + size_t i = 0; + size_t pos = 0; + unsigned char char_array_4[4]; + decoded_data.clear(); // If just want to append to decoded_data, then don't clear here + for (size_t in_ = 0; in_ < in_len && pos != std::string::npos; in_++) + { + pos = base64_chars.find(encoded_string[in_]); + char_array_4[i++] = (pos != std::string::npos) ? pos : 0; + if (i == 4 || pos == std::string::npos) { + if (pos == std::string::npos) + i--; + for (int j = 0; j < (i-1); j++) + decoded_data.push_back(((char_array_4[j] & masks[j]) << (2*(j+1))) + + ((char_array_4[j+1] & masks[j+3]) >> (2*(2-j)))); + i = 0; + } + } + return true; +} + + diff --git a/Crypto/Base64/Base64.cpp b/Crypto/Base64/Base64.cpp new file mode 100644 index 0000000..59d060e --- /dev/null +++ b/Crypto/Base64/Base64.cpp @@ -0,0 +1,78 @@ +#include "Base64.h" + + +static const std::string base64_chars = + "ABCDEFGHIJKLMNOPQRSTUVWXYZ" "abcdefghijklmnopqrstuvwxyz" "0123456789+/"; + + +bool Base64::Encode(const std::vector& input_data, std::string& encoded_string) +{ + std::string ret; + unsigned char const* bytes_to_encode = input_data.data(); + unsigned int in_len = input_data.size(); + + int i = 0; + int j = 0; + unsigned char char_array_3[3]; + unsigned char char_array_4[4]; + + while (in_len--) { + char_array_3[i++] = *(bytes_to_encode++); + if (i == 3) { + char_array_4[0] = (char_array_3[0] & 0xfc) >> 2; + char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4); + char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6); + char_array_4[3] = char_array_3[2] & 0x3f; + + for(i = 0; (i <4) ; i++) + ret += base64_chars[char_array_4[i]]; + i = 0; + } + } + + if (i) + { + for(j = i; j < 3; j++) + char_array_3[j] = '\0'; + + char_array_4[0] = (char_array_3[0] & 0xfc) >> 2; + char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4); + char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6); + char_array_4[3] = char_array_3[2] & 0x3f; + + for (j = 0; (j < i + 1); j++) + ret += base64_chars[char_array_4[j]]; + + while((i++ < 3)) + ret += '='; + + } + encoded_string = ret; +} + + +bool Base64::Decode(const std::string& encoded_string, std::vector& decoded_data) +{ + static const uint8_t masks[6] = { 0xFF, 0x0F, 0x03, 0x30, 0x3C, 0xFF }; + size_t in_len = encoded_string.size(); + size_t i = 0; + size_t pos = 0; + unsigned char char_array_4[4]; + decoded_data.clear(); // If just want to append to decoded_data, then don't clear here + for (size_t in_ = 0; in_ < in_len && pos != std::string::npos; in_++) + { + pos = base64_chars.find(encoded_string[in_]); + char_array_4[i++] = (pos != std::string::npos) ? pos : 0; + if (i == 4 || pos == std::string::npos) { + if (pos == std::string::npos) + i--; + for (int j = 0; j < (i-1); j++) + decoded_data.push_back(((char_array_4[j] & masks[j]) << (2*(j+1))) + + ((char_array_4[j+1] & masks[j+3]) >> (2*(2-j)))); + i = 0; + } + } + return true; +} + + diff --git a/Crypto/Base64/Base64.h b/Crypto/Base64/Base64.h new file mode 100644 index 0000000..9966468 --- /dev/null +++ b/Crypto/Base64/Base64.h @@ -0,0 +1,17 @@ +#ifndef BASE64_H +#define BASE64_H + + +#include +#include + + +namespace Base64 +{ + bool Encode(const std::vector& input_data, std::string& encoded_string); + bool Decode(const std::string& encoded_string, std::vector& decoded_data); +} + + +#endif // BASE64_H + diff --git a/Crypto/Base64/Base64.cpp b/Crypto/Base64/Base64.cpp new file mode 100644 index 0000000..59d060e --- /dev/null +++ b/Crypto/Base64/Base64.cpp @@ -0,0 +1,78 @@ +#include "Base64.h" + + +static const std::string base64_chars = + "ABCDEFGHIJKLMNOPQRSTUVWXYZ" "abcdefghijklmnopqrstuvwxyz" "0123456789+/"; + + +bool Base64::Encode(const std::vector& input_data, std::string& encoded_string) +{ + std::string ret; + unsigned char const* bytes_to_encode = input_data.data(); + unsigned int in_len = input_data.size(); + + int i = 0; + int j = 0; + unsigned char char_array_3[3]; + unsigned char char_array_4[4]; + + while (in_len--) { + char_array_3[i++] = *(bytes_to_encode++); + if (i == 3) { + char_array_4[0] = (char_array_3[0] & 0xfc) >> 2; + char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4); + char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6); + char_array_4[3] = char_array_3[2] & 0x3f; + + for(i = 0; (i <4) ; i++) + ret += base64_chars[char_array_4[i]]; + i = 0; + } + } + + if (i) + { + for(j = i; j < 3; j++) + char_array_3[j] = '\0'; + + char_array_4[0] = (char_array_3[0] & 0xfc) >> 2; + char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4); + char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6); + char_array_4[3] = char_array_3[2] & 0x3f; + + for (j = 0; (j < i + 1); j++) + ret += base64_chars[char_array_4[j]]; + + while((i++ < 3)) + ret += '='; + + } + encoded_string = ret; +} + + +bool Base64::Decode(const std::string& encoded_string, std::vector& decoded_data) +{ + static const uint8_t masks[6] = { 0xFF, 0x0F, 0x03, 0x30, 0x3C, 0xFF }; + size_t in_len = encoded_string.size(); + size_t i = 0; + size_t pos = 0; + unsigned char char_array_4[4]; + decoded_data.clear(); // If just want to append to decoded_data, then don't clear here + for (size_t in_ = 0; in_ < in_len && pos != std::string::npos; in_++) + { + pos = base64_chars.find(encoded_string[in_]); + char_array_4[i++] = (pos != std::string::npos) ? pos : 0; + if (i == 4 || pos == std::string::npos) { + if (pos == std::string::npos) + i--; + for (int j = 0; j < (i-1); j++) + decoded_data.push_back(((char_array_4[j] & masks[j]) << (2*(j+1))) + + ((char_array_4[j+1] & masks[j+3]) >> (2*(2-j)))); + i = 0; + } + } + return true; +} + + diff --git a/Crypto/Base64/Base64.h b/Crypto/Base64/Base64.h new file mode 100644 index 0000000..9966468 --- /dev/null +++ b/Crypto/Base64/Base64.h @@ -0,0 +1,17 @@ +#ifndef BASE64_H +#define BASE64_H + + +#include +#include + + +namespace Base64 +{ + bool Encode(const std::vector& input_data, std::string& encoded_string); + bool Decode(const std::string& encoded_string, std::vector& decoded_data); +} + + +#endif // BASE64_H + diff --git a/Crypto/Base64/Makefile b/Crypto/Base64/Makefile new file mode 100644 index 0000000..5724b9f --- /dev/null +++ b/Crypto/Base64/Makefile @@ -0,0 +1,9 @@ +TARGET = tests +SOURCES = Base64.cpp main.cpp + +include ../../Makefile.inc + +test-report.txt: $(TARGET_BINARY) + ./$(TARGET_BINARY) >> test-report.txt + + diff --git a/Crypto/Base64/Base64.cpp b/Crypto/Base64/Base64.cpp new file mode 100644 index 0000000..59d060e --- /dev/null +++ b/Crypto/Base64/Base64.cpp @@ -0,0 +1,78 @@ +#include "Base64.h" + + +static const std::string base64_chars = + "ABCDEFGHIJKLMNOPQRSTUVWXYZ" "abcdefghijklmnopqrstuvwxyz" "0123456789+/"; + + +bool Base64::Encode(const std::vector& input_data, std::string& encoded_string) +{ + std::string ret; + unsigned char const* bytes_to_encode = input_data.data(); + unsigned int in_len = input_data.size(); + + int i = 0; + int j = 0; + unsigned char char_array_3[3]; + unsigned char char_array_4[4]; + + while (in_len--) { + char_array_3[i++] = *(bytes_to_encode++); + if (i == 3) { + char_array_4[0] = (char_array_3[0] & 0xfc) >> 2; + char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4); + char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6); + char_array_4[3] = char_array_3[2] & 0x3f; + + for(i = 0; (i <4) ; i++) + ret += base64_chars[char_array_4[i]]; + i = 0; + } + } + + if (i) + { + for(j = i; j < 3; j++) + char_array_3[j] = '\0'; + + char_array_4[0] = (char_array_3[0] & 0xfc) >> 2; + char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4); + char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6); + char_array_4[3] = char_array_3[2] & 0x3f; + + for (j = 0; (j < i + 1); j++) + ret += base64_chars[char_array_4[j]]; + + while((i++ < 3)) + ret += '='; + + } + encoded_string = ret; +} + + +bool Base64::Decode(const std::string& encoded_string, std::vector& decoded_data) +{ + static const uint8_t masks[6] = { 0xFF, 0x0F, 0x03, 0x30, 0x3C, 0xFF }; + size_t in_len = encoded_string.size(); + size_t i = 0; + size_t pos = 0; + unsigned char char_array_4[4]; + decoded_data.clear(); // If just want to append to decoded_data, then don't clear here + for (size_t in_ = 0; in_ < in_len && pos != std::string::npos; in_++) + { + pos = base64_chars.find(encoded_string[in_]); + char_array_4[i++] = (pos != std::string::npos) ? pos : 0; + if (i == 4 || pos == std::string::npos) { + if (pos == std::string::npos) + i--; + for (int j = 0; j < (i-1); j++) + decoded_data.push_back(((char_array_4[j] & masks[j]) << (2*(j+1))) + + ((char_array_4[j+1] & masks[j+3]) >> (2*(2-j)))); + i = 0; + } + } + return true; +} + + diff --git a/Crypto/Base64/Base64.h b/Crypto/Base64/Base64.h new file mode 100644 index 0000000..9966468 --- /dev/null +++ b/Crypto/Base64/Base64.h @@ -0,0 +1,17 @@ +#ifndef BASE64_H +#define BASE64_H + + +#include +#include + + +namespace Base64 +{ + bool Encode(const std::vector& input_data, std::string& encoded_string); + bool Decode(const std::string& encoded_string, std::vector& decoded_data); +} + + +#endif // BASE64_H + diff --git a/Crypto/Base64/Makefile b/Crypto/Base64/Makefile new file mode 100644 index 0000000..5724b9f --- /dev/null +++ b/Crypto/Base64/Makefile @@ -0,0 +1,9 @@ +TARGET = tests +SOURCES = Base64.cpp main.cpp + +include ../../Makefile.inc + +test-report.txt: $(TARGET_BINARY) + ./$(TARGET_BINARY) >> test-report.txt + + diff --git a/Crypto/Base64/main.cpp b/Crypto/Base64/main.cpp new file mode 100644 index 0000000..e08350a --- /dev/null +++ b/Crypto/Base64/main.cpp @@ -0,0 +1,61 @@ +#include "Base64.h" +#include + + +void base64_test(const char* test, const char* test_str) +{ + // Encode + std::vector input_data(test_str, test_str + strlen(test_str)); + std::string encoded_str; + Base64::Encode(input_data, encoded_str); + + // Decode + std::vector decoded_data; + Base64::Decode(encoded_str, decoded_data); + std::string decoded(decoded_data.begin(),decoded_data.end()); + + if ( decoded == test_str ) + { + printf("%s: PASS\n", test); + } + else + { + printf("%s: FAIL\n", test); + printf("String: -%s-\n", test_str); + printf("Encoded: -%s-\n", encoded_str.c_str()); + printf("Decoded: -%s-\n", decoded.c_str()); + printf("Length: -%li-\n", decoded.size()); + printf("Length: -%li-\n", strlen(test_str)); + for (int i = 0; i < decoded.size(); i++) + { + if (decoded[i] != test_str[i]) + { + printf("mismatch at character %i, %02X != %02X\n", i, decoded[i], test_str[i]); + break; + } + } + exit(-1); + } +} + + +int main() +{ + const char* test_str1 = ""; + const char* test_str2 = "The quick brown fox jumps over the lazy dog."; + const char* test_str3 = "The quick brown fox jumps over the lazy dog"; + base64_test("Test1", test_str1); + base64_test("Test2", test_str2); + base64_test("Test3", test_str3); + + // base64 tests + std::vector decoded; + Base64::Decode("dGVzdAo=", decoded); + printf("base64 test1: -%s-\n", decoded.data()); + Base64::Decode("dGVzdDEK", decoded); + printf("base64 test2: -%s-\n", decoded.data()); + Base64::Decode("dGVzdDEyCg==", decoded); + printf("base64 test3: -%s-\n", decoded.data()); +} + +