feat: v1.3-konforme API, HTTP-Fixes, Base64, Example erweitert; Makefile & README Setup
This commit is contained in:
@@ -1,6 +1,28 @@
|
||||
#include "json_parser.h"
|
||||
#include <iostream>
|
||||
#include <chrono>
|
||||
#include <cryptopp/base64.h>
|
||||
#include <cryptopp/filters.h>
|
||||
#include <cryptopp/queue.h>
|
||||
|
||||
// Helper: Base64 encode without line breaks
|
||||
static std::string encode_base64(const std::vector<unsigned char>& data) {
|
||||
std::string result;
|
||||
CryptoPP::StringSource ss(
|
||||
data.data(), data.size(), true,
|
||||
new CryptoPP::Base64Encoder(new CryptoPP::StringSink(result), false /* insertLineBreaks */)
|
||||
);
|
||||
return result;
|
||||
}
|
||||
|
||||
// Helper: Base64 decode
|
||||
static std::vector<unsigned char> decode_base64(const std::string& b64) {
|
||||
std::string decoded;
|
||||
CryptoPP::StringSource ss(
|
||||
b64, true, new CryptoPP::Base64Decoder(new CryptoPP::StringSink(decoded))
|
||||
);
|
||||
return std::vector<unsigned char>(decoded.begin(), decoded.end());
|
||||
}
|
||||
|
||||
json JsonParser::create_paste_json(const std::vector<unsigned char>& cipher_text,
|
||||
const std::vector<unsigned char>& auth_tag,
|
||||
@@ -11,17 +33,16 @@ json JsonParser::create_paste_json(const std::vector<unsigned char>& cipher_text
|
||||
bool burn_after_reading,
|
||||
bool open_discussion) {
|
||||
|
||||
// Convert binary data to base64 strings (stub implementation)
|
||||
std::string cipher_text_b64(cipher_text.begin(), cipher_text.end());
|
||||
std::string auth_tag_b64(auth_tag.begin(), auth_tag.end());
|
||||
std::string iv_b64(iv.begin(), iv.end());
|
||||
std::string salt_b64(salt.begin(), salt.end());
|
||||
// Convert binary data to base64 strings
|
||||
std::string cipher_text_b64 = encode_base64(cipher_text);
|
||||
std::string iv_b64 = encode_base64(iv);
|
||||
std::string salt_b64 = encode_base64(salt);
|
||||
|
||||
// Get current timestamp
|
||||
auto now = std::chrono::duration_cast<std::chrono::seconds>(
|
||||
std::chrono::system_clock::now().time_since_epoch()).count();
|
||||
|
||||
// Create the metadata array
|
||||
// Create the metadata array according to PrivateBin v2
|
||||
json metadata = {
|
||||
{
|
||||
iv_b64, // base64(cipher_iv)
|
||||
@@ -33,21 +54,19 @@ json JsonParser::create_paste_json(const std::vector<unsigned char>& cipher_text
|
||||
"gcm", // mode
|
||||
"zlib" // compression
|
||||
},
|
||||
format, // format
|
||||
format, // formatter key
|
||||
burn_after_reading ? 1 : 0, // burn after reading
|
||||
open_discussion ? 1 : 0 // open discussion
|
||||
};
|
||||
|
||||
// Create the main JSON structure
|
||||
// JSON close to v1.3 JSON-LD: meta.expire used by server
|
||||
json paste_json = {
|
||||
{"v", 2}, // version
|
||||
{"adata", metadata}, // metadata
|
||||
{"ct", cipher_text_b64}, // cipher text
|
||||
{"v", 2}, // version
|
||||
{"adata", metadata}, // metadata
|
||||
{"ct", cipher_text_b64}, // ciphertext (base64)
|
||||
{"meta", {
|
||||
{"expire", expiration},
|
||||
{"created", now},
|
||||
{"time_to_live", 300}, // This would be calculated based on expiration
|
||||
{"icon", "data:image/png;base64,..."} // Placeholder
|
||||
{"expire", expiration}
|
||||
}}
|
||||
};
|
||||
|
||||
@@ -62,29 +81,32 @@ bool JsonParser::parse_paste_json(const json& json_data,
|
||||
std::string& expiration) {
|
||||
|
||||
try {
|
||||
// Extract cipher text
|
||||
std::string ct = json_data["ct"];
|
||||
cipher_text = std::vector<unsigned char>(ct.begin(), ct.end());
|
||||
// Extract and decode cipher text
|
||||
std::string ct_b64 = json_data.at("ct");
|
||||
cipher_text = decode_base64(ct_b64);
|
||||
|
||||
// Extract metadata
|
||||
json adata = json_data["adata"];
|
||||
// Optional explicit tag field
|
||||
if (json_data.contains("tag")) {
|
||||
std::string tag_b64 = json_data.at("tag");
|
||||
auth_tag = decode_base64(tag_b64);
|
||||
} else {
|
||||
auth_tag.clear();
|
||||
}
|
||||
|
||||
// Extract metadata and decode IV & salt
|
||||
json adata = json_data.at("adata");
|
||||
if (adata.size() >= 1) {
|
||||
json first_element = adata[0];
|
||||
if (first_element.is_array() && first_element.size() >= 2) {
|
||||
// Extract IV and salt (from base64 in real implementation)
|
||||
std::string iv_str = first_element[0];
|
||||
std::string salt_str = first_element[1];
|
||||
|
||||
iv = std::vector<unsigned char>(iv_str.begin(), iv_str.end());
|
||||
salt = std::vector<unsigned char>(salt_str.begin(), salt_str.end());
|
||||
std::string iv_b64 = first_element[0];
|
||||
std::string salt_b64 = first_element[1];
|
||||
iv = decode_base64(iv_b64);
|
||||
salt = decode_base64(salt_b64);
|
||||
}
|
||||
}
|
||||
|
||||
// Extract expiration
|
||||
expiration = json_data["meta"]["expire"];
|
||||
|
||||
// Auth tag would be extracted from the ciphertext in a real implementation
|
||||
auth_tag.resize(16, 0);
|
||||
expiration = json_data.at("meta").at("expire");
|
||||
|
||||
return true;
|
||||
} catch (...) {
|
||||
@@ -101,17 +123,13 @@ bool JsonParser::parse_response(const std::string& response,
|
||||
|
||||
try {
|
||||
json json_response = json::parse(response);
|
||||
|
||||
status = json_response["status"];
|
||||
|
||||
status = json_response.value("status", 1);
|
||||
if (status == 0) {
|
||||
// Success response
|
||||
paste_id = json_response["id"];
|
||||
url = json_response["url"];
|
||||
delete_token = json_response["deletetoken"];
|
||||
paste_id = json_response.value("id", "");
|
||||
url = json_response.value("url", "");
|
||||
delete_token = json_response.value("deletetoken", "");
|
||||
} else {
|
||||
// Error response
|
||||
message = json_response["message"];
|
||||
message = json_response.value("message", "");
|
||||
}
|
||||
|
||||
return true;
|
||||
|
||||
Reference in New Issue
Block a user