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