libmoost
/home/mhx/git/github/libmoost/test/digest/sha2.cpp
Go to the documentation of this file.
00001 /* vim:set ts=3 sw=3 sts=3 et: */
00028 #include <string>
00029 #include <sstream>
00030 
00031 #include <boost/test/unit_test.hpp>
00032 #include <boost/scoped_ptr.hpp>
00033 #include <boost/cstdint.hpp>
00034 #include <boost/cast.hpp>
00035 
00036 #include "../../include/moost/digest/sha2.h"
00037 
00038 BOOST_AUTO_TEST_SUITE(moost_digest_sha2)
00039 
00040 namespace
00041 {
00042 
00043 std::string hex2bin(const std::string& hex)
00044 {
00045    BOOST_REQUIRE(hex.size() % 2 == 0);
00046    std::vector<boost::uint8_t> bin(hex.size()/2);
00047    for (size_t i = 0; i < bin.size(); ++i)
00048    {
00049       std::istringstream iss(hex.substr(2*i, 2));
00050       int byteval;
00051       iss >> std::hex >> byteval;
00052       BOOST_REQUIRE(!iss.fail());
00053       bin[i] = boost::numeric_cast<boost::uint8_t>(byteval);
00054    }
00055    return std::string(reinterpret_cast<const char *>(&bin[0]), bin.size());
00056 }
00057 
00058 struct hexdigests
00059 {
00060    std::string empty;
00061    std::string abc;
00062    std::string abc_long;
00063    std::string zero32;
00064 };
00065 
00066 template <class Sha2Impl>
00067 void sha2_test(const hexdigests& hd)
00068 {
00069    Sha2Impl d;
00070    d.add(std::string(""));
00071    BOOST_CHECK_EQUAL(d.hexdigest(), hd.empty);
00072    BOOST_CHECK_EQUAL(d.digest(), hex2bin(hd.empty));
00073 
00074    d.reset();
00075    d.add(std::string("abc"));
00076    BOOST_CHECK_EQUAL(d.hexdigest(), hd.abc);
00077 
00078    d.reset();
00079    d.add(std::string("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"));
00080    BOOST_CHECK_EQUAL(d.hexdigest(), hd.abc_long);
00081 
00082    d.reset();
00083    d.add(std::string("abc"));
00084    d.add(std::string("dbcdec"));
00085    d.add(std::string("defdefgefghfghighijhijkijkljklmklmnlmnomnopnop"));
00086    d.add(std::string("q"));
00087    BOOST_CHECK_EQUAL(d.hexdigest(), hd.abc_long);
00088 
00089    boost::scoped_ptr<moost::digest::base> b(new Sha2Impl);
00090    b->add(std::string("abc"));
00091    BOOST_CHECK_EQUAL(b->hexdigest(), hd.abc);
00092    BOOST_CHECK_EQUAL(b->digest(), hex2bin(hd.abc));
00093 
00094    b->reset();
00095    boost::uint32_t zero = 0;
00096    b->add(zero);
00097    BOOST_CHECK_EQUAL(b->hexdigest(), hd.zero32);
00098    BOOST_CHECK_EQUAL(b->digest(), hex2bin(hd.zero32));
00099 }
00100 
00101 }
00102 
00103 BOOST_AUTO_TEST_CASE(digest_sha224)
00104 {
00105    hexdigests hd;
00106    hd.empty = "d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f";
00107    hd.abc = "23097d223405d8228642a477bda255b32aadbce4bda0b3f7e36c9da7";
00108    hd.abc_long = "75388b16512776cc5dba5da1fd890150b0c6455cb4f58b1952522525";
00109    hd.zero32 = "ac2d118dd210c8401caff1e8b29fa85d29286831505da1b86a91ed63";
00110    sha2_test<moost::digest::sha224>(hd);
00111 }
00112 
00113 BOOST_AUTO_TEST_CASE(digest_sha256)
00114 {
00115    hexdigests hd;
00116    hd.empty = "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855";
00117    hd.abc = "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad";
00118    hd.abc_long = "248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1";
00119    hd.zero32 = "df3f619804a92fdb4057192dc43dd748ea778adc52bc498ce80524c014b81119";
00120    sha2_test<moost::digest::sha256>(hd);
00121 }
00122 
00123 BOOST_AUTO_TEST_CASE(digest_sha384)
00124 {
00125    hexdigests hd;
00126    hd.empty = "38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b";
00127    hd.abc = "cb00753f45a35e8bb5a03d699ac65007272c32ab0eded1631a8b605a43ff5bed8086072ba1e7cc2358baeca134c825a7";
00128    hd.abc_long = "3391fdddfc8dc7393707a65b1b4709397cf8b1d162af05abfe8f450de5f36bc6b0455a8520bc4e6f5fe95b1fe3c8452b";
00129    hd.zero32 = "394341b7182cd227c5c6b07ef8000cdfd86136c4292b8e576573ad7ed9ae41019f5818b4b971c9effc60e1ad9f1289f0";
00130    sha2_test<moost::digest::sha384>(hd);
00131 }
00132 
00133 BOOST_AUTO_TEST_CASE(digest_sha512)
00134 {
00135    hexdigests hd;
00136    hd.empty = "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e";
00137    hd.abc = "ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f";
00138    hd.abc_long = "204a8fc6dda82f0a0ced7beb8e08a41657c16ef468b228a8279be331a703c33596fd15c13b1b07f9aa1d3bea57789ca031ad85c7a71dd70354ec631238ca3445";
00139    hd.zero32 = "ec2d57691d9b2d40182ac565032054b7d784ba96b18bcb5be0bb4e70e3fb041eff582c8af66ee50256539f2181d7f9e53627c0189da7e75a4d5ef10ea93b20b3";
00140    sha2_test<moost::digest::sha512>(hd);
00141 }
00142 
00143 BOOST_AUTO_TEST_SUITE_END()