1 /*
2  * This file is part of gtkD.
3  *
4  * gtkD is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License
6  * as published by the Free Software Foundation; either version 3
7  * of the License, or (at your option) any later version, with
8  * some exceptions, please read the COPYING file.
9  *
10  * gtkD 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 gtkD; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
18  */
19 
20 // generated automatically - do not change
21 // find conversion definition on APILookup.txt
22 // implement new conversion functionalities on the wrap.utils pakage
23 
24 
25 module glib.Hmac;
26 
27 private import glib.Bytes;
28 private import glib.ConstructionException;
29 private import glib.Str;
30 private import glib.c.functions;
31 public  import glib.c.types;
32 public  import gtkc.glibtypes;
33 private import gtkd.Loader;
34 
35 
36 /**
37  * An opaque structure representing a HMAC operation.
38  * To create a new GHmac, use g_hmac_new(). To free
39  * a GHmac, use g_hmac_unref().
40  *
41  * Since: 2.30
42  */
43 public class Hmac
44 {
45 	/** the main Gtk struct */
46 	protected GHmac* gHmac;
47 	protected bool ownedRef;
48 
49 	/** Get the main Gtk struct */
50 	public GHmac* getHmacStruct(bool transferOwnership = false)
51 	{
52 		if (transferOwnership)
53 			ownedRef = false;
54 		return gHmac;
55 	}
56 
57 	/** the main Gtk struct as a void* */
58 	protected void* getStruct()
59 	{
60 		return cast(void*)gHmac;
61 	}
62 
63 	/**
64 	 * Sets our main struct and passes it to the parent class.
65 	 */
66 	public this (GHmac* gHmac, bool ownedRef = false)
67 	{
68 		this.gHmac = gHmac;
69 		this.ownedRef = ownedRef;
70 	}
71 
72 	~this ()
73 	{
74 		if ( Linker.isLoaded(LIBRARY_GLIB) && ownedRef )
75 			g_hmac_unref(gHmac);
76 	}
77 
78 	/**
79 	 * Gets the digest from checksum as a raw binary array and places it
80 	 * into buffer. The size of the digest depends on the type of checksum.
81 	 *
82 	 * Once this function has been called, the Hmac is closed and can
83 	 * no longer be updated with update().
84 	 *
85 	 * Params:
86 	 *     buffer = output buffer
87 	 *
88 	 * Since: 2.30
89 	 */
90 	public void getDigest(ref ubyte[] buffer)
91 	{
92 		size_t digestLen = buffer.length;
93 
94 		g_hmac_get_digest(gHmac, buffer.ptr, &digestLen);
95 
96 		buffer = buffer[0 .. digestLen];
97 	}
98 
99 	/**
100 	 */
101 
102 	/**
103 	 * Copies a #GHmac. If @hmac has been closed, by calling
104 	 * g_hmac_get_string() or g_hmac_get_digest(), the copied
105 	 * HMAC will be closed as well.
106 	 *
107 	 * Returns: the copy of the passed #GHmac. Use g_hmac_unref()
108 	 *     when finished using it.
109 	 *
110 	 * Since: 2.30
111 	 */
112 	public Hmac copy()
113 	{
114 		auto p = g_hmac_copy(gHmac);
115 
116 		if(p is null)
117 		{
118 			return null;
119 		}
120 
121 		return new Hmac(cast(GHmac*) p);
122 	}
123 
124 	/**
125 	 * Gets the HMAC as an hexadecimal string.
126 	 *
127 	 * Once this function has been called the #GHmac can no longer be
128 	 * updated with g_hmac_update().
129 	 *
130 	 * The hexadecimal characters will be lower case.
131 	 *
132 	 * Returns: the hexadecimal representation of the HMAC. The
133 	 *     returned string is owned by the HMAC and should not be modified
134 	 *     or freed.
135 	 *
136 	 * Since: 2.30
137 	 */
138 	public string getString()
139 	{
140 		return Str.toString(g_hmac_get_string(gHmac));
141 	}
142 
143 	/**
144 	 * Atomically increments the reference count of @hmac by one.
145 	 *
146 	 * This function is MT-safe and may be called from any thread.
147 	 *
148 	 * Returns: the passed in #GHmac.
149 	 *
150 	 * Since: 2.30
151 	 */
152 	public Hmac doref()
153 	{
154 		auto p = g_hmac_ref(gHmac);
155 
156 		if(p is null)
157 		{
158 			return null;
159 		}
160 
161 		return new Hmac(cast(GHmac*) p);
162 	}
163 
164 	/**
165 	 * Atomically decrements the reference count of @hmac by one.
166 	 *
167 	 * If the reference count drops to 0, all keys and values will be
168 	 * destroyed, and all memory allocated by the hash table is released.
169 	 * This function is MT-safe and may be called from any thread.
170 	 * Frees the memory allocated for @hmac.
171 	 *
172 	 * Since: 2.30
173 	 */
174 	public void unref()
175 	{
176 		g_hmac_unref(gHmac);
177 	}
178 
179 	/**
180 	 * Feeds @data into an existing #GHmac.
181 	 *
182 	 * The HMAC must still be open, that is g_hmac_get_string() or
183 	 * g_hmac_get_digest() must not have been called on @hmac.
184 	 *
185 	 * Params:
186 	 *     data = buffer used to compute the checksum
187 	 *
188 	 * Since: 2.30
189 	 */
190 	public void update(char[] data)
191 	{
192 		g_hmac_update(gHmac, data.ptr, cast(ptrdiff_t)data.length);
193 	}
194 
195 	/**
196 	 * Creates a new #GHmac, using the digest algorithm @digest_type.
197 	 * If the @digest_type is not known, %NULL is returned.
198 	 * A #GHmac can be used to compute the HMAC of a key and an
199 	 * arbitrary binary blob, using different hashing algorithms.
200 	 *
201 	 * A #GHmac works by feeding a binary blob through g_hmac_update()
202 	 * until the data is complete; the digest can then be extracted
203 	 * using g_hmac_get_string(), which will return the checksum as a
204 	 * hexadecimal string; or g_hmac_get_digest(), which will return a
205 	 * array of raw bytes. Once either g_hmac_get_string() or
206 	 * g_hmac_get_digest() have been called on a #GHmac, the HMAC
207 	 * will be closed and it won't be possible to call g_hmac_update()
208 	 * on it anymore.
209 	 *
210 	 * Support for digests of type %G_CHECKSUM_SHA512 has been added in GLib 2.42.
211 	 * Support for %G_CHECKSUM_SHA384 was added in GLib 2.52.
212 	 *
213 	 * Params:
214 	 *     digestType = the desired type of digest
215 	 *     key = the key for the HMAC
216 	 *
217 	 * Returns: the newly created #GHmac, or %NULL.
218 	 *     Use g_hmac_unref() to free the memory allocated by it.
219 	 *
220 	 * Since: 2.30
221 	 *
222 	 * Throws: ConstructionException GTK+ fails to create the object.
223 	 */
224 	public this(GChecksumType digestType, char[] key)
225 	{
226 		auto p = g_hmac_new(digestType, key.ptr, cast(size_t)key.length);
227 
228 		if(p is null)
229 		{
230 			throw new ConstructionException("null returned by new");
231 		}
232 
233 		this(cast(GHmac*) p);
234 	}
235 
236 	/**
237 	 * Computes the HMAC for a binary @data of @length. This is a
238 	 * convenience wrapper for g_hmac_new(), g_hmac_get_string()
239 	 * and g_hmac_unref().
240 	 *
241 	 * The hexadecimal string returned will be in lower case.
242 	 *
243 	 * Params:
244 	 *     digestType = a #GChecksumType to use for the HMAC
245 	 *     key = the key to use in the HMAC
246 	 *     data = binary blob to compute the HMAC of
247 	 *
248 	 * Returns: the HMAC of the binary data as a string in hexadecimal.
249 	 *     The returned string should be freed with g_free() when done using it.
250 	 *
251 	 * Since: 2.30
252 	 */
253 	public static string computeHmacForData(GChecksumType digestType, char[] key, char[] data)
254 	{
255 		auto retStr = g_compute_hmac_for_data(digestType, key.ptr, cast(size_t)key.length, data.ptr, cast(size_t)data.length);
256 
257 		scope(exit) Str.freeString(retStr);
258 		return Str.toString(retStr);
259 	}
260 
261 	/**
262 	 * Computes the HMAC for a string.
263 	 *
264 	 * The hexadecimal string returned will be in lower case.
265 	 *
266 	 * Params:
267 	 *     digestType = a #GChecksumType to use for the HMAC
268 	 *     key = the key to use in the HMAC
269 	 *     str = the string to compute the HMAC for
270 	 *
271 	 * Returns: the HMAC as a hexadecimal string.
272 	 *     The returned string should be freed with g_free()
273 	 *     when done using it.
274 	 *
275 	 * Since: 2.30
276 	 */
277 	public static string computeHmacForString(GChecksumType digestType, char[] key, string str)
278 	{
279 		auto retStr = g_compute_hmac_for_string(digestType, key.ptr, cast(size_t)key.length, Str.toStringz(str), cast(ptrdiff_t)str.length);
280 
281 		scope(exit) Str.freeString(retStr);
282 		return Str.toString(retStr);
283 	}
284 
285 	/**
286 	 * Computes the HMAC for a binary @data. This is a
287 	 * convenience wrapper for g_hmac_new(), g_hmac_get_string()
288 	 * and g_hmac_unref().
289 	 *
290 	 * The hexadecimal string returned will be in lower case.
291 	 *
292 	 * Params:
293 	 *     digestType = a #GChecksumType to use for the HMAC
294 	 *     key = the key to use in the HMAC
295 	 *     data = binary blob to compute the HMAC of
296 	 *
297 	 * Returns: the HMAC of the binary data as a string in hexadecimal.
298 	 *     The returned string should be freed with g_free() when done using it.
299 	 *
300 	 * Since: 2.50
301 	 */
302 	public static string computeHmacForBytes(GChecksumType digestType, Bytes key, Bytes data)
303 	{
304 		auto retStr = g_compute_hmac_for_bytes(digestType, (key is null) ? null : key.getBytesStruct(), (data is null) ? null : data.getBytesStruct());
305 
306 		scope(exit) Str.freeString(retStr);
307 		return Str.toString(retStr);
308 	}
309 }