00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "nabstracthash.h"
00023
00028 NAbstractHash::NAbstractHash(void)
00029 : m_digest_context(),
00030 m_message_digest(NULL),
00031 m_total(0),
00032 m_hash_buffer_size(0)
00033 {
00034
00035 }
00036
00037 NAbstractHash::~NAbstractHash(void) {}
00038
00039 void NAbstractHash::init(const NString &hash_name) {
00040 OpenSSL_add_all_digests();
00041
00042 assert(hash_name.toChar());
00043
00044 m_message_digest = EVP_get_digestbyname(hash_name.toChar());
00045 if (!m_message_digest) {
00046 throw NException(NSTR("Unknown message digest: ") + hash_name,
00047 NException::SECURITY);
00048 }
00049
00050 EVP_MD_CTX_init(&m_digest_context);
00051 EVP_DigestInit_ex(&m_digest_context, m_message_digest, NULL);
00052 }
00053
00054
00055 int NAbstractHash::update(const void *data, int bytes) {
00056 EVP_DigestUpdate(&m_digest_context, data, bytes);
00057 m_total += bytes;
00058
00059 return m_total;
00060 }
00061
00062
00063 NString NAbstractHash::cleanup(void) {
00064 char *tmp = p_cleanup();
00065 NString ret = tmp;
00066
00067 delete[] tmp;
00068 return ret;
00069 }
00070
00071 char *NAbstractHash::p_cleanup(void) {
00072 unsigned char md_value[EVP_MAX_MD_SIZE];
00073 unsigned int md_len;
00074 char *out_hash = NULL;
00075
00076 assert(m_hash_buffer_size);
00077
00078 memset(md_value, 0, sizeof(md_value));
00079 EVP_DigestFinal_ex(&m_digest_context, md_value, &md_len);
00080 EVP_MD_CTX_cleanup(&m_digest_context);
00081
00082 try {
00083 out_hash = new char[m_hash_buffer_size];
00084 memset(out_hash, 0, m_hash_buffer_size);
00085 }
00086 catch (std::bad_alloc &) {
00087 throw NException(N_ENOMEM, NException::SECURITY);
00088 }
00089
00090
00091 for(unsigned int i = 0; i < md_len; i++) {
00092 char tmp[3];
00093 memset(tmp, 0, sizeof(tmp));
00094
00095 #if !defined(_MSC_VER)
00096 snprintf(tmp, sizeof(tmp), "%02x", (md_value[i] & 0x000000FF));
00097 #else
00098 _snprintf_s(tmp, sizeof(tmp), "%02x", (md_value[i] & 0x000000FF));
00099 #endif
00100 strcat(out_hash, tmp);
00101 }
00102
00103 return out_hash;
00104 }
00105
00106
00107 bool NAbstractHash::match(const NString &str, const NString &hash) {
00108 if (calculate(str).getValue() == hash) {
00109 return true;
00110 }
00111
00112 return false;
00113 }
00114
00115
00116 bool NAbstractHash::match(const NString &str, const NHash &hash) {
00117 if (calculate(str) == hash) {
00118 return true;
00119 }
00120
00121 return false;
00122 }
00123
00124 bool NAbstractHash::match(NFile *file, const NString &hash) {
00125 assert(file);
00126
00127 if (calculateFile(file).getValue() == hash) {
00128 return true;
00129 }
00130
00131 return false;
00132 }
00133
00134
00135 bool NAbstractHash::match(NFile *file, const NHash &hash) {
00136 assert(file);
00137
00138 if (calculateFile(file) == hash) {
00139 return true;
00140 }
00141
00142 return false;
00143 }
00144
00145
00146 void NAbstractHash::setHashBufferSize(int size) {
00147 m_hash_buffer_size = size;
00148 }
00149
00150
00151 int NAbstractHash::getHashBufferSize(void) const {
00152 return m_hash_buffer_size;
00153 }
00154