Die /dev/urandom
pseudo-Gerät, zusammen mit dd
, kann dies für Sie tun:
dd if=/dev/urandom of=newfile bs=1M count=10
Dies wird eine Datei newfile
von Größe 10M erstellen.
Das Gerät /dev/random
blockiert häufig, wenn keine ausreichende Zufälligkeit vorhanden ist, urandom
wird nicht blockiert. Wenn Sie die Zufälligkeit für Krypto-Zeug verwenden, können Sie sich von urandom
fernhalten. Für alles andere sollte es ausreichend und wahrscheinlich schneller sein.
Wenn Sie möchten, zu korrumpieren nur Bits der Datei (nicht die gesamte Datei), können Sie einfach die C-Stil Zufallsfunktionen verwenden. Verwenden Sie einfach rnd()
eine Offset und Länge n
, um herauszufinden, dann verwenden Sie es n
mal zufällige Bytes packen Sie Ihre Datei mit zu überschreiben.
Der folgende Perl-Skript zeigt, wie diese (ohne C-Code zu kümmern über das Kompilieren) getan werden kann:
use strict;
use warnings;
sub corrupt ($$$$) {
# Get parameters, names should be self-explanatory.
my $filespec = shift;
my $mincount = shift;
my $maxcount = shift;
my $charset = shift;
# Work out position and size of corruption.
my @fstat = stat ($filespec);
my $size = $fstat[7];
my $count = $mincount + int (rand ($maxcount + 1 - $mincount));
my $pos = 0;
if ($count >= $size) {
$count = $size;
} else {
$pos = int (rand ($size - $count));
}
# Output for debugging purposes.
my $last = $pos + $count - 1;
print "'$filespec', $size bytes, corrupting $pos through $last\n";
# Open file, seek to position, corrupt and close.
open (my $fh, "+<$filespec") || die "Can't open $filespec: $!";
seek ($fh, $pos, 0);
while ($count-- > 0) {
my $newval = substr ($charset, int (rand (length ($charset) + 1)), 1);
print $fh $newval;
}
close ($fh);
}
# Test harness.
system ("echo =========="); #DEBUG
system ("cp base-testfile testfile"); #DEBUG
system ("cat testfile"); #DEBUG
system ("echo =========="); #DEBUG
corrupt ("testfile", 8, 16, "ABCDEFGHIJKLMNOPQRSTUVWXYZ ");
system ("echo =========="); #DEBUG
system ("cat testfile"); #DEBUG
system ("echo =========="); #DEBUG
Es besteht aus der corrupt
Funktion, Sie rufen mit einem Dateinamen, minimaler und maximaler Korruptionsgröße und einem Zeichensatz auf, um die Korruption zu zeichnen. Das Bit an der Unterseite ist nur Unit-Test-Code. Nachfolgend finden Sie einige Beispielausgabe, wo Sie, dass ein Abschnitt der Datei beschädigt ist, sehen kann:
auf einer grundlegenden Ebene
==========
this is a file with nothing in it except for lowercase
letters (and spaces and punctuation and newlines).
that will make it easy to detect corruptions from the
test program since the character range there is from
uppercase a through z.
i have to make it big enough so that the random stuff
will work nicely, which is why i am waffling on a bit.
==========
'testfile', 344 bytes, corrupting 122 through 135
==========
this is a file with nothing in it except for lowercase
letters (and spaces and punctuation and newlines).
that will make iFHCGZF VJ GZDYct corruptions from the
test program since the character range there is from
uppercase a through z.
i have to make it big enough so that the random stuff
will work nicely, which is why i am waffling on a bit.
==========
Es getestet, aber man kann es Pflege finden Kantenfehlerfälle sind, die getroffen werden müssen. Mach damit was du willst.
+1 Correct - obwohl man den Unterschied zwischen zufälligen und urandom erklären könnten wollen ... – Konerak
Eigentlich hatte ich zu verwenden 10M für die dd arg 'bs' – Benoit
'urandom' ist nicht so schnell, wie ich möchte. "urandom" generiert Sequenzen, die gut genug sind, um private Schlüssel zu erstellen. Ich brauche solche Garantien nicht, aber ich will mehr Geschwindigkeit. – akostadinov