MBDyn-1.7.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups
crypt.cc
Go to the documentation of this file.
1 /* $Header: /var/cvs/mbdyn/mbdyn/mbdyn-1.0/libraries/libmbutil/crypt.cc,v 1.17 2017/01/12 14:44:04 masarati Exp $ */
2 /*
3  * MBDyn (C) is a multibody analysis code.
4  * http://www.mbdyn.org
5  *
6  * Copyright (C) 1996-2017
7  *
8  * Pierangelo Masarati <masarati@aero.polimi.it>
9  * Paolo Mantegazza <mantegazza@aero.polimi.it>
10  *
11  * Dipartimento di Ingegneria Aerospaziale - Politecnico di Milano
12  * via La Masa, 34 - 20156 Milano, Italy
13  * http://www.aero.polimi.it
14  *
15  * Changing this copyright notice is forbidden.
16  *
17  * This program is free software; you can redistribute it and/or modify
18  * it under the terms of the GNU General Public License as published by
19  * the Free Software Foundation (version 2 of the License).
20  *
21  *
22  * This program is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25  * GNU General Public License for more details.
26  *
27  * You should have received a copy of the GNU General Public License
28  * along with this program; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */
31 
32 #include "mbconfig.h" /* This goes first in every *.c,*.cc file */
33 
34 #include "myassert.h"
35 #include <stdlib.h>
36 #include <string.h>
37 #include "crypt.h"
38 
39 char *
40 mbdyn_make_salt(char *salt, size_t saltlen, const char *salt_format)
41 {
42  static char salt_charset[] =
43  "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789./";
44 
45  ASSERT(strlen(salt_charset) == 64);
46  ASSERT(salt);
47  ASSERT(saltlen > 2);
48 
49  char buf[34];
50 
51 #if defined(HAVE_DEV_RANDOM) || defined(HAVE_DEV_URANDOM)
52  FILE *fin = NULL;
53 
54 #if defined(HAVE_DEV_RANDOM)
55  fin = fopen("/dev/random");
56 #elif defined(HAVE_DEV_URANDOM)
57  fin = fopen("/dev/urandom");
58 #endif /* HAVE_DEV_RANDOM || HAVE_DEV_URANDOM */
59 
60  fread(buf, STRLENOF(buf), 1, fin);
61  buf[STRLENOF(buf)] = '\0';
62  fclose(fin);
63 
64  for (unsigned int i = 0; i < STRLENOF(buf); i++) {
65  buf[i] = salt_charset[buf[i] % STRLENOF(salt_charset)];
66  }
67 #else
68  for (unsigned int i = 0; i < STRLENOF(buf); i++) {
69  buf[i] = salt_charset[rand() % STRLENOF(salt_charset)];
70  }
71 #endif
72 
73  if (salt_format) {
74  snprintf(salt, saltlen, salt_format, buf);
75  } else {
76  strncpy(salt, buf, saltlen);
77  }
78 
79  return salt;
80 }
81 
#define ASSERT(expression)
Definition: colamd.c:977
#define STRLENOF(s)
Definition: mbdyn.h:166
char * mbdyn_make_salt(char *salt, size_t saltlen, const char *salt_format)
Definition: crypt.cc:40
static doublereal buf[BUFSIZE]
Definition: discctrl.cc:333