casta/bcrypt — pull request #1 by mstap (2012-11-25 11:51:23+00:00)
Title: support large files (larger than RAM and larger than 2 GiB)

This commit makes bcrypt use a window of 256K when encrypting/decrypting
files and adds support for large files (> 2 GiB on 32-bit platforms).

I have verified that the tool still works correctly by using this automated script:

```perl
#!/usr/bin/env perl
# vim:ts=4:sw=4:expandtab

use strict;
use warnings;
use Data::Dumper;
use v5.10;

# verify that we can still decompress all files which the original bcrypt creates
for (my $cnt = 0; $cnt < 1000; $cnt++) {
say "Iteration $cnt / 1000";
# test files with up to 20 MiB. It’s unlikely that there is any bug which
# only triggers for > 20 MiB and unfortunately, bcrypt is slow.
my $filesize = int(rand(20 * 1024 * 1024));

system(qq|dd if=/dev/urandom of=/tmp/input bs=$filesize count=1|);
chomp(my $hash_before = qx(md5sum /tmp/input));

my $compress = (int(rand(5)) == 3 ? "-c" : "");
system(qq,echo 'foobarbaz\nfoobarbaz' | /usr/bin/bcrypt $compress /tmp/input,);

system(qq,echo 'foobarbaz' | ./bcrypt /tmp/input.bfe,);
chomp(my $hash_after = qx(md5sum /tmp/input));

if ($hash_before ne $hash_after) {
die "different hashes";
}
}

# verify that the original bcrypt can still decrypt our files
for (my $cnt = 0; $cnt < 1000; $cnt++) {
say "Iteration $cnt / 1000";
# test files with up to 20 MiB. It’s unlikely that there is any bug which
# only triggers for > 20 MiB and unfortunately, bcrypt is slow.
my $filesize = int(rand(20 * 1024 * 1024));

system(qq|dd if=/dev/urandom of=/tmp/input bs=$filesize count=1|);
chomp(my $hash_before = qx(md5sum /tmp/input));

my $compress = (int(rand(5)) == 3 ? "-c" : "");
system(qq,echo 'foobarbaz\nfoobarbaz' | ./bcrypt $compress /tmp/input,);

system(qq,echo 'foobarbaz' | /usr/bin/bcrypt /tmp/input.bfe,);
chomp(my $hash_after = qx(md5sum /tmp/input));

if ($hash_before ne $hash_after) {
die "different hashes";
}
}
```

The script runs 1000 iterations of each test, that is, it tests that the new version can still read files created by the old version, and it tests that the old version can still read files created by the new version.

I also tested that files created on big-endian machines can be decrypted on little-endian machines and vice-versa.