source: appstream-generator/build/girepo/glib/Hmac.d @ 4841

Last change on this file since 4841 was 4841, checked in by Juanma, 2 years ago

Initial release

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