Deleted Added
full compact
random_password.c (78527) random_password.c (178825)
1/*
2 * Copyright (c) 1998, 1999 Kungliga Tekniska H�gskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:

--- 19 unchanged lines hidden (view full) ---

28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#include "kadmin_locl.h"
35
1/*
2 * Copyright (c) 1998, 1999 Kungliga Tekniska H�gskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:

--- 19 unchanged lines hidden (view full) ---

28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#include "kadmin_locl.h"
35
36RCSID("$Id: random_password.c,v 1.4 2001/02/15 04:20:53 assar Exp $");
36RCSID("$Id: random_password.c 21745 2007-07-31 16:11:25Z lha $");
37
38/* This file defines some a function that generates a random password,
39 that can be used when creating a large amount of principals (such
40 as for a batch of students). Since this is a political matter, you
41 should think about how secure generated passwords has to be.
42
43 Both methods defined here will give you at least 55 bits of
44 entropy.

--- 73 unchanged lines hidden (view full) ---

118 int len;
119 int freq;
120 } *classes;
121 va_list ap;
122 int len, i;
123 unsigned char rbuf[8]; /* random buffer */
124 int rleft = 0;
125
37
38/* This file defines some a function that generates a random password,
39 that can be used when creating a large amount of principals (such
40 as for a batch of students). Since this is a political matter, you
41 should think about how secure generated passwords has to be.
42
43 Both methods defined here will give you at least 55 bits of
44 entropy.

--- 73 unchanged lines hidden (view full) ---

118 int len;
119 int freq;
120 } *classes;
121 va_list ap;
122 int len, i;
123 unsigned char rbuf[8]; /* random buffer */
124 int rleft = 0;
125
126 *pw = NULL;
127
126 classes = malloc(num_classes * sizeof(*classes));
128 classes = malloc(num_classes * sizeof(*classes));
129 if(classes == NULL)
130 return;
127 va_start(ap, num_classes);
128 len = 0;
129 for(i = 0; i < num_classes; i++){
130 classes[i].str = va_arg(ap, const char*);
131 classes[i].len = strlen(classes[i].str);
132 classes[i].freq = va_arg(ap, int);
133 len += classes[i].freq;
134 }
135 va_end(ap);
136 *pw = malloc(len + 1);
131 va_start(ap, num_classes);
132 len = 0;
133 for(i = 0; i < num_classes; i++){
134 classes[i].str = va_arg(ap, const char*);
135 classes[i].len = strlen(classes[i].str);
136 classes[i].freq = va_arg(ap, int);
137 len += classes[i].freq;
138 }
139 va_end(ap);
140 *pw = malloc(len + 1);
137 if(*pw == NULL)
141 if(*pw == NULL) {
142 free(classes);
138 return;
143 return;
144 }
139 for(i = 0; i < len; i++) {
140 int j;
141 int x = RND(rbuf, sizeof(rbuf), &rleft) % (len - i);
142 int t = 0;
143 for(j = 0; j < num_classes; j++) {
144 if(x < t + classes[j].freq) {
145 (*pw)[i] = classes[j].str[RND(rbuf, sizeof(rbuf), &rleft)
146 % classes[j].len];
147 classes[j].freq--;
148 break;
149 }
150 t += classes[j].freq;
151 }
152 }
153 (*pw)[len] = '\0';
154 memset(rbuf, 0, sizeof(rbuf));
155 free(classes);
156}
157#endif
145 for(i = 0; i < len; i++) {
146 int j;
147 int x = RND(rbuf, sizeof(rbuf), &rleft) % (len - i);
148 int t = 0;
149 for(j = 0; j < num_classes; j++) {
150 if(x < t + classes[j].freq) {
151 (*pw)[i] = classes[j].str[RND(rbuf, sizeof(rbuf), &rleft)
152 % classes[j].len];
153 classes[j].freq--;
154 break;
155 }
156 t += classes[j].freq;
157 }
158 }
159 (*pw)[len] = '\0';
160 memset(rbuf, 0, sizeof(rbuf));
161 free(classes);
162}
163#endif