]> Dogcows Code - chaz/p5-File-KDBX-XS/blob - eg/benchmark.pl
initial commit
[chaz/p5-File-KDBX-XS] / eg / benchmark.pl
1 #!/usr/bin/env perl
2
3 use warnings;
4 use strict;
5
6 use Crypt::Rijndael;
7 use Crypt::Cipher;
8 use File::KDBX::XS;
9
10 use Benchmark qw(:all :hireswallclock);
11 use Test::More;
12
13 my $iterations = shift // 50;
14
15 my $rounds = 500_000;
16 my $key = "\1" x 32;
17 my $seed = "\1" x 16;
18 my $expected = pack('H*', '3f7dfb512060cc8be094cd259c7ff03c');
19
20 sub xs {
21 my $result = File::KDBX::KDF::AES::_transform_half_xs($key, $seed, $rounds);
22 return $result;
23 }
24
25 sub cryptx {
26 my $cipher = Crypt::Cipher->new('AES', $key);
27 my $result = $seed;
28 for (my $i = 0; $i < $rounds; ++$i) {
29 $result = $cipher->encrypt($result);
30 }
31 return $result;
32 }
33
34 sub crypt_rijndael {
35 my $cipher = Crypt::Rijndael->new($key, Crypt::Rijndael::MODE_ECB());
36 my $result = $seed;
37 for (my $i = 0; $i < $rounds; ++$i) {
38 $result = $cipher->encrypt($result);
39 }
40 return $result;
41 }
42
43 my $r = xs();
44 is $r, $expected, 'AES KDF transform works' or diag explain unpack('H*', $r);
45 is $r, cryptx(), 'XS transform agrees with CryptX';
46 is $r, crypt_rijndael(), 'XS transform agrees with Crypt::Rijndael';
47
48 done_testing;
49
50 my $timings = timethese($iterations, {
51 crypt_rijndael => \&crypt_rijndael,
52 cryptx => \&cryptx,
53 xs => \&xs,
54 });
55 cmpthese($timings);
This page took 0.032122 seconds and 4 git commands to generate.