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 private import gtkd.Loader;
33 
34 
35 /**
36  * An opaque structure representing a HMAC operation.
37  * To create a new GHmac, use g_hmac_new(). To free
38  * a GHmac, use g_hmac_unref().
39  *
40  * Since: 2.30
41  */
42 public class Hmac
43 {
44 	/** the main Gtk struct */
45 	protected GHmac* gHmac;
46 	protected bool ownedRef;
47 
48 	/** Get the main Gtk struct */
49 	public GHmac* getHmacStruct(bool transferOwnership = false)
50 	{
51 		if (transferOwnership)
52 			ownedRef = false;
53 		return gHmac;
54 	}
55 
56 	/** the main Gtk struct as a void* */
57 	protected void* getStruct()
58 	{
59 		return cast(void*)gHmac;
60 	}
61 
62 	/**
63 	 * Sets our main struct and passes it to the parent class.
64 	 */
65 	public this (GHmac* gHmac, bool ownedRef = false)
66 	{
67 		this.gHmac = gHmac;
68 		this.ownedRef = ownedRef;
69 	}
70 
71 	~this ()
72 	{
73 		if ( Linker.isLoaded(LIBRARY_GLIB) && ownedRef )
74 			g_hmac_unref(gHmac);
75 	}
76 
77 	/**
78 	 * Gets the digest from checksum as a raw binary array and places it
79 	 * into buffer. The size of the digest depends on the type of checksum.
80 	 *
81 	 * Once this function has been called, the Hmac is closed and can
82 	 * no longer be updated with update().
83 	 *
84 	 * Params:
85 	 *     buffer = output buffer
86 	 *
87 	 * Since: 2.30
88 	 */
89 	public void getDigest(ref ubyte[] buffer)
90 	{
91 		size_t digestLen = buffer.length;
92 
93 		g_hmac_get_digest(gHmac, buffer.ptr, &digestLen);
94 
95 		buffer = buffer[0 .. digestLen];
96 	}
97 
98 	/**
99 	 */
100 
101 	/**
102 	 * Copies a #GHmac. If @hmac has been closed, by calling
103 	 * g_hmac_get_string() or g_hmac_get_digest(), the copied
104 	 * HMAC will be closed as well.
105 	 *
106 	 * Returns: the copy of the passed #GHmac. Use g_hmac_unref()
107 	 *     when finished using it.
108 	 *
109 	 * Since: 2.30
110 	 */
111 	public Hmac copy()
112 	{
113 		auto __p = g_hmac_copy(gHmac);
114 
115 		if(__p is null)
116 		{
117 			return null;
118 		}
119 
120 		return new Hmac(cast(GHmac*) __p);
121 	}
122 
123 	/**
124 	 * Gets the HMAC as a hexadecimal string.
125 	 *
126 	 * Once this function has been called the #GHmac can no longer be
127 	 * updated with g_hmac_update().
128 	 *
129 	 * The hexadecimal characters will be lower case.
130 	 *
131 	 * Returns: the hexadecimal representation of the HMAC. The
132 	 *     returned string is owned by the HMAC and should not be modified
133 	 *     or freed.
134 	 *
135 	 * Since: 2.30
136 	 */
137 	public string getString()
138 	{
139 		return Str.toString(g_hmac_get_string(gHmac));
140 	}
141 
142 	alias doref = ref_;
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 ref_()
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 }