gen_random.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /**
  2. * \brief Generate random data into a file
  3. *
  4. * Copyright (C) 2006-2010, Brainspark B.V.
  5. *
  6. * This file is part of PolarSSL (http://www.polarssl.org)
  7. * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
  8. *
  9. * All rights reserved.
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License along
  22. * with this program; if not, write to the Free Software Foundation, Inc.,
  23. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  24. */
  25. #include "polarssl/config.h"
  26. #include "polarssl/havege.h"
  27. #include <time.h>
  28. #ifdef PRINTF_STDLIB
  29. #include <stdio.h>
  30. #endif
  31. #ifdef PRINTF_CUSTOM
  32. #include "tinystdio.h"
  33. #endif
  34. #if !defined(POLARSSL_HAVEGE_C)
  35. int main( void )
  36. {
  37. printf("POLARSSL_HAVEGE_C not defined.\n");
  38. return( 0 );
  39. }
  40. #else
  41. int main( int argc, char *argv[] )
  42. {
  43. FILE *f;
  44. time_t t;
  45. int i, j, k;
  46. havege_state hs;
  47. unsigned char buf[1024];
  48. if( argc < 2 )
  49. {
  50. fprintf( stderr, "usage: %s <output filename>\n", argv[0] );
  51. return( 1 );
  52. }
  53. if( ( f = fopen( argv[1], "wb+" ) ) == NULL )
  54. {
  55. printf( "failed to open '%s' for writing.\n", argv[0] );
  56. return( 1 );
  57. }
  58. havege_init( &hs );
  59. t = time( NULL );
  60. for( i = 0, k = 768; i < k; i++ )
  61. {
  62. for( j = 0; j < (int) sizeof( buf ); j++ )
  63. buf[j] = havege_rand( &hs );
  64. fwrite( buf, sizeof( buf ), 1, f );
  65. printf( "Generating 32Mb of data in file '%s'... %04.1f" \
  66. "%% done\r", argv[1], (100 * (float) (i + 1)) / k );
  67. fflush( stdout );
  68. }
  69. if( t == time( NULL ) )
  70. t--;
  71. fclose( f );
  72. return( 0 );
  73. }
  74. #endif /* POLARSSL_HAVEGE_C */