OLD | NEW |
1 /* | 1 /* |
2 ** Copyright (c) 1999, 2000, 2001, 2002, 2003 | 2 ** Copyright (c) 1999, 2000, 2001, 2002, 2003 |
3 ** Adel I. Mirzazhanov. All rights reserved | 3 ** Adel I. Mirzazhanov. All rights reserved |
4 ** | 4 ** |
5 ** Redistribution and use in source and binary forms, with or without | 5 ** Redistribution and use in source and binary forms, with or without |
6 ** modification, are permitted provided that the following conditions | 6 ** modification, are permitted provided that the following conditions |
7 ** are met: | 7 ** are met: |
8 ** | 8 ** |
9 ** 1.Redistributions of source code must retain the above copyright notice, | 9 ** 1.Redistributions of source code must retain the above copyright notice, |
10 ** this list of conditions and the following disclaimer. | 10 ** this list of conditions and the following disclaimer. |
(...skipping 16 matching lines...) Expand all Loading... |
27 ** SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 27 ** SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
28 */ | 28 */ |
29 | 29 |
30 /* | 30 /* |
31 ** randpass.c - Random password generation module of PWGEN program | 31 ** randpass.c - Random password generation module of PWGEN program |
32 */ | 32 */ |
33 | 33 |
34 #include <stdio.h> | 34 #include <stdio.h> |
35 #include <stdlib.h> | 35 #include <stdlib.h> |
36 #include <time.h> | 36 #include <time.h> |
37 #if !defined(WIN32) && !defined(_WIN32) && !defined(__WIN32) && !defined(__WIN32
__) | 37 |
38 #include <pwd.h> | 38 #include "base/rand_util.h" |
39 #endif | 39 #include "owntypes.h" |
40 #include <unistd.h> | |
41 #include "randpass.h" | 40 #include "randpass.h" |
42 | |
43 #include "owntypes.h" | |
44 #include "smbl.h" | 41 #include "smbl.h" |
45 | 42 |
46 /* | 43 /* |
47 ** gen_rand_pass - generates random password of specified type | 44 ** gen_rand_pass - generates random password of specified type |
48 ** INPUT: | 45 ** INPUT: |
49 ** char * - password string. | 46 ** char * - password string. |
50 ** int - minimum password length. | 47 ** int - minimum password length. |
51 ** int - maximum password length. | 48 ** int - maximum password length. |
52 ** unsigned int - password generation mode. | 49 ** unsigned int - password generation mode. |
53 ** OUTPUT: | 50 ** OUTPUT: |
54 ** int - password length or -1 on error. | 51 ** int - password length or -1 on error. |
55 ** NOTES: | 52 ** NOTES: |
56 ** none. | 53 ** none. |
57 */ | 54 */ |
58 int | 55 int |
59 gen_rand_pass (char *password_string, int minl, int maxl, unsigned int pass_mode
) | 56 gen_rand_pass (char *password_string, int minl, int maxl, unsigned int pass_mode
) |
60 { | 57 { |
61 int i = 0; | 58 int i = 0; |
62 int j = 0; | 59 int j = 0; |
63 int length = 0; | 60 int length = 0; |
64 char *str_pointer; | 61 char *str_pointer; |
65 int random_weight[94]; | 62 int random_weight[94]; |
66 int max_weight = 0; | 63 int max_weight = 0; |
67 int max_weight_element_number = 0; | 64 int max_weight_element_number = 0; |
68 | 65 |
69 if (minl > APG_MAX_PASSWORD_LENGTH || maxl > APG_MAX_PASSWORD_LENGTH || | 66 if (minl > APG_MAX_PASSWORD_LENGTH || maxl > APG_MAX_PASSWORD_LENGTH || |
70 minl < 1 || maxl < 1 || minl > maxl) | 67 minl < 1 || maxl < 1 || minl > maxl) |
71 return (-1); | 68 return (-1); |
72 for (i = 0; i <= 93; i++) random_weight[i] = 0; | 69 for (i = 0; i <= 93; i++) random_weight[i] = 0; |
73 length = minl + randint(maxl-minl+1); | 70 length = base::RandInt(minl, maxl); |
74 str_pointer = password_string; | 71 str_pointer = password_string; |
75 | 72 |
76 for (i = 0; i < length; i++) | 73 for (i = 0; i < length; i++) |
77 { | 74 { |
78 /* Asign random weight in weight array if mode is present*/ | 75 /* Asign random weight in weight array if mode is present*/ |
79 for (j = 0; j <= 93 ; j++) | 76 for (j = 0; j <= 93 ; j++) |
80 if ( ( (pass_mode & smbl[j].type) > 0) && | 77 if ( ( (pass_mode & smbl[j].type) > 0) && |
81 !( (S_RS & smbl[j].type) > 0)) | 78 !( (S_RS & smbl[j].type) > 0)) |
82 » random_weight[j] = 1 + randint(20000); | 79 random_weight[j] = base::RandInt(1, 20000); |
83 j = 0; | 80 j = 0; |
84 /* Find an element with maximum weight */ | 81 /* Find an element with maximum weight */ |
85 for (j = 0; j <= 93; j++) | 82 for (j = 0; j <= 93; j++) |
86 if (random_weight[j] > max_weight) | 83 if (random_weight[j] > max_weight) |
87 { | 84 { |
88 max_weight = random_weight[j]; | 85 max_weight = random_weight[j]; |
89 max_weight_element_number = j; | 86 max_weight_element_number = j; |
90 } | 87 } |
91 /* Get password symbol */ | 88 /* Get password symbol */ |
92 *str_pointer = smbl[max_weight_element_number].ch; | 89 *str_pointer = smbl[max_weight_element_number].ch; |
(...skipping 25 matching lines...) Expand all Loading... |
118 int max_weight = 0; | 115 int max_weight = 0; |
119 int max_weight_element_number = 0; | 116 int max_weight_element_number = 0; |
120 | 117 |
121 for (j = 0; j <= 93; j++) random_weight[j] = 0; | 118 for (j = 0; j <= 93; j++) random_weight[j] = 0; |
122 str_pointer = symbol; | 119 str_pointer = symbol; |
123 j = 0; | 120 j = 0; |
124 /* Asign random weight in weight array if mode is present*/ | 121 /* Asign random weight in weight array if mode is present*/ |
125 for (j = 0; j <= 93 ; j++) | 122 for (j = 0; j <= 93 ; j++) |
126 if ( ( (mode & smbl[j].type) > 0) && | 123 if ( ( (mode & smbl[j].type) > 0) && |
127 !( (S_RS & smbl[j].type) > 0)) | 124 !( (S_RS & smbl[j].type) > 0)) |
128 » random_weight[j] = 1 + randint(20000); | 125 random_weight[j] = base::RandInt(1, 20000); |
129 j = 0; | 126 j = 0; |
130 /* Find an element with maximum weight */ | 127 /* Find an element with maximum weight */ |
131 for (j = 0; j <= 93; j++) | 128 for (j = 0; j <= 93; j++) |
132 if (random_weight[j] > max_weight) | 129 if (random_weight[j] > max_weight) |
133 { | 130 { |
134 max_weight = random_weight[j]; | 131 max_weight = random_weight[j]; |
135 max_weight_element_number = j; | 132 max_weight_element_number = j; |
136 } | 133 } |
137 /* Get password symbol */ | 134 /* Get password symbol */ |
138 *str_pointer = smbl[max_weight_element_number].ch; | 135 *str_pointer = smbl[max_weight_element_number].ch; |
(...skipping 15 matching lines...) Expand all Loading... |
154 int | 151 int |
155 is_restricted_symbol (char symbol) | 152 is_restricted_symbol (char symbol) |
156 { | 153 { |
157 int j = 0; | 154 int j = 0; |
158 for (j = 0; j <= 93 ; j++) | 155 for (j = 0; j <= 93 ; j++) |
159 if (symbol == smbl[j].ch) | 156 if (symbol == smbl[j].ch) |
160 if ((S_RS & smbl[j].type) > 0) | 157 if ((S_RS & smbl[j].type) > 0) |
161 return(1); | 158 return(1); |
162 return(0); | 159 return(0); |
163 } | 160 } |
OLD | NEW |