Alexandre Lision | ddd731e | 2014-01-31 11:50:08 -0500 | [diff] [blame] | 1 | // Copyright (C) 2010 David Sugar, Tycho Softworks. |
| 2 | // |
| 3 | // This file is part of GNU uCommon C++. |
| 4 | // |
| 5 | // GNU uCommon C++ is free software: you can redistribute it and/or modify |
| 6 | // it under the terms of the GNU Lesser General Public License as published |
| 7 | // by the Free Software Foundation, either version 3 of the License, or |
| 8 | // (at your option) any later version. |
| 9 | // |
| 10 | // GNU uCommon C++ is distributed in the hope that it will be useful, |
| 11 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | // GNU Lesser General Public License for more details. |
| 14 | // |
| 15 | // You should have received a copy of the GNU Lesser General Public License |
| 16 | // along with GNU uCommon C++. If not, see <http://www.gnu.org/licenses/>. |
| 17 | |
| 18 | #include "local.h" |
| 19 | |
| 20 | bool Digest::has(const char *id) |
| 21 | { |
| 22 | return (EVP_get_digestbyname(id) != NULL); |
| 23 | } |
| 24 | |
| 25 | void Digest::set(const char *type) |
| 26 | { |
| 27 | secure::init(); |
| 28 | |
| 29 | release(); |
| 30 | |
| 31 | // never use sha0 |
| 32 | if(eq_case(type, "sha")) |
| 33 | type = "sha1"; |
| 34 | |
| 35 | hashtype = (void *)EVP_get_digestbyname(type); |
| 36 | if(hashtype) { |
| 37 | context = new EVP_MD_CTX; |
| 38 | EVP_MD_CTX_init((EVP_MD_CTX *)context); |
| 39 | EVP_DigestInit_ex((EVP_MD_CTX *)context, (const EVP_MD *)hashtype, NULL); |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | void Digest::release(void) |
| 44 | { |
| 45 | if(context) |
| 46 | EVP_MD_CTX_cleanup((EVP_MD_CTX *)context); |
| 47 | |
| 48 | if(context) { |
| 49 | delete (EVP_MD_CTX *)context; |
| 50 | context = NULL; |
| 51 | } |
| 52 | |
| 53 | bufsize = 0; |
| 54 | textbuf[0] = 0; |
| 55 | } |
| 56 | |
| 57 | bool Digest::put(const void *address, size_t size) |
| 58 | { |
| 59 | if(!context) |
| 60 | return false; |
| 61 | |
| 62 | EVP_DigestUpdate((EVP_MD_CTX *)context, address, size); |
| 63 | return true; |
| 64 | } |
| 65 | |
| 66 | void Digest::reset(void) |
| 67 | { |
| 68 | if(!context) { |
| 69 | if(hashtype) { |
| 70 | context = new EVP_MD_CTX; |
| 71 | EVP_MD_CTX_init((EVP_MD_CTX *)context); |
| 72 | } |
| 73 | else |
| 74 | return; |
| 75 | } |
| 76 | |
| 77 | EVP_DigestInit_ex((EVP_MD_CTX *)context, (const EVP_MD *)hashtype, NULL); |
| 78 | bufsize = 0; |
| 79 | } |
| 80 | |
| 81 | void Digest::recycle(bool bin) |
| 82 | { |
| 83 | unsigned size = bufsize; |
| 84 | |
| 85 | if(!context) |
| 86 | return; |
| 87 | |
| 88 | if(!bufsize) |
| 89 | EVP_DigestFinal_ex((EVP_MD_CTX *)context, buffer, &size); |
| 90 | |
| 91 | EVP_DigestInit_ex((EVP_MD_CTX *)context, (const EVP_MD *)hashtype, NULL); |
| 92 | |
| 93 | if(bin) |
| 94 | EVP_DigestUpdate((EVP_MD_CTX *)context, buffer, size); |
| 95 | else { |
| 96 | unsigned count = 0; |
| 97 | while(count < size) { |
| 98 | snprintf(textbuf + (count * 2), 3, "%2.2x", |
| 99 | buffer[count]); |
| 100 | ++count; |
| 101 | } |
| 102 | EVP_DigestUpdate((EVP_MD_CTX *)context, textbuf, size * |
| 103 | 2); |
| 104 | } |
| 105 | bufsize = 0; |
| 106 | |
| 107 | } |
| 108 | |
| 109 | const unsigned char *Digest::get(void) |
| 110 | { |
| 111 | unsigned count = 0; |
| 112 | unsigned size = 0; |
| 113 | |
| 114 | if(bufsize) |
| 115 | return buffer; |
| 116 | |
| 117 | if(!context) |
| 118 | return NULL; |
| 119 | |
| 120 | EVP_DigestFinal_ex((EVP_MD_CTX *)context, buffer, &size); |
| 121 | |
| 122 | release(); |
| 123 | |
| 124 | bufsize = size; |
| 125 | |
| 126 | while(count < bufsize) { |
| 127 | snprintf(textbuf + (count * 2), 3, "%2.2x", |
| 128 | buffer[count]); |
| 129 | ++count; |
| 130 | } |
| 131 | return buffer; |
| 132 | } |
| 133 | |