security/nss/lib/freebl/mpi/utils/prng.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/security/nss/lib/freebl/mpi/utils/prng.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,57 @@
     1.4 +/*
     1.5 + *  prng.c
     1.6 + *
     1.7 + *  Command-line pseudo-random number generator
     1.8 + *
     1.9 + * This Source Code Form is subject to the terms of the Mozilla Public
    1.10 + * License, v. 2.0. If a copy of the MPL was not distributed with this
    1.11 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
    1.12 +
    1.13 +#include <stdio.h>
    1.14 +#include <stdlib.h>
    1.15 +#include <limits.h>
    1.16 +#include <time.h>
    1.17 +
    1.18 +#ifdef __OS2__
    1.19 +#include <types.h>
    1.20 +#include <process.h>
    1.21 +#else
    1.22 +#include <unistd.h>
    1.23 +#endif
    1.24 +
    1.25 +#include "bbs_rand.h"
    1.26 +
    1.27 +int main(int argc, char *argv[])
    1.28 +{
    1.29 +  unsigned char *seed;
    1.30 +  unsigned int   ix, num = 1;
    1.31 +  pid_t          pid;
    1.32 +  
    1.33 +  if(argc > 1) {
    1.34 +    num = atoi(argv[1]);
    1.35 +    if(num <= 0) 
    1.36 +      num = 1;
    1.37 +  }
    1.38 +
    1.39 +  pid = getpid();
    1.40 +  srand(time(NULL) * (unsigned int)pid);
    1.41 +
    1.42 +  /* Not a perfect seed, but not bad */
    1.43 +  seed = malloc(bbs_seed_size);
    1.44 +  for(ix = 0; ix < bbs_seed_size; ix++) {
    1.45 +    seed[ix] = rand() % UCHAR_MAX;
    1.46 +  }
    1.47 +
    1.48 +  bbs_srand(seed, bbs_seed_size);
    1.49 +  memset(seed, 0, bbs_seed_size);
    1.50 +  free(seed);
    1.51 +
    1.52 +  while(num-- > 0) {
    1.53 +    ix = bbs_rand();
    1.54 +
    1.55 +    printf("%u\n", ix);
    1.56 +  }
    1.57 +
    1.58 +  return 0;
    1.59 +
    1.60 +}

mercurial