Generate randomized 256MB files onto your drive.(C++)

MosaicFuneral 2 Tallied Votes 3K Views Share

Haven't programmed in a long time. Last project was a GCODE pathway generator I wrote several years back. My compiler is obviously several years out of date.
Uses time(), itoa(), rand(). Current standard compliant compilers should have , , , and to_string() in suppliment to those more deprecated functions.

This is a very simple, high speed method of generatinga alot of randomized junk data onto your drive. Make sure you have about 300MB of RAM available for the overhead. An older non-SSD might croak.

Useful for making old files unrecoverable by overwriting the old space on a drive. Doesn't go about overwriting any file tables, partitions, etc. Only where the file system allows it to dump the files by defualt. So it's fairly safe, since you're not completely rewriting the disk.

/*generates 256MB randomized files*/
#include <iostream>
#include <fstream>
#include <vector>
#include <time.h>
#include <stdlib.h>
#include <string>
#include <sstream>
using namespace std;

void seed_kilo(string &kilo_seed)
{
    /*Reducing most of the rand() calls here, that reduces most of the CPU time.*/
    for(unsigned int j = 0; j < 1024; j++) {
        kilo_seed += (const char)rand()%256;
    }
}

void reseed_kilo(string &kilo_seed)
{
    char n = rand()%256;

    for(unsigned int j = 0; j < 1024; j++) {
        kilo_seed[j] = (const char)(kilo_seed[j] ^ n);
    }
}

void randomize_quarter_gig(vector<string> &quarter_gig, string &kilo_seed)
{
    string temp;
    char n = rand()%256;

    for(unsigned int i = 0; i < quarter_gig.size(); i++) {
        temp.clear();
        n = n ^ i; /*linear progression, but it's fast and random enough*/

        for(unsigned int j = 0; j < 1024; j++) {
            temp += (const char)(n ^ kilo_seed[j]);
        }

        quarter_gig[i] = temp;
    }
}

int main()
{
    vector<string>  quarter_gig(262144);
    string          temp, kilo_seed, input;
    ofstream        file;
    unsigned int    how_many = 0;
    char            num[4];

    srand(time(NULL));
    seed_kilo(kilo_seed);

    cout << "How many randomized 256MB files to generate ?--> ";
    getline(cin, input);
    stringstream(input) >> how_many;

    for(int i = 0; i < how_many; i++) {
        itoa(i, num, 10);
        temp = "stuff";
        temp += num;

        file.open(temp.c_str(), ofstream::out);

        if(!file.is_open())
            return(1);

        randomize_quarter_gig(quarter_gig, kilo_seed);

        cout << "256MB randomized.\n";

        for(unsigned int j = 0; j < quarter_gig.size(); j++) {
            file.write(quarter_gig[j].c_str(), quarter_gig[j].size());
        }

        file.close();

        cout << "File #" << (i+1) << " written." << "\n";

        if(i != (how_many - 1)) {
            reseed_kilo(kilo_seed);
        }
    }

    return(0);
}
DGPickett 20 Newbie Poster

For masking, write the random numbers directly, not as ascii. Write with fwrite() so each number is buffered into large block writes. Note that RAND_MAX is just 31 bits:

/usr/include/stdlib.h:#define RAND_MAX 2147483647

You can write 3 bytes at a time to have pure randomness. Remember that the bytes you want are located at different ends on little-endian hosts like x86 and big-endian hosts like SPARC. The middle 2 bytes are safe!

I would think that, for masking old data, a random block of a handy size like 65536 would suffice, written over and over to make a large file. More masking on magnetic media takes more write passes with different random data.

Many write passes might shorten SSD life, but I do not think SSD data are possible to recover after a rewrite.

Depending on the file system, you may be limited to 4GB files, but just open a new file and keep on. Systems may get unstable if there is no space left on the primary disk partition, so automated deletes immediately at write error would be nice.

commented: Thanks man. That's legit good advice. +11
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.