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 a 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 	alias doref = ref_;
144 	/**
145 	 * Atomically increments the reference count of @hmac by one.
146 	 *
147 	 * This function is MT-safe and may be called from any thread.
148 	 *
149 	 * Returns: the passed in #GHmac.
150 	 *
151 	 * Since: 2.30
152 	 */
153 	public Hmac ref_()
154 	{
155 		auto __p = g_hmac_ref(gHmac);
156 
157 		if(__p is null)
158 		{
159 			return null;
160 		}
161 
162 		return new Hmac(cast(GHmac*) __p);
163 	}
164 
165 	/**
166 	 * Atomically decrements the reference count of @hmac by one.
167 	 *
168 	 * If the reference count drops to 0, all keys and values will be
169 	 * destroyed, and all memory allocated by the hash table is released.
170 	 * This function is MT-safe and may be called from any thread.
171 	 * Frees the memory allocated for @hmac.
172 	 *
173 	 * Since: 2.30
174 	 */
175 	public void unref()
176 	{
177 		g_hmac_unref(gHmac);
178 	}
179 
180 	/**
181 	 * Feeds @data into an existing #GHmac.
182 	 *
183 	 * The HMAC must still be open, that is g_hmac_get_string() or
184 	 * g_hmac_get_digest() must not have been called on @hmac.
185 	 *
186 	 * Params:
187 	 *     data = buffer used to compute the checksum
188 	 *
189 	 * Since: 2.30
190 	 */
191 	public void update(char[] data)
192 	{
193 		g_hmac_update(gHmac, data.ptr, cast(ptrdiff_t)data.length);
194 	}
195 
196 	/**
197 	 * Creates a new #GHmac, using the digest algorithm @digest_type.
198 	 * If the @digest_type is not known, %NULL is returned.
199 	 * A #GHmac can be used to compute the HMAC of a key and an
200 	 * arbitrary binary blob, using different hashing algorithms.
201 	 *
202 	 * A #GHmac works by feeding a binary blob through g_hmac_update()
203 	 * until the data is complete; the digest can then be extracted
204 	 * using g_hmac_get_string(), which will return the checksum as a
205 	 * hexadecimal string; or g_hmac_get_digest(), which will return a
206 	 * array of raw bytes. Once either g_hmac_get_string() or
207 	 * g_hmac_get_digest() have been called on a #GHmac, the HMAC
208 	 * will be closed and it won't be possible to call g_hmac_update()
209 	 * on it anymore.
210 	 *
211 	 * Support for digests of type %G_CHECKSUM_SHA512 has been added in GLib 2.42.
212 	 * Support for %G_CHECKSUM_SHA384 was added in GLib 2.52.
213 	 *
214 	 * Params:
215 	 *     digestType = the desired type of digest
216 	 *     key = the key for the HMAC
217 	 *
218 	 * Returns: the newly created #GHmac, or %NULL.
219 	 *     Use g_hmac_unref() to free the memory allocated by it.
220 	 *
221 	 * Since: 2.30
222 	 *
223 	 * Throws: ConstructionException GTK+ fails to create the object.
224 	 */
225 	public this(GChecksumType digestType, char[] key)
226 	{
227 		auto __p = g_hmac_new(digestType, key.ptr, cast(size_t)key.length);
228 
229 		if(__p is null)
230 		{
231 			throw new ConstructionException("null returned by new");
232 		}
233 
234 		this(cast(GHmac*) __p);
235 	}
236 
237 	/**
238 	 * Computes the HMAC for a binary @data of @length. This is a
239 	 * convenience wrapper for g_hmac_new(), g_hmac_get_string()
240 	 * and g_hmac_unref().
241 	 *
242 	 * The hexadecimal string returned will be in lower case.
243 	 *
244 	 * Params:
245 	 *     digestType = a #GChecksumType to use for the HMAC
246 	 *     key = the key to use in the HMAC
247 	 *     data = binary blob to compute the HMAC of
248 	 *
249 	 * Returns: the HMAC of the binary data as a string in hexadecimal.
250 	 *     The returned string should be freed with g_free() when done using it.
251 	 *
252 	 * Since: 2.30
253 	 */
254 	public static string computeHmacForData(GChecksumType digestType, char[] key, char[] data)
255 	{
256 		auto retStr = g_compute_hmac_for_data(digestType, key.ptr, cast(size_t)key.length, data.ptr, cast(size_t)data.length);
257 
258 		scope(exit) Str.freeString(retStr);
259 		return Str.toString(retStr);
260 	}
261 
262 	/**
263 	 * Computes the HMAC for a string.
264 	 *
265 	 * The hexadecimal string returned will be in lower case.
266 	 *
267 	 * Params:
268 	 *     digestType = a #GChecksumType to use for the HMAC
269 	 *     key = the key to use in the HMAC
270 	 *     str = the string to compute the HMAC for
271 	 *
272 	 * Returns: the HMAC as a hexadecimal string.
273 	 *     The returned string should be freed with g_free()
274 	 *     when done using it.
275 	 *
276 	 * Since: 2.30
277 	 */
278 	public static string computeHmacForString(GChecksumType digestType, char[] key, string str)
279 	{
280 		auto retStr = g_compute_hmac_for_string(digestType, key.ptr, cast(size_t)key.length, Str.toStringz(str), cast(ptrdiff_t)str.length);
281 
282 		scope(exit) Str.freeString(retStr);
283 		return Str.toString(retStr);
284 	}
285 
286 	/**
287 	 * Computes the HMAC for a binary @data. This is a
288 	 * convenience wrapper for g_hmac_new(), g_hmac_get_string()
289 	 * and g_hmac_unref().
290 	 *
291 	 * The hexadecimal string returned will be in lower case.
292 	 *
293 	 * Params:
294 	 *     digestType = a #GChecksumType to use for the HMAC
295 	 *     key = the key to use in the HMAC
296 	 *     data = binary blob to compute the HMAC of
297 	 *
298 	 * Returns: the HMAC of the binary data as a string in hexadecimal.
299 	 *     The returned string should be freed with g_free() when done using it.
300 	 *
301 	 * Since: 2.50
302 	 */
303 	public static string computeHmacForBytes(GChecksumType digestType, Bytes key, Bytes data)
304 	{
305 		auto retStr = g_compute_hmac_for_bytes(digestType, (key is null) ? null : key.getBytesStruct(), (data is null) ? null : data.getBytesStruct());
306 
307 		scope(exit) Str.freeString(retStr);
308 		return Str.toString(retStr);
309 	}
310 }