How to resolve the algorithm Password generator step by step in the Prolog programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Password generator step by step in the Prolog programming language

Table of Contents

Problem Statement

Create a password generation program which will generate passwords containing random ASCII characters from the following groups:

The generated password(s) must include   at least one   (of each of the four groups):

The user must be able to specify the password length and the number of passwords to generate. The passwords should be displayed or written to a file, one per line. The randomness should be from a system source or library. The program should implement a help option or button which should describe the program and options when invoked. You may also allow the user to specify a seed value, and give the option of excluding visually similar characters. For example:           Il1     O0     5S     2Z           where the characters are:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Password generator step by step in the Prolog programming language

Source code in the prolog programming language

:- set_prolog_flag(double_quotes, chars).
:- initialization(main, main).

main( Argv ) :-
    opt_spec( Spec ),
    opt_parse( Spec, Argv, Opts, _ ),    
    (
        member( help(true), Opts ) -> show_help
        ; 
        member( length( Len ), Opts ),
        member( number( Num ), Opts ),
        print_set_of_passwords( Len, Num )
    ).
    
show_help :-
    opt_spec( Spec ),   
    opt_help( Spec, HelpText ),
    write( 'Usage: swipl pgen.pl <options>\n\n' ),
    write( HelpText ),
    nl.    
    
opt_spec([
    [opt(help),   type(boolean), default(false), shortflags([h]), longflags([help]),   
        help('Show Help')],
    [opt(length), type(integer), default(10),    shortflags([l]), longflags([length]), 
        help('Specify the length of each password.')], 
    [opt(number), type(integer), default(1),     shortflags([n]), longflags([number]), 
        help('Specify the number of passwords to create.')]    
]).    
    
print_set_of_passwords( Length, Number ) :-
    forall(
         between( 1, Number, _ ), 
        (
            random_pword( Length, P ), 
            maplist( format('~w'), P ), 
            nl
        )
    ).

random_pword( Length, Pword ) :-
    length( GenPword, Length ),
    findall( C, pword_char( _, C), PwordChars ),
    repeat, 
    maplist(populate_pword( PwordChars ), GenPword ),
    maplist( pword_char_rule( GenPword ), [lower, upper, digits, special] ),
    random_permutation( GenPword, Pword ).
    
populate_pword( PwordChars, C ) :- random_member( C, PwordChars ).

pword_char_rule( Pword, Type ) :-
    pword_char( Type, C ),
    member( C, Pword).
    
pword_char( lower, C ) :- member( C, "abcdefghijklmnopqrstuvwxyz" ).
pword_char( upper, C ) :- member( C, "ABCDEFGHIJKLMNOPQRSTUVWXYZ" ).
pword_char( digits, C ) :- member( C, "0123456789" ).
pword_char( special, C ) :- member( C, "!\"#$%&'()*+,-./:;<=>?@[]^_{|}~" ).


  

You may also check:How to resolve the algorithm Factors of a Mersenne number step by step in the CoffeeScript programming language
You may also check:How to resolve the algorithm Barnsley fern step by step in the Run BASIC programming language
You may also check:How to resolve the algorithm Odd word problem step by step in the Elixir programming language
You may also check:How to resolve the algorithm Arithmetic/Integer step by step in the R programming language
You may also check:How to resolve the algorithm Case-sensitivity of identifiers step by step in the Scala programming language