gen_random.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. #include <stdio.h>
  29. #if !defined(POLARSSL_HAVEGE_C)
  30. int main( void )
  31. {
  32. printf("POLARSSL_HAVEGE_C not defined.\n");
  33. return( 0 );
  34. }
  35. #else
  36. int main( int argc, char *argv[] )
  37. {
  38. FILE *f;
  39. time_t t;
  40. int i, j, k;
  41. havege_state hs;
  42. unsigned char buf[1024];
  43. if( argc < 2 )
  44. {
  45. fprintf( stderr, "usage: %s <output filename>\n", argv[0] );
  46. return( 1 );
  47. }
  48. if( ( f = fopen( argv[1], "wb+" ) ) == NULL )
  49. {
  50. printf( "failed to open '%s' for writing.\n", argv[0] );
  51. return( 1 );
  52. }
  53. havege_init( &hs );
  54. t = time( NULL );
  55. for( i = 0, k = 768; i < k; i++ )
  56. {
  57. for( j = 0; j < (int) sizeof( buf ); j++ )
  58. buf[j] = havege_rand( &hs );
  59. fwrite( buf, sizeof( buf ), 1, f );
  60. printf( "Generating 32Mb of data in file '%s'... %04.1f" \
  61. "%% done\r", argv[1], (100 * (float) (i + 1)) / k );
  62. fflush( stdout );
  63. }
  64. if( t == time( NULL ) )
  65. t--;
  66. fclose( f );
  67. return( 0 );
  68. }
  69. #endif /* POLARSSL_HAVEGE_C */