libmoost
/home/mhx/git/github/libmoost/src/digest/sha2.cpp
Go to the documentation of this file.
00001 /* vim:set ts=3 sw=3 sts=3 et: */
00028 #include <boost/cast.hpp>
00029 #include <boost/cstdint.hpp>
00030 
00031 #include "../../include/moost/digest/sha2.h"
00032 
00033 #include "rfc6234/sha.h"
00034 
00035 #define SHA2_CLASS_IMPL_(bits)                                        \
00036    class sha ## bits ## _impl                                         \
00037    {                                                                  \
00038    public:                                                            \
00039       SHA ## bits ## Context context;                                 \
00040    };                                                                 \
00041                                                                       \
00042    sha ## bits::sha ## bits()                                         \
00043       : m_impl(new sha ## bits ## _impl)                              \
00044    {                                                                  \
00045       reset();                                                        \
00046    }                                                                  \
00047                                                                       \
00048    void sha ## bits::reset()                                          \
00049    {                                                                  \
00050       SHA ## bits ## Reset(&m_impl->context);                         \
00051    }                                                                  \
00052                                                                       \
00053    void sha ## bits::add_raw(const void *data, size_t size)           \
00054    {                                                                  \
00055       SHA ## bits ## Input(&m_impl->context,                          \
00056                   reinterpret_cast<const boost::uint8_t *>(data),     \
00057                   boost::numeric_cast<unsigned int>(size));           \
00058    }                                                                  \
00059                                                                       \
00060    std::string sha ## bits::digest() const                            \
00061    {                                                                  \
00062       boost::uint8_t digest[SHA ## bits ## HashSize];                 \
00063       SHA ## bits ## Result(&m_impl->context, digest);                \
00064       return std::string(reinterpret_cast<const char *>(&digest[0]),  \
00065                          sizeof(digest));                             \
00066    }
00067 
00068 namespace moost { namespace digest {
00069 
00070 SHA2_CLASS_IMPL_(224)
00071 SHA2_CLASS_IMPL_(256)
00072 SHA2_CLASS_IMPL_(384)
00073 SHA2_CLASS_IMPL_(512)
00074 
00075 }}