41 while ((opt = getopt(argc, argv,
"hvV:")) != EOF) {
44 if (!strncmp(
"LOG_NULL", optarg, 8))
46 else if (!strncmp(
"LOG_NOTICE", optarg, 10))
48 else if (!strncmp(
"LOG_INFO", optarg, 8))
50 else if (!strncmp(
"LOG_ERR", optarg, 7))
52 else if (!strncmp(
"LOG_DEBUG", optarg, 9))
55 fprintf(stderr,
"Unknown log level %s\n", optarg);
60 fprintf(stderr,
"ex_random\n");
64 fprintf(stderr,
"usage: %s [-V LOG_LEVEL]\n", argv[0]);
73 memset(a, 0,
sizeof(a));
74 memset(b, 0,
sizeof(b));
82 if (memcmp(a, b,
sizeof(a)) == 0) {
88 for (
size_t i = 0; i <
sizeof(a); i++) {
101 n_log(
LOG_ERR,
"n_random_bytes(NULL,0) should succeed");
121 for (
size_t i = 0; i < t1->
written; i++) {
122 if (!isxdigit((
unsigned char)t1->
data[i]) || (t1->
data[i] >=
'A' && t1->
data[i] <=
'F')) {
123 n_log(
LOG_ERR,
"token has a non-lowercase-hex character at %zu", i);
128 if (strcmp(t1->
data, t2->
data) == 0) {
145int main(
int argc,
char** argv) {
void process_args(int argc, char **argv)
static void test_bytes(void)
static void test_token(void)
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
#define LOG_DEBUG
debug-level messages
#define LOG_ERR
error conditions
void set_log_level(const int log_level)
Set the global log level value ( static int LOG_LEVEL )
#define LOG_NOTICE
normal but significant condition
#define LOG_NULL
no log output
#define LOG_INFO
informational
int n_random_bytes(unsigned char *buf, size_t n)
fill buf with n cryptographically secure random bytes (getrandom(2) when available,...
N_STR * n_random_hex_token(size_t nbytes)
generate nbytes secure random bytes and return them as a lowercase hex N_STR (2*nbytes characters); f...
size_t written
number of meaningful bytes in data, excluding the null terminator; the size including the null termin...
#define free_nstr(__ptr)
free a N_STR structure and set the pointer to NULL
A box including a string and his lenght.
Common headers and low-level functions & define.
Cryptographically secure random bytes and hex tokens (POSIX)
N_STR and string function declaration.