ucommon  7.0.0
About: GNU uCommon C++ is a portable and optimized class framework for writing C++ applications that need to use threads and support concurrent synchronization, and that use sockets, XML parsing, object serialization, thread-optimized string and data structure classes, etc..
  Fossies Dox: ucommon-7.0.0.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

Loading...
Searching...
No Matches
digest.cpp
Go to the documentation of this file.
1// Copyright (C) 2010-2014 David Sugar, Tycho Softworks.
2// Copyright (C) 2015 Cherokees of Idaho.
3//
4// This file is part of GNU uCommon C++.
5//
6// GNU uCommon C++ is free software: you can redistribute it and/or modify
7// it under the terms of the GNU Lesser General Public License as published
8// by the Free Software Foundation, either version 3 of the License, or
9// (at your option) any later version.
10//
11// GNU uCommon C++ is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU Lesser General Public License for more details.
15//
16// You should have received a copy of the GNU Lesser General Public License
17// along with GNU uCommon C++. If not, see <http://www.gnu.org/licenses/>.
18
19#include "local.h"
20
21namespace ucommon {
22
24{
25 if(context) {
26 gnutls_hash_deinit((MD_CTX)context, buffer);
27 context = NULL;
28 }
29
30 bufsize = 0;
31 memset(textbuf, 0, sizeof(textbuf));
32 hashid = 0;
33}
34
35int __context::map_digest(const char *type)
36{
37 if(eq_case(type, "sha") || eq_case(type, "sha1") || eq_case(type, "sha160"))
38 return GNUTLS_DIG_SHA1;
39 else if(eq_case(type, "sha256"))
40 return GNUTLS_DIG_SHA256;
41 else if(eq_case(type, "sha512"))
42 return GNUTLS_DIG_SHA512;
43 else if(eq_case(type, "md5"))
44 return GNUTLS_DIG_MD5;
45 else if(eq_case(type, "md2"))
46 return GNUTLS_DIG_MD2;
47 else if(eq_case(type, "rmd160"))
48 return GNUTLS_DIG_RMD160;
49 else
50 return 0;
51}
52
53void Digest::set(const char *type)
54{
56
57 release();
58
60
61 if(!hashid || gnutls_hash_get_len((MD_ID)hashid) < 1) {
62 hashid = 0;
63 return;
64 }
65
66 gnutls_hash_init((MD_CTX *)&context, (MD_ID)hashid);
67}
68
69bool Digest::has(const char *type)
70{
72
73 if(!id || (gnutls_hash_get_len(id) < 1))
74 return false;
75
76 return true;
77}
78
79bool Digest::put(const void *address, size_t size)
80{
81 if(!context || hashid == 0)
82 return false;
83
84 gnutls_hash((MD_CTX)context, address, size);
85 return true;
86}
87
88void Digest::reset(void)
89{
90 uint8_t temp[MAX_DIGEST_HASHSIZE / 8];
91
92 if(context) {
93 gnutls_hash_deinit((MD_CTX)context, temp);
94 context = NULL;
95 }
96 if(hashid == 0)
97 return;
98
99 gnutls_hash_init((MD_CTX *)&context, (MD_ID)hashid);
100 bufsize = 0;
101}
102
103void Digest::recycle(bool bin)
104{
105 unsigned size = bufsize;
106
107 if(!context || hashid == 0)
108 return;
109
110 if(!bufsize) {
111 gnutls_hash_deinit((MD_CTX)context, buffer);
112 context = NULL;
113 gnutls_hash_init((MD_CTX *)&context, (MD_ID)hashid);
114 }
115 else
117
118 size = gnutls_hash_get_len((MD_ID)hashid);
119
120 if(!size || !context || !hashid)
121 return;
122
123 if(bin)
124 gnutls_hash((MD_CTX)context, buffer, size);
125 else {
126 unsigned count = 0;
127
128 while(count < size) {
129 snprintf(textbuf + (count * 2), 3, "%2.2x",
130buffer[count]);
131 ++count;
132
133 }
134 gnutls_hash((MD_CTX)context, textbuf, size * 2);
135 }
136 bufsize = 0;
137}
138
139const uint8_t *Digest::get(void)
140{
141 unsigned count = 0;
142 unsigned size = 0;
143
144 if(bufsize)
145 return buffer;
146
147 if(!context || hashid == 0)
148 return NULL;
149
150 gnutls_hash_deinit((MD_CTX)context, buffer);
151 size = gnutls_hash_get_len((MD_ID)hashid);
152 context = NULL;
153 bufsize = size;
154
155 while(count < bufsize) {
156 snprintf(textbuf + (count * 2), 3, "%2.2x",
157buffer[count]);
158 ++count;
159 }
160 return buffer;
161}
162
163} // namespace ucommon
uint8_t buffer[512/8]
Definition: secure.h:520
unsigned bufsize
Definition: secure.h:519
unsigned size() const
Definition: secure.h:567
bool put(const void *memory, size_t size)
Definition: digest.cpp:79
void reset(void)
Reset and restart digest object.
Definition: digest.cpp:88
char textbuf[512/8+1]
Definition: secure.h:521
void recycle(bool binary=false)
Finalize and recycle current digest to start a new digest.
Definition: digest.cpp:103
static bool has(const char *name)
Test to see if a specific digest type is supported.
Definition: digest.cpp:69
void * context
Definition: secure.h:512
void set(const char *id)
Definition: digest.cpp:53
const uint8_t * get(void)
Definition: digest.cpp:139
void release(void)
Definition: digest.cpp:23
static int map_digest(const char *type)
Definition: digest.cpp:35
static bool init(void)
Initialize secure stack for first use, and report if SSL support is compiled in.
Definition: secure.cpp:37
Common namespace for all ucommon objects.
Definition: access.cpp:23
gnutls_digest_algorithm_t MD_ID
Definition: local.h:54
gnutls_hash_hd_t MD_CTX
Definition: local.h:55
bool eq_case(char const *s1, char const *s2)
Compare two null terminated strings if equal ignoring case.
Definition: string.h:1699
#define MAX_DIGEST_HASHSIZE
Definition: secure.h:54
static bool temp
Definition: zerofill.cpp:29