From 3bbb724b08d7d796b4e9b4cd3edc24255fc29f70 Mon Sep 17 00:00:00 2001 From: Marcin Kasperski Date: Sun, 18 Nov 2012 00:46:34 +0100 Subject: [PATCH] (needs careful review) Attempt to improve performance. Previous implementation happened to be very slow (on my laptop it took 5 minutes to read small pwsafe file and save it back). I profiled the code and it turned out, this cost was totally dominated by the Crypt::PWSafe3::random function (which on every call accesses system random seed). I replaced it with Bytes::Random::Secure which seeds using OS crypto but afterwards uses embedded generator. Performance improvement is drastical, the same read&save takes about half a second on the same laptop. --- Makefile.PL | 1 + lib/Crypt/PWSafe3.pm | 15 +++++++++++---- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/Makefile.PL b/Makefile.PL index 1c98889..f7f557b 100644 --- a/Makefile.PL +++ b/Makefile.PL @@ -12,6 +12,7 @@ WriteMakefile( 'Crypt::Random' => 1.25, 'Data::UUID' => 1.217, 'Shell' => 0.5, + 'Bytes::Random::Secure' => 0.09, }, 'AUTHOR' => 'Thomas Linden ', 'clean' => { diff --git a/lib/Crypt/PWSafe3.pm b/lib/Crypt/PWSafe3.pm index 8d21736..f38010c 100644 --- a/lib/Crypt/PWSafe3.pm +++ b/lib/Crypt/PWSafe3.pm @@ -621,12 +621,19 @@ sub writebytes { } } +# This is original, very slow random +#sub random { +# # +# # helper, return some secure random bytes +# my($this, $len) = @_; +# my $bits = makerandom(Size => 256, Strength => 1); +# return substr($bits, 0, $len); +#} + +use Bytes::Random::Secure qw(random_bytes random_bytes_hex); sub random { - # - # helper, return some secure random bytes my($this, $len) = @_; - my $bits = makerandom(Size => 256, Strength => 1); - return substr($bits, 0, $len); + return random_bytes($len); } sub getheader {