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