2 ** Copyright (c) 1999, 2000, 2001, 2002, 2003
3 ** Adel I. Mirzazhanov. All rights reserved
5 ** Redistribution and use in source and binary forms, with or without
6 ** modification, are permitted provided that the following conditions
9 ** 1.Redistributions of source code must retain the above copyright notice,
10 ** this list of conditions and the following disclaimer.
11 ** 2.Redistributions in binary form must reproduce the above copyright
12 ** notice, this list of conditions and the following disclaimer in the
13 ** documentation and/or other materials provided with the distribution.
14 ** 3.The name of the author may not be used to endorse or promote products
15 ** derived from this software without specific prior written permission.
17 ** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
18 ** OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 ** WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 ** ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
21 ** DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 ** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23 ** GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 ** INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 ** WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26 ** NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 ** SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 ** randpass.c - Random password generation module of PWGEN program
38 #include "base/rand_util.h"
44 ** gen_rand_pass - generates random password of specified type
46 ** char * - password string.
47 ** int - minimum password length.
48 ** int - maximum password length.
49 ** unsigned int - password generation mode.
51 ** int - password length or -1 on error.
56 gen_rand_pass (char *password_string
, int minl
, int maxl
, unsigned int pass_mode
)
62 int random_weight
[94];
64 int max_weight_element_number
= 0;
66 if (minl
> APG_MAX_PASSWORD_LENGTH
|| maxl
> APG_MAX_PASSWORD_LENGTH
||
67 minl
< 1 || maxl
< 1 || minl
> maxl
)
69 for (i
= 0; i
<= 93; i
++) random_weight
[i
] = 0;
70 length
= base::RandInt(minl
, maxl
);
71 str_pointer
= password_string
;
73 for (i
= 0; i
< length
; i
++)
75 /* Asign random weight in weight array if mode is present*/
76 for (j
= 0; j
<= 93 ; j
++)
77 if ( ( (pass_mode
& smbl
[j
].type
) > 0) &&
78 !( (S_RS
& smbl
[j
].type
) > 0))
79 random_weight
[j
] = base::RandInt(1, 20000);
81 /* Find an element with maximum weight */
82 for (j
= 0; j
<= 93; j
++)
83 if (random_weight
[j
] > max_weight
)
85 max_weight
= random_weight
[j
];
86 max_weight_element_number
= j
;
88 /* Get password symbol */
89 *str_pointer
= smbl
[max_weight_element_number
].ch
;
92 max_weight_element_number
= 0;
93 for (j
= 0; j
<= 93; j
++) random_weight
[j
] = 0;
100 ** gen_rand_symbol - generates random password of specified type
103 ** unsigned int - symbol type.
105 ** int - password length or -1 on error.
110 gen_rand_symbol (char *symbol
, unsigned int mode
)
114 int random_weight
[94];
116 int max_weight_element_number
= 0;
118 for (j
= 0; j
<= 93; j
++) random_weight
[j
] = 0;
119 str_pointer
= symbol
;
121 /* Asign random weight in weight array if mode is present*/
122 for (j
= 0; j
<= 93 ; j
++)
123 if ( ( (mode
& smbl
[j
].type
) > 0) &&
124 !( (S_RS
& smbl
[j
].type
) > 0))
125 random_weight
[j
] = base::RandInt(1, 20000);
127 /* Find an element with maximum weight */
128 for (j
= 0; j
<= 93; j
++)
129 if (random_weight
[j
] > max_weight
)
131 max_weight
= random_weight
[j
];
132 max_weight_element_number
= j
;
134 /* Get password symbol */
135 *str_pointer
= smbl
[max_weight_element_number
].ch
;
137 max_weight_element_number
= 0;
142 ** is_restricted_symbol - detcts if symbol is restricted rigt now
146 ** int - 0 - not restricted
152 is_restricted_symbol (char symbol
)
155 for (j
= 0; j
<= 93 ; j
++)
156 if (symbol
== smbl
[j
].ch
)
157 if ((S_RS
& smbl
[j
].type
) > 0)