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