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 | void Digest::release(void) |
| 21 | { |
| 22 | if(context) { |
| 23 | gnutls_hash_deinit((MD_CTX)context, buffer); |
| 24 | context = NULL; |
| 25 | } |
| 26 | |
| 27 | bufsize = 0; |
| 28 | textbuf[0] = 0; |
| 29 | hashid = 0; |
| 30 | } |
| 31 | |
| 32 | int context::map_digest(const char *type) |
| 33 | { |
| 34 | if(eq_case(type, "sha") || eq_case(type, "sha1")) |
| 35 | return GNUTLS_DIG_SHA1; |
| 36 | else if(eq_case(type, "sha256")) |
| 37 | return GNUTLS_DIG_SHA256; |
| 38 | else if(eq_case(type, "sha512")) |
| 39 | return GNUTLS_DIG_SHA512; |
| 40 | else if(eq_case(type, "md5")) |
| 41 | return GNUTLS_DIG_MD5; |
| 42 | else if(eq_case(type, "md2")) |
| 43 | return GNUTLS_DIG_MD2; |
| 44 | else if(eq_case(type, "rmd160")) |
| 45 | return GNUTLS_DIG_RMD160; |
| 46 | else |
| 47 | return 0; |
| 48 | } |
| 49 | |
| 50 | void Digest::set(const char *type) |
| 51 | { |
| 52 | secure::init(); |
| 53 | |
| 54 | release(); |
| 55 | |
| 56 | hashid = context::map_digest(type); |
| 57 | |
| 58 | if(!hashid || gnutls_hash_get_len((MD_ID)hashid) < 1) { |
| 59 | hashid = 0; |
| 60 | return; |
| 61 | } |
| 62 | |
| 63 | gnutls_hash_init((MD_CTX *)&context, (MD_ID)hashid); |
| 64 | } |
| 65 | |
| 66 | bool Digest::has(const char *type) |
| 67 | { |
| 68 | MD_ID id = (MD_ID)context::map_digest(type); |
| 69 | |
| 70 | if(!id || (gnutls_hash_get_len(id) < 1)) |
| 71 | return false; |
| 72 | |
| 73 | return true; |
| 74 | } |
| 75 | |
| 76 | bool Digest::put(const void *address, size_t size) |
| 77 | { |
| 78 | if(!context || hashid == 0) |
| 79 | return false; |
| 80 | |
| 81 | gnutls_hash((MD_CTX)context, address, size); |
| 82 | return true; |
| 83 | } |
| 84 | |
| 85 | void Digest::reset(void) |
| 86 | { |
| 87 | unsigned char temp[MAX_DIGEST_HASHSIZE / 8]; |
| 88 | |
| 89 | if(context) { |
| 90 | gnutls_hash_deinit((MD_CTX)context, temp); |
| 91 | context = NULL; |
| 92 | } |
| 93 | if(hashid == 0) |
| 94 | return; |
| 95 | |
| 96 | gnutls_hash_init((MD_CTX *)&context, (MD_ID)hashid); |
| 97 | bufsize = 0; |
| 98 | } |
| 99 | |
| 100 | void Digest::recycle(bool bin) |
| 101 | { |
| 102 | unsigned size = bufsize; |
| 103 | |
| 104 | if(!context || hashid == 0) |
| 105 | return; |
| 106 | |
| 107 | if(!bufsize) { |
| 108 | gnutls_hash_deinit((MD_CTX)context, buffer); |
| 109 | context = NULL; |
| 110 | gnutls_hash_init((MD_CTX *)&context, (MD_ID)hashid); |
| 111 | } |
| 112 | else |
| 113 | Digest::reset(); |
| 114 | |
| 115 | size = gnutls_hash_get_len((MD_ID)hashid); |
| 116 | |
| 117 | if(!size || !context || !hashid) |
| 118 | return; |
| 119 | |
| 120 | if(bin) |
| 121 | gnutls_hash((MD_CTX)context, buffer, size); |
| 122 | else { |
| 123 | unsigned count = 0; |
| 124 | |
| 125 | while(count < size) { |
| 126 | snprintf(textbuf + (count * 2), 3, "%2.2x", |
| 127 | buffer[count]); |
| 128 | ++count; |
| 129 | |
| 130 | } |
| 131 | gnutls_hash((MD_CTX)context, textbuf, size * 2); |
| 132 | } |
| 133 | bufsize = 0; |
| 134 | } |
| 135 | |
| 136 | const unsigned char *Digest::get(void) |
| 137 | { |
| 138 | unsigned count = 0; |
| 139 | unsigned size = 0; |
| 140 | |
| 141 | if(bufsize) |
| 142 | return buffer; |
| 143 | |
| 144 | if(!context || hashid == 0) |
| 145 | return NULL; |
| 146 | |
| 147 | gnutls_hash_deinit((MD_CTX)context, buffer); |
| 148 | size = gnutls_hash_get_len((MD_ID)hashid); |
| 149 | context = NULL; |
| 150 | bufsize = size; |
| 151 | |
| 152 | while(count < bufsize) { |
| 153 | snprintf(textbuf + (count * 2), 3, "%2.2x", |
| 154 | buffer[count]); |
| 155 | ++count; |
| 156 | } |
| 157 | return buffer; |
| 158 | } |
| 159 | |