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  * Conversion parameters:
26  * inFile  = glib-Data-HMACs.html
27  * outPack = glib
28  * outFile = Hmac
29  * strct   = GHmac
30  * realStrct=
31  * ctorStrct=
32  * clss    = Hmac
33  * interf  = 
34  * class Code: Yes
35  * interface Code: No
36  * template for:
37  * extend  = 
38  * implements:
39  * prefixes:
40  * 	- g_hmac_
41  * 	- g_
42  * omit structs:
43  * omit prefixes:
44  * omit code:
45  * 	- g_hmac_get_digest
46  * omit signals:
47  * imports:
48  * 	- glib.Str
49  * 	- gtkc.Loader
50  * 	- gtkc.paths
51  * structWrap:
52  * 	- GHmac* -> Hmac
53  * module aliases:
54  * local aliases:
55  * overrides:
56  */
57 
58 module glib.Hmac;
59 
60 public  import gtkc.glibtypes;
61 
62 private import gtkc.glib;
63 private import glib.ConstructionException;
64 
65 
66 private import glib.Str;
67 private import gtkc.Loader;
68 private import gtkc.paths;
69 
70 
71 
72 
73 /**
74  * HMACs should be used when producing a cookie or hash based on data
75  * and a key. Simple mechanisms for using SHA1 and other algorithms to
76  * digest a key and data together are vulnerable to various security
77  * issues. HMAC
78  * uses algorithms like SHA1 in a secure way to produce a digest of a
79  * key and data.
80  *
81  * Both the key and data are arbitrary byte arrays of bytes or characters.
82  *
83  * Support for HMAC Digests has been added in GLib 2.30.
84  */
85 public class Hmac
86 {
87 	
88 	/** the main Gtk struct */
89 	protected GHmac* gHmac;
90 	
91 	
92 	public GHmac* getHmacStruct()
93 	{
94 		return gHmac;
95 	}
96 	
97 	
98 	/** the main Gtk struct as a void* */
99 	protected void* getStruct()
100 	{
101 		return cast(void*)gHmac;
102 	}
103 	
104 	/**
105 	 * Sets our main struct and passes it to the parent class
106 	 */
107 	public this (GHmac* gHmac)
108 	{
109 		this.gHmac = gHmac;
110 	}
111 	
112 	~this ()
113 	{
114 		if ( Linker.isLoaded(LIBRARY.GLIB) && gHmac !is null )
115 		{
116 			g_hmac_unref(gHmac);
117 		}
118 	}
119 	
120 	/**
121 	 * Gets the digest from checksum as a raw binary array and places it
122 	 * into buffer. The size of the digest depends on the type of checksum.
123 	 * Once this function has been called, the GHmac is closed and can
124 	 * no longer be updated with g_checksum_update().
125 	 * Since 2.30
126 	 * Params:
127 	 * buffer = output buffer
128 	 */
129 	public void getDigest(ref ubyte[] buffer)
130 	{
131 		// void g_hmac_get_digest (GHmac *hmac,  guint8 *buffer,  gsize *digest_len);
132 		size_t digestLen = buffer.length;
133 		
134 		g_hmac_get_digest(gHmac, buffer.ptr, &digestLen);
135 		
136 		buffer = buffer[0 .. digestLen];
137 	}
138 	
139 	/**
140 	 */
141 	
142 	/**
143 	 * Creates a new GHmac, using the digest algorithm digest_type.
144 	 * If the digest_type is not known, NULL is returned.
145 	 * A GHmac can be used to compute the HMAC of a key and an
146 	 * arbitrary binary blob, using different hashing algorithms.
147 	 * A GHmac works by feeding a binary blob through g_hmac_update()
148 	 * until the data is complete; the digest can then be extracted
149 	 * using g_hmac_get_string(), which will return the checksum as a
150 	 * hexadecimal string; or g_hmac_get_digest(), which will return a
151 	 * array of raw bytes. Once either g_hmac_get_string() or
152 	 * g_hmac_get_digest() have been called on a GHmac, the HMAC
153 	 * will be closed and it won't be possible to call g_hmac_update()
154 	 * on it anymore.
155 	 * Since 2.30
156 	 * Params:
157 	 * digestType = the desired type of digest
158 	 * key = the key for the HMAC. [array length=key_len]
159 	 * Throws: ConstructionException GTK+ fails to create the object.
160 	 */
161 	public this (GChecksumType digestType, char[] key)
162 	{
163 		// GHmac * g_hmac_new (GChecksumType digest_type,  const guchar *key,  gsize key_len);
164 		auto p = g_hmac_new(digestType, key.ptr, cast(int) key.length);
165 		if(p is null)
166 		{
167 			throw new ConstructionException("null returned by g_hmac_new(digestType, key.ptr, cast(int) key.length)");
168 		}
169 		this(cast(GHmac*) p);
170 	}
171 	
172 	/**
173 	 * Copies a GHmac. If hmac has been closed, by calling
174 	 * g_hmac_get_string() or g_hmac_get_digest(), the copied
175 	 * HMAC will be closed as well.
176 	 * Since 2.30
177 	 * Returns: the copy of the passed GHmac. Use g_hmac_unref() when finished using it.
178 	 */
179 	public Hmac copy()
180 	{
181 		// GHmac * g_hmac_copy (const GHmac *hmac);
182 		auto p = g_hmac_copy(gHmac);
183 		
184 		if(p is null)
185 		{
186 			return null;
187 		}
188 		
189 		return new Hmac(cast(GHmac*) p);
190 	}
191 	
192 	/**
193 	 * Atomically increments the reference count of hmac by one.
194 	 * This function is MT-safe and may be called from any thread.
195 	 * Since 2.30
196 	 * Returns: the passed in GHmac.
197 	 */
198 	public Hmac doref()
199 	{
200 		// GHmac * g_hmac_ref (GHmac *hmac);
201 		auto p = g_hmac_ref(gHmac);
202 		
203 		if(p is null)
204 		{
205 			return null;
206 		}
207 		
208 		return new Hmac(cast(GHmac*) p);
209 	}
210 	
211 	/**
212 	 * Atomically decrements the reference count of hmac by one.
213 	 * If the reference count drops to 0, all keys and values will be
214 	 * destroyed, and all memory allocated by the hash table is released.
215 	 * This function is MT-safe and may be called from any thread.
216 	 * Frees the memory allocated for hmac.
217 	 * Since 2.30
218 	 */
219 	public void unref()
220 	{
221 		// void g_hmac_unref (GHmac *hmac);
222 		g_hmac_unref(gHmac);
223 	}
224 	
225 	/**
226 	 * Feeds data into an existing GHmac.
227 	 * The HMAC must still be open, that is g_hmac_get_string() or
228 	 * g_hmac_get_digest() must not have been called on hmac.
229 	 * Since 2.30
230 	 * Params:
231 	 * data = buffer used to compute the checksum. [array length=length]
232 	 */
233 	public void update(char[] data)
234 	{
235 		// void g_hmac_update (GHmac *hmac,  const guchar *data,  gssize length);
236 		g_hmac_update(gHmac, data.ptr, cast(int) data.length);
237 	}
238 	
239 	/**
240 	 * Gets the HMAC as an hexadecimal string.
241 	 * Once this function has been called the GHmac can no longer be
242 	 * updated with g_hmac_update().
243 	 * The hexadecimal characters will be lower case.
244 	 * Since 2.30
245 	 * Returns: the hexadecimal representation of the HMAC. The returned string is owned by the HMAC and should not be modified or freed.
246 	 */
247 	public string getString()
248 	{
249 		// const gchar * g_hmac_get_string (GHmac *hmac);
250 		return Str.toString(g_hmac_get_string(gHmac));
251 	}
252 	
253 	/**
254 	 * Computes the HMAC for a binary data of length. This is a
255 	 * convenience wrapper for g_hmac_new(), g_hmac_get_string()
256 	 * and g_hmac_unref().
257 	 * The hexadecimal string returned will be in lower case.
258 	 * Since 2.30
259 	 * Params:
260 	 * digestType = a GChecksumType to use for the HMAC
261 	 * key = the key to use in the HMAC. [array length=key_len]
262 	 * data = binary blob to compute the HMAC of
263 	 * Returns: the HMAC of the binary data as a string in hexadecimal. The returned string should be freed with g_free() when done using it.
264 	 */
265 	public static string computeHmacForData(GChecksumType digestType, char[] key, char[] data)
266 	{
267 		// gchar * g_compute_hmac_for_data (GChecksumType digest_type,  const guchar *key,  gsize key_len,  const guchar *data,  gsize length);
268 		return Str.toString(g_compute_hmac_for_data(digestType, key.ptr, cast(int) key.length, data.ptr, cast(int) data.length));
269 	}
270 	
271 	/**
272 	 * Computes the HMAC for a string.
273 	 * The hexadecimal string returned will be in lower case.
274 	 * Since 2.30
275 	 * Params:
276 	 * digestType = a GChecksumType to use for the HMAC
277 	 * key = the key to use in the HMAC. [array length=key_len]
278 	 * str = the string to compute the HMAC for
279 	 * Returns: the HMAC as a hexadecimal string. The returned string should be freed with g_free() when done using it.
280 	 */
281 	public static string computeHmacForString(GChecksumType digestType, char[] key, string str)
282 	{
283 		// gchar * g_compute_hmac_for_string (GChecksumType digest_type,  const guchar *key,  gsize key_len,  const gchar *str,  gssize length);
284 		return Str.toString(g_compute_hmac_for_string(digestType, key.ptr, cast(int) key.length, cast(char*)str.ptr, cast(int) str.length));
285 	}
286 }