=================================================================== RCS file: /cvs/palm/Palm-Keyring/lib/Palm/Keyring.pm,v retrieving revision 1.21 retrieving revision 1.22 diff -u -r1.21 -r1.22 --- palm/Palm-Keyring/lib/Palm/Keyring.pm 2007/01/31 05:32:20 1.21 +++ palm/Palm-Keyring/lib/Palm/Keyring.pm 2007/02/01 01:56:11 1.22 @@ -1,6 +1,6 @@ package Palm::Keyring; -# $RedRiver: Keyring.pm,v 1.20 2007/01/31 04:29:08 andrew Exp $ +# $RedRiver: Keyring.pm,v 1.21 2007/01/31 05:32:20 andrew Exp $ # # Perl class for dealing with Keyring for Palm OS databases. # @@ -90,6 +90,9 @@ my $rec0_id = $self->{'records'}->[0]->{'id'}; if ($rec->{'encrypted'} && ! $rec->{'id'} == $rec0_id) { + if (! defined $rec->{'name'}) { + $rec->{'name'} = $EMPTY; + } $rec->{'data'} = join $NULL, $rec->{'name'}, $rec->{'encrypted'}; delete $rec->{'name'}; delete $rec->{'encrypted'}; @@ -120,21 +123,64 @@ croak("Incorrect Password!\n"); } - $self->{'digest'} ||= _calc_keys( $pass ); + $self->{'digest'} ||= _calc_keys( $pass ); $data->{'account'} ||= $EMPTY; $data->{'password'} ||= $EMPTY; $data->{'notes'} ||= $EMPTY; + my $changed = 0; + my $need_newdate = 0; + my $acct = {}; + if ($rec->{'encrypted'}) { + $acct = $self->Decrypt($rec, $pass); + foreach my $key (keys %{ $data }) { + next if $key eq 'lastchange'; + if ($data->{$key} ne $acct->{$key}) { + $changed = 1; + last; + } + } + if ( exists $data->{'lastchange'} && exists $acct->{'lastchange'} && ( + $data->{'lastchange'}->{day} != $acct->{'lastchange'}->{day} || + $data->{'lastchange'}->{month} != $acct->{'lastchange'}->{month} || + $data->{'lastchange'}->{year} != $acct->{'lastchange'}->{year} + )) { + $changed = 1; + $need_newdate = 0; + } else { + $need_newdate = 1; + } + + } else { + $changed = 1; + } + + # no need to re-encrypt if it has not changed. + return 1 if ! $changed; + my ($day, $month, $year); - # XXX the 0 here is a placeholder for $record_has_changed. - if ( 0 || ! $data->{'lastchange'}) { + if ($data->{'lastchange'} && ! $need_newdate ) { + $day = $data->{'lastchange'}->{'day'} || 1; + $month = $data->{'lastchange'}->{'month'} || 0; + $year = $data->{'lastchange'}->{'year'} || 0; + + # XXX Need to actually validate the above information somehow + if ($year >= 1900) { + $year -= 1900; + } + } else { + $need_newdate = 1; + } + + if ($need_newdate) { ($day, $month, $year) = (localtime)[3,4,5]; - $year -= 4; - $month++; } + $year -= 4; + $month++; + my $p = $day | ($month << 5) | ($year << 9); my $packeddate = pack 'n', $p; @@ -237,6 +283,7 @@ foreach my $i (0..$#accts) { next if $i == 0; + delete $self->{'records'}->[$i]->{'encrypted'}; $self->Encrypt($self->{'records'}->[$i], $accts[$i], $pass); } } @@ -468,14 +515,20 @@ }, }; +If you have changed anything other than the lastchange, or don't pass in a +lastchange record, Encrypt() will generate a new lastchange for you. + +If you pass in a lastchange field that is different than the one in the +record, it will honor what you passed in. + +It also only uses the $acct->{'name'} if there is not already a $rec->{'name'}. + =head2 Decrypt my $acct = $pdb->Decrypt($rec[, $password]); Decrypts the record and returns a hashref for the account as described under Encrypt(). -However, it ignores the "lastchange" field and generates its own. -It also only uses the "name" field if there is not already a $rec->{'name'}. foreach (0..$#{ $pdb->{'records'}) { next if $_ == 0;