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