diff options
author | Günther Wagner <info@gunibert.de> | 2022-01-12 18:26:38 +0000 |
---|---|---|
committer | Günther Wagner <info@gunibert.de> | 2022-01-12 18:26:38 +0000 |
commit | 6d65d8862ad7d39e55be0adb868c05f194250e74 (patch) | |
tree | 46ffe23af55fb90e3199b184b9c820e0f8af0c9a /rest/rest-utils.c | |
parent | 7766d718735270d1c0850685a3c7655a5726d60f (diff) | |
download | librest-6d65d8862ad7d39e55be0adb868c05f194250e74.tar.gz |
OAuth2 Pkce Workflow
Diffstat (limited to 'rest/rest-utils.c')
-rw-r--r-- | rest/rest-utils.c | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/rest/rest-utils.c b/rest/rest-utils.c new file mode 100644 index 0000000..939d49e --- /dev/null +++ b/rest/rest-utils.c @@ -0,0 +1,44 @@ +/* rest-utils.c + * + * Copyright 2021 Günther Wagner <info@gunibert.de> + * + * This program is free software; you can redistribute it and/or modify it + * under the terms and conditions of the GNU Lesser General Public License, + * version 2.1, as published by the Free Software Foundation. + * + * This program is distributed in the hope it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for + * more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "rest-utils.h" + +/** + * random_string: + * @length: the length of the random string + * + * Creates a random string from a given alphabeth with length @length + * + * Returns: (transfer full): a random string + */ +gchar * +random_string (guint length) +{ + g_autoptr(GRand) rand = g_rand_new (); + gchar *buffer = g_slice_alloc0 (sizeof (gchar) * length + 1); + gchar alphabeth[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._~"; + + for (guint i = 0; i < length; i++) + { + buffer[i] = alphabeth[g_rand_int (rand) % (sizeof (alphabeth) - 1)]; + } + buffer[length] = '\0'; + + return buffer; +} + |