C++11S/MIME:具有安全功能的简单MIME解析器和生成器

这是我的简单MIME解析器和生成器的版本2,该版本使用C++11元素和Win32API进行快速处理。

对于S/MIME,库现在使用我的AdES。

从此处包括QP解码器。

单个消息生成器

// Single MessageMIME2::CONTENT c;

c["MIME-Version"] = "1.0";
c["Content-Type"] = "text/plain";
c.SetData("Hello");auto str = c.SerializeToVector();12345678复制代码类型:[cpp]

结果:

MIME-Version: 1.0Content-Type: text/plain

Hello1234复制代码类型:[cpp]

一些二进制消息

MIME2::CONTENT c;
c["MIME-Version"] = "1.0";
c["Content-Type"] = "application/octet-stream";
c["Content-Transfer-Encoding"] = "base64";
c["Content-Disposition"] = "attachment; filename=\"hello.txt\"";string out = MIME2::Char2Base64("Hello", 5);
c.SetData(out.c_str());auto str = c.SerializeToVector();123456789复制代码类型:[cpp]

结果:

MIME-Version: 1.0Content-Type: application/octet-stream
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="hello.txt"SGVsbG8=123456复制代码类型:[cpp]

多部分生成器

MIME2::CONTENTBUILDER cb;
MIME2::CONTENT e1;
MIME2::CONTENT e2;
e1["Content-Type"] = "text/plain";
e1.SetData("Hello\r\n\r\n");
e2["Content-Type"] = "text/html";
e2.SetData("<b>Hello</b>");

cb.Add(e1);
cb.Add(e2);

MIME2::CONTENT cc;
cb.Build(cc, "multipart/alternative");auto str = cc.SerializeToVector();123456789101112131415复制代码类型:[cpp]

结果:

MIME-Version: 1.0Content-Type: multipart/alternative; boundary="{79EAC9E2-BAF9-11CE-8C82-00AA004BA90B}"--{79EAC9E2-BAF9-11CE-8C82-00AA004BA90B}
Content-Type: text/plain

Hello

--{79EAC9E2-BAF9-11CE-8C82-00AA004BA90B}
Content-Type: text/html

<b>Hello</b>

--{79EAC9E2-BAF9-11CE-8C82-00AA004BA90B}--1234567891011121314复制代码类型:[cpp]

简单解析

string str =
R"(MIME-Version: 1.0
Content-Type: text/plain

Hello)";

    MIME2::CONTENT c;    if (c.Parse(str.c_str()) != MIME2::MIMEERR::OK)        return;    auto a1 = c.hval("Content-Type"); // a1 = "text/plain"
    auto a2 = c.GetData(); // vector<char> with the data123456789101112复制代码类型:[cpp]

解析多个

string str = "MIME-Version: 1.0\r\nContent-Type: multipart/alternative; boundary=\"{79EAC9E2-BAF9-11CE-8C82-00AA004BA90B}\"\r\n\r\n\r\n--{79EAC9E2-BAF9-11CE-8C82-00AA004BA90B}\r\nContent-Type: text/plain\r\n\r\nHello\r\n\r\n--{79EAC9E2-BAF9-11CE-8C82-00AA004BA90B}\r\nContent-Type: text/html\r\n\r\n<b>Hello</b>\r\n\r\n--{79EAC9E2-BAF9-11CE-8C82-00AA004BA90B}--";

    MIME2::CONTENT c;    if (c.Parse(str.c_str()) != MIME2::MIMEERR::OK)        return;    auto a1 = c.hval("Content-Type","boundary"); // a1 = the boundary
    if (a1.empty())        return;    vector<MIME2::CONTENT> Contents;
    MIMELIB::ParseMultipleContent(str.c_str(), a1.c_str(), Contents);    // Should have 2
    vector<char> d;
    Contents[1].DecodeData(d); // Decodes from Base64 or Quoted-Printable
    // d = "<b>Hello</b>"123456789101112131415161718192021222324252627282930复制代码类型:[cpp]

S/MIME

对于S/MIME,该库现在使用AdES。您必须#defineMIME_CMS使用S/MIME。

MIMEERR Encrypt(CONTENT& c, std::vector<PCCERT_CONTEXT> certs, bool BinaryOutput = false);MIMEERR Decrypt(CONTENT& c);MIMEERR Sign(CONTENT& co, std::vector<PCCERT_CONTEXT> certs,      std::vector<PCCERT_CONTEXT> addcerts, const wchar_t* TimeStampServer = 0,      bool Attached = true, bool BinaryOutput = false);MIMEERR Verify(vector<PCCERT_CONTEXT>* Certs = 0, AdES::CLEVEL* plev = 0);123456复制代码类型:[cpp]

HTTP支持

vector<char> data = "HTTP 1/1 200 OK\r\n...."c.Parse(data.data(),true); // true indicates a possible HTTP headerauto mh = c1.httphdr();    // Gets the header123复制代码类型:[java]

或建立:

c.AddHTTPHeader("HTTP 1/1 200 OK");
(0)

相关推荐