AnonymousNamefag

srm.dod.c

Jan 24th, 2019
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.92 KB | None | 0 0
  1. /*******************************************
  2.  * srm-dod v. 1.0                          *
  3.  * Description: Implementation of the DoD  *
  4.  * secure erase algorithm with a couple of *
  5.  * modifications for extra obscurity       *
  6.  * Author: Michael Warren                  *
  7.  * Date: January 8 & 17 2019               *
  8.  *******************************************/
  9.  
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <errno.h>
  13. #include <string.h>
  14. #include <time.h>
  15.  
  16. int main( int argc, char **argv ){
  17.     FILE *fp;
  18.     for( int i = 1; i < argc; i++ ){
  19.         if( !(fp = fopen( argv[i], "r+" )) ){
  20.             // Error handling section:
  21.             char errorstring[strlen( argv[0] ) + strlen( argv[i] ) + 3];
  22.             sprintf( errorstring, "%s: %s", argv[0], argv[i] );
  23.             perror( errorstring );
  24.             exit( errno );
  25.         }
  26.         // Determine file length:
  27.         fseek( fp, 0, SEEK_END );
  28.         long file_length = ftell( fp );
  29.         rewind( fp );
  30.  
  31.         // Proceed with DoD algorithm:
  32.         srand( time( NULL ) );
  33.         char c = rand() % 256;
  34.         for( int i = 0; i < file_length; i++ ){
  35.             fputc( c, fp );
  36.         }
  37.         rewind( fp );
  38.         c = ~c;
  39.         for( int i = 0; i < file_length; i++ ){
  40.             fputc( c, fp );
  41.         }
  42.         rewind( fp );
  43.         for( int i = 0; i < file_length; i++ ){
  44.             fputc( rand() % 256, fp );
  45.         }
  46.         rewind( fp );
  47.  
  48.         // Two extra zero passes to conceal the
  49.         // fact that data was securely erased:
  50.         for( int i = 0; i < file_length; i++ ){
  51.             fputc( '\0', fp );
  52.         }
  53.         rewind( fp );
  54.         for( int i = 0; i < file_length; i++ ){
  55.             fputc( '\0', fp );
  56.         }
  57.  
  58.         // Truncate:
  59.         fclose( fp );
  60.         fp = fopen( argv[i], "w" );
  61.         fclose( fp );
  62.  
  63.         // Change filename and delete:
  64.         char newname[TMP_MAX];
  65.         for( int i = 0; i < TMP_MAX; i++ ){
  66.             newname[i] = rand() % 26 + 'A';
  67.         }
  68.         if( rename( argv[i], newname ) ){
  69.             printf( "%s: Error renaming file %s\n", argv[0], argv[i] );
  70.             printf( "Error code: %d\n", errno );
  71.             exit( errno );
  72.         }
  73.         remove( newname );
  74.         fclose( fp );
  75.     }
  76.     return 0;
  77. }
Add Comment
Please, Sign In to add comment