Generating password using LibCapy

As a simple example of use of the CapyRandom I'll show in this article how to write a command line app to generate passwords and random data files.

(The complete code is available at the end of the article)

First, lets define a structure to hold all the needed data.

We'll need also an array of valid characters for a password, which I divide in two arrays to manage passwords with/without special characters.

Generating random data is as simple as creating the CapyRandom instance:

and getting the data in the required format, here a single byte:

here an integer in a range of the appropriate size for the passwords:

To select between password and data file, we'll use the command line parser provided by LibCapy. The argument definition looks as follow:

and the actual parsing is done with:

Of course we'll want to check the arguments passed by the user, and to process any problem cleanly, we'll use some purpose made exceptions:

The ExcToStr conversion function is set with:

and the arguments checking looks like this:

After the arguments have been checked, we can use them as follow:

I skip intentionally releasing memory. This is done automatically at the end of the program, which is so small and simple that there is nothing to fear here. Everything put together, it becomes:

Compile with the following Makefile:

Example of use:

2021-12-16
in All, C programming, LibCapy examples,
80 views
Copyright 2021-2024 Baillehache Pascal