MBDyn-1.7.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups
crypt.cc File Reference
#include "mbconfig.h"
#include "myassert.h"
#include <stdlib.h>
#include <string.h>
#include "crypt.h"
Include dependency graph for crypt.cc:

Go to the source code of this file.

Functions

char * mbdyn_make_salt (char *salt, size_t saltlen, const char *salt_format)
 

Function Documentation

char* mbdyn_make_salt ( char *  salt,
size_t  saltlen,
const char *  salt_format 
)

Definition at line 40 of file crypt.cc.

References ASSERT, buf, and STRLENOF.

Referenced by main().

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 }
#define ASSERT(expression)
Definition: colamd.c:977
#define STRLENOF(s)
Definition: mbdyn.h:166
static doublereal buf[BUFSIZE]
Definition: discctrl.cc:333