From 60a0a98971920c433ad832b74afd8948d3faad63 Mon Sep 17 00:00:00 2001 From: "C. McEnroe" Date: Sat, 25 Jan 2020 07:06:21 -0500 Subject: Add UUIDv4 generation --- .gitignore | 1 + notemap.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 .gitignore create mode 100644 notemap.c diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..62e16ab --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +notemap diff --git a/notemap.c b/notemap.c new file mode 100644 index 0000000..7d06205 --- /dev/null +++ b/notemap.c @@ -0,0 +1,46 @@ +/* Copyright (C) 2020 C. McEnroe + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include +#include +#include +#include + +typedef unsigned char byte; + +static const char *uuidGen(void) { + byte uuid[16]; + arc4random_buf(uuid, sizeof(uuid)); + uuid[6] &= 0x0F; + uuid[6] |= 0x40; + uuid[8] &= 0x3F; + uuid[8] |= 0x80; + + static char str[sizeof("00000000-0000-0000-0000-000000000000")]; + snprintf( + str, sizeof(str), + "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x", + uuid[0], uuid[1], uuid[2], uuid[3], + uuid[4], uuid[5], uuid[6], uuid[7], + uuid[8], uuid[9], uuid[10], uuid[11], + uuid[12], uuid[13], uuid[14], uuid[15] + ); + return str; +} + +int main(void) { + printf("%s\n", uuidGen()); +} -- cgit 1.4.1