Tie-EncryptedHash-1.24/ 0000755 0000764 0000764 00000000000 11037167600 014011 5 ustar vipul vipul Tie-EncryptedHash-1.24/t/ 0000755 0000764 0000764 00000000000 11037167600 014254 5 ustar vipul vipul Tie-EncryptedHash-1.24/t/ref.t 0000644 0000764 0000764 00000005113 11037065234 015215 0 ustar vipul vipul #!/usr/bin/perl -s ## ## Copyright (c) 2000, Vipul Ved Prakash. All rights reserved. ## This code is free software; you can redistribute it and/or modify ## it under the same terms as Perl itself. ## ## $Id: ref.t,v 1.1 2000/08/31 16:31:13 vipul Exp vipul $ use lib '../lib'; use lib 'lib'; use Tie::EncryptedHash; use Data::Dumper; no warnings; print "1..15\n"; my %h = (); tie %h, Tie::EncryptedHash, 'Blacksun', 'Blowfish'; print "STORE/FETCH explicit reference:\n"; $h{_bamf} = {first => 1.16, second => 0.99 , third => { a => 'b' } }; $h{_age} = 12; print $h{_bamf}->{third}->{a} eq "b" ? "ok 1\n" : "not ok 1\n"; print $h{_bamf}; print "FETCH explicit reference with incorrect password:\n"; $h{__password} = 10; #delete $h{__password}; print "$h{_bamf}\n"; print $h{_bamf} =~ /^Blowfish/ ? "ok 2\n" : "not ok 2\n"; print "Autovivification:\n"; $h{__password} = "foobar"; $h{_foo }->{bar}->{cow} = "baw"; print $h{_foo}->{bar}{cow} eq "baw" ? "ok 3\n" : "not ok 3\n"; $h{__password} = "Hownow?"; print "Autovivification with incorrect password:\n"; print $h{_foo}->{bar}{cow} eq "baw" ? "not ok 4\n" : "ok 4\n"; print "Encryption and serialization of implicit references at FETCH:\n"; $h{__password} = 17; $h{_vivi}->{x} = 4; $h{__password} = 16; print $h{_vivi} =~ /^Blowfish/ ? "ok 5\n" : "not ok 5\n"; # checks if autovivification with incorrect password has clobbered our # hash. print "Recovery from password change:\n"; $h{__password} = 10; $h{_boo}->{bam} = 12; print $h{_boo}{bam} eq 12 ? "ok 6\n" : "not ok 6\n"; print "More Autovivification:\n"; $h{_bar}{a} = "x"; $h{_bar}{b} = "y"; $h{_bar}{d} = "y"; $h{_bar}{e} = { u => 'y' }; print $h{_bar}{e}{u} eq 'y' ? "ok 7\n" : "not ok 7\n"; $h{__password} = "xx"; print $h{_bar} =~ /^Blowfish/ ? "ok 8\n" : "not ok 8\n"; $h{__password} = "Blacksun"; print "STORE/FETCH encrypted listref:\n"; $h{_list}->[4] = 15; print $h{_list}->[4] == 15 ? "ok 9\n" : "not ok 9\n"; $h{_linked}{list1}{list2}[6] = 15; print $h{_linked}{list1}{list2}[6] == 15 ? "ok 10\n" : "not ok 10\n"; print "FETCH encrypted listref with incorrect password:\n"; $h{__password} = 24; print $h{_linked}{list1}{list2}[6] == 15 ? "not ok 11\n" : "ok 11\n"; print $h{_linked} =~ /^Blowfish/ ? "ok 12\n" : "not ok 12\n"; print "STORE/FETCH encrypted scalarref:\n"; $h{__password} = "Blacksun"; $h{_scalar} = \"string"; print ${$h{_scalar}} eq "string" ? "ok 13\n" : "not ok 13\n"; print "STORE/FETCH encrypted scalarref with incorrect password:\n"; $h{__password} = 12; print ${$h{_scalar}} eq "string" ? "not ok 14\n" : "ok 14\n"; print $h{_scalar} =~ /^Blowfish/ ? "ok 15\n" : "not ok 15"; Tie-EncryptedHash-1.24/t/construction.t 0000644 0000764 0000764 00000001356 07153504107 017201 0 ustar vipul vipul #!/usr/bin/perl -s ## ## Copyright (c) 2000, Vipul Ved Prakash. All rights reserved. ## This code is free software; you can redistribute it and/or modify ## it under the same terms as Perl itself. ## ## $Id: construction.t,v 1.1 2000/08/31 16:31:03 vipul Exp vipul $ use lib '../lib'; use lib 'lib'; use Tie::EncryptedHash; print "1..2\n"; my %h = (); tie %h, Tie::EncryptedHash, 'Blacksun', 'Blowfish'; $h{_enc} = "hush hush"; delete $h{__password}; print $h{_enc} =~ /^Blowfish/ ? "ok 1" : "not ok 1"; print qq{\n}; my $h = new Tie::EncryptedHash __password => 'Blacksun', __cipher => 'DES'; $h->{_enc} = "hush hush"; delete $h->{__password}; print $h->{_enc} =~ /^DES/ ? "ok 2" : "not ok 2"; print qq{\n}; Tie-EncryptedHash-1.24/t/hide.t 0000644 0000764 0000764 00000001354 11037065214 015353 0 ustar vipul vipul #!/usr/bin/perl -s ## ## Copyright (c) 2000, Vipul Ved Prakash. All rights reserved. ## This code is free software; you can redistribute it and/or modify ## it under the same terms as Perl itself. ## ## $Id: hide.t,v 1.1 2000/08/31 16:31:07 vipul Exp vipul $ use lib '../lib'; use lib 'lib'; use Tie::EncryptedHash; no warnings; print "1..4\n"; my %h = (); tie %h, Tie::EncryptedHash, 'Blacksun'; my $i = 1; $h{_a} = 'b'; $h{_b}{c}{d} = "meme"; $h{__hide} = 1; delete $h{__password}; print $h{_a} eq "" ? "ok $i\n" : "not ok $i\n"; $i++; print $h{_b} eq "" ? "ok $i\n" : "not ok $i\n"; $i++; delete $h{__hide }; print $h{_a} =~ /^Blowfish/ ? "ok $i\n" : "not ok $i\n"; $i++; print $h{_b} =~ /^Blowfish/ ? "ok $i\n" : "not ok $i\n"; $i++; Tie-EncryptedHash-1.24/t/reforgy.t 0000644 0000764 0000764 00000002644 11037065523 016125 0 ustar vipul vipul #!/usr/bin/perl -s ## ## Copyright (c) 2000, Vipul Ved Prakash. All rights reserved. ## This code is free software; you can redistribute it and/or modify ## it under the same terms as Perl itself. ## ## $Id: reforgy.t,v 1.1 2000/08/31 16:31:16 vipul Exp vipul $ use lib '../lib'; use lib 'lib'; no warnings; use Tie::EncryptedHash; use Data::Dumper qw(DumperX); my @a = ('Araa'..'Zeck'); sub ralpha { _.$a[int(rand(26))] } sub rnum { return int(rand(4)) } print "1..1\n"; print "generating a large, random data structure...\n"; my %h = (); tie %h, Tie::EncryptedHash, 'Blacksun', 'Blowfish'; for ( 1..50 ) { my $rnd = int(rand(7)); if ( $rnd == 1 ) { $h{ralpha()}->[rnum()] = { ralpha() => ralpha().ralpha() }; } elsif ( $rnd == 2 ) { $h{ralpha()}->[rnum()]->{ralpha()} => ralpha (); } elsif ( $rnd == 3 ) { $h{ralpha()}->[rnum()] = { ralpha() => [1..rnum()] } } elsif ( $rnd == 4 ) { $h{ralpha()}->[rnum()]->{'HASH_' . ralpha()} = { ralpha() => ralpha() }; } elsif ( $rnd == 5 ) { $h{ralpha()}->[rnum()]->{'ARRAY_' . ralpha()} = ralpha (); } elsif ( $rnd == 6 ) { for ( 0 .. rnum() ) { $h{ralpha()}->[rnum()]->{ralpha()}->[$_] = { ralpha() => [1..rnum()] }; $h{ralpha()}->[$_] = { ralpha() => [1..rnum()] }; } } } $h{__password} = "sds"; print DumperX \%h; $h{__password} = "Blacksun"; print DumperX \%h; print "ok 1\n"; Tie-EncryptedHash-1.24/t/emptysubclass.t 0000644 0000764 0000764 00000013200 11037065216 017333 0 ustar vipul vipul #!/usr/bin/perl -s ## ## Copyright (c) 2000, Vipul Ved Prakash. All rights reserved. ## This code is free software; you can redistribute it and/or modify ## it under the same terms as Perl itself. ## ## $Id: emptysubclass.t,v 1.1 2000/08/31 16:31:05 vipul Exp vipul $ package Tie::SecretHash; use lib '../lib'; use lib 'lib'; use Tie::EncryptedHash; @ISA = qw(Tie::EncryptedHash); package main; use Data::Dumper; no warnings; %v = ( password => 'Doorknob', password_wrong => 'Bweoo', cipher => 'Blowfish', plaintext => 'Mirrorshades.', ); print "1..45\n"; my $i = 1; for (qw(Object TiedHash)) { if (/Object/) { $h = new Tie::SecretHash } if (/Tied/) { $h = {}; tie %$h, Tie::SecretHash } $h->{__password} = $v{password}; $h->{__cipher} = $v{cipher}; print qq{($_) STORE/FETCH to/from unencrypted field: \n}; $h->{plain} = $v{plaintext}; print $h->{plain} eq $v{plaintext} ? "ok $i\n" : "not ok $i\n"; $i++; print qq{($_) STORE/FETCH to/from encrypted field:\n}; $h->{_encrypted} = $v{plaintext}; print $h->{_encrypted} eq $v{plaintext} ? "ok $i\n" : "not ok $i\n"; $i++; print qq{($_) STORE/FETCH with incorrect password:\n}; $h->{__password} = $v{password_wrong}; print $h->{_encrypted} eq $v{plaintext} ? "not ok $i\n" : "ok $i\n"; $i++; print qq{($_) Recover from password change:\n}; $h->{__password} = $v{password}; print $h->{_encrypted} eq $v{plaintext} ? "ok $i\n" : "not ok $i\n"; $i++; print qq{($_) EXISTS unencrypted field:\n}; print exists $h->{plain} ? "ok $i\n" : "not ok $i\n"; $i++; print qq{($_) !EXISTS unencrypted field:\n}; print exists $$h{plain2} ? "not ok $i\n" : "ok $i\n"; $i++; print qq{($_) EXISTS encrypted field:\n}; print exists $h->{_encrypted} ? "ok $i\n" : "not ok $i\n"; $i++; print qq{($_) !EXISTS encrypted field:\n}; print exists $$h{_encrypted2} ? "not ok $i\n" : "ok $i\n"; $i++; print qq{($_) EXISTS encrypted field (incorrect password):\n}; $h->{__password} = $v{password_wrong}; print exists $h->{_encrypted} ? "ok $i\n" : "not ok $i\n"; print Dumper $h; $i++; print qq{($_) EXISTS encrypted field (incorrect password + hide):\n}; $h->{__password} = $v{password_wrong}; $h->{__hide} = "yes"; print exists $h->{_encrypted} ? "no ok $i\n" : "ok $i\n"; $i++; $h->{__hide} = "no"; print qq{($_) DELETE plaintext field:\n}; delete $$h{plain}; print exists $h->{plain} ? "no ok $i\n" : "ok $i\n"; $i++; print qq{($_) DELETE encrypted field:\n}; $h->{__password} = $v{password}; delete $$h{_encrypted}; print exists $h->{_encrypted} ? "no ok $i\n" : "ok $i\n"; $i++; print qq{($_) FIRSTKEY/NEXTKEY:\n}; $$h{plain} = $v{plaintext}; $$h{clone} = $v{plaintext}; $$h{_encrypted} = $v{plaintext}; @keys = keys %$h; print "@keys\n"; print $#keys == 2 ? "ok $i\n" : "no ok $i\n"; $i++; print qq{($_) CLEAR Hash with incorrect password:\n}; $$h{plain} = $v{plaintext}; $$h{_encrypted} = $v{plaintext}; $$h{__password} = $v{password_wrong}; %$h = (); print exists $h->{plain} ? "ok $i\n" : "no ok $i\n"; $i++; print qq{($_) CLEAR Hash with correct password:\n}; $$h{__password} = $v{password}; $$h{plain} = $v{plaintext}; $$h{_encrypted} = $v{plaintext}; %$h = (); print exists $h->{plain} ? "no ok $i\n" : "ok $i\n"; $i++; undef $h; } my %h = (); tie %h, Tie::SecretHash, 'Blacksun', 'Blowfish'; print "STORE/FETCH explicit reference:\n"; $h{_bamf} = {first => 1.16, second => 0.99 , third => { a => 'b' } }; $h{_age} = 12; print $h{_bamf}->{third}->{a} eq "b" ? "ok 31\n" : "not ok 31\n"; print $h{_bamf}; print "FETCH explicit reference with incorrect password:\n"; $h{__password} = 10; print "$h{_bamf}\n"; print $h{_bamf} =~ /^Blowfish/ ? "ok 32\n" : "not ok 32\n"; print "Autovivification:\n"; $h{_foo }->{bar}->{cow} = "baw"; print $h{_foo}->{bar}{cow} eq "baw" ? "ok 33\n" : "not ok 33\n"; $h{__password} = "Hownow?"; print "Autovivification with incorrect password:\n"; print $h{_foo}->{bar}{cow} eq "baw" ? "not ok 34\n" : "ok 34\n"; print "Encryption and serialization of implicit references at FETCH:\n"; $h{__password} = 17; $h{_vivi}->{x} = 4; $h{__password} = 16; print $h{_vivi} =~ /^Blowfish/ ? "ok 35\n" : "not ok 35\n"; # checks if autovivification with incorrect password has clobbered our # hash. print "Recovery from password change:\n"; $h{__password} = 10; $h{_boo}->{bam} = 12; print $h{_boo}{bam} eq 12 ? "ok 36\n" : "not ok 36\n"; print "More Autovivification:\n"; $h{_bar}{a} = "x"; $h{_bar}{b} = "y"; $h{_bar}{d} = "y"; $h{_bar}{e} = { u => 'y' }; print $h{_bar}{e}{u} eq 'y' ? "ok 37\n" : "not ok 37\n"; $h{__password} = "xx"; print $h{_bar} =~ /^Blowfish/ ? "ok 38\n" : "not ok 38\n"; $h{__password} = "Blacksun"; print "STORE/FETCH encrypted listref:\n"; $h{_list}->[4] = 15; print $h{_list}->[4] == 15 ? "ok 39\n" : "not ok 39\n"; $h{_linked}{list1}{list2}[6] = 15; print $h{_linked}{list1}{list2}[6] == 15 ? "ok 40\n" : "not ok 40\n"; print "FETCH encrypted listref with incorrect password:\n"; $h{__password} = 24; print $h{_linked}{list1}{list2}[6] == 15 ? "not ok 41\n" : "ok 41\n"; print $h{_linked} =~ /^Blowfish/ ? "ok 42\n" : "not ok 42\n"; print "STORE/FETCH encrypted scalarref:\n"; $h{__password} = "Blacksun"; $h{_scalar} = \"string"; print ${$h{_scalar}} eq "string" ? "ok 43\n" : "not ok 43\n"; print "STORE/FETCH encrypted scalarref with incorrect password:\n"; $h{__password} = 12; print ${$h{_scalar}} eq "string" ? "not ok 44\n" : "ok 44\n"; print $h{_scalar} =~ /^Blowfish/ ? "ok 45\n" : "not ok 45"; Tie-EncryptedHash-1.24/t/opertations.t 0000644 0000764 0000764 00000006676 07153504116 017030 0 ustar vipul vipul #!/usr/bin/perl -s ## ## Copyright (c) 2000, Vipul Ved Prakash. All rights reserved. ## This code is free software; you can redistribute it and/or modify ## it under the same terms as Perl itself. ## ## $Id: opertations.t,v 1.1 2000/08/31 16:31:10 vipul Exp vipul $ use lib 'lib'; use lib '../lib'; use lib '/home/vipul/PERL/crypto/secrethash/lib'; use Tie::EncryptedHash; use Crypt::Blowfish; use Data::Dumper; %v = ( password => 'Doorknob', password_wrong => 'Bweoo', cipher => 'Blowfish', plaintext => 'Mirrorshades.', ); print "1..30\n"; my $i = 1; for (qw(Object TiedHash)) { if (/Object/) { $h = new Tie::EncryptedHash } if (/Tied/) { $h = {}; tie %$h, Tie::EncryptedHash } $h->{__password} = $v{password}; $h->{__cipher} = $v{cipher}; print qq{($_) STORE/FETCH to/from unencrypted field: \n}; $h->{plain} = $v{plaintext}; print $h->{plain} eq $v{plaintext} ? "ok $i\n" : "not ok $i\n"; $i++; print qq{($_) STORE/FETCH to/from encrypted field:\n}; $h->{_encrypted} = $v{plaintext}; print $h->{_encrypted} eq $v{plaintext} ? "ok $i\n" : "not ok $i\n"; $i++; print qq{($_) STORE/FETCH with incorrect password:\n}; $h->{__password} = $v{password_wrong}; print $h->{_encrypted} eq $v{plaintext} ? "not ok $i\n" : "ok $i\n"; $i++; print qq{($_) Recover from password change:\n}; $h->{__password} = $v{password}; print $h->{_encrypted} eq $v{plaintext} ? "ok $i\n" : "not ok $i\n"; $i++; print qq{($_) EXISTS unencrypted field:\n}; print exists $h->{plain} ? "ok $i\n" : "not ok $i\n"; $i++; print qq{($_) !EXISTS unencrypted field:\n}; print exists $$h{plain2} ? "not ok $i\n" : "ok $i\n"; $i++; print qq{($_) EXISTS encrypted field:\n}; print exists $h->{_encrypted} ? "ok $i\n" : "not ok $i\n"; $i++; print qq{($_) !EXISTS encrypted field:\n}; print exists $$h{_encrypted2} ? "not ok $i\n" : "ok $i\n"; $i++; print qq{($_) EXISTS encrypted field (incorrect password):\n}; $h->{__password} = $v{password_wrong}; print exists $h->{_encrypted} ? "ok $i\n" : "not ok $i\n"; print Dumper $h; $i++; print qq{($_) EXISTS encrypted field (incorrect password + hide):\n}; $h->{__password} = $v{password_wrong}; $h->{__hide} = "yes"; print exists $h->{_encrypted} ? "no ok $i\n" : "ok $i\n"; $i++; $h->{__hide} = "no"; print qq{($_) DELETE plaintext field:\n}; delete $$h{plain}; print exists $h->{plain} ? "no ok $i\n" : "ok $i\n"; $i++; print qq{($_) DELETE encrypted field:\n}; $h->{__password} = $v{password}; delete $$h{_encrypted}; print exists $h->{_encrypted} ? "no ok $i\n" : "ok $i\n"; $i++; print qq{($_) FIRSTKEY/NEXTKEY:\n}; $$h{plain} = $v{plaintext}; $$h{clone} = $v{plaintext}; $$h{_encrypted} = $v{plaintext}; @keys = keys %$h; print "@keys\n"; print $#keys == 2 ? "ok $i\n" : "no ok $i\n"; $i++; print qq{($_) CLEAR Hash with incorrect password:\n}; $$h{plain} = $v{plaintext}; $$h{_encrypted} = $v{plaintext}; $$h{__password} = $v{password_wrong}; %$h = (); print exists $h->{plain} ? "ok $i\n" : "no ok $i\n"; $i++; print qq{($_) CLEAR Hash with correct password:\n}; $$h{__password} = $v{password}; $$h{plain} = $v{plaintext}; $$h{_encrypted} = $v{plaintext}; %$h = (); print exists $h->{plain} ? "no ok $i\n" : "ok $i\n"; $i++; undef $h; } Tie-EncryptedHash-1.24/t/assignopaque.t 0000644 0000764 0000764 00000001153 07153504144 017142 0 ustar vipul vipul #!/usr/bin/perl -s ## ## Copyright (c) 2000, Vipul Ved Prakash. All rights reserved. ## This code is free software; you can redistribute it and/or modify ## it under the same terms as Perl itself. ## ## $Id: assignopaque.t,v 1.1 2000/08/31 16:31:00 vipul Exp vipul $ use lib '../lib'; use lib 'lib'; use Tie::EncryptedHash; print "1..1\n"; my %h = (); tie %h, Tie::EncryptedHash, 'Blacksun', 'Blowfish'; $h{_enc} = "hush hush"; delete $h{__password}; $u = $h{_enc}; my %g = (); tie %g, Tie::EncryptedHash; $g{_secret} = $u; $g{__password} = 'Blacksun'; print $g{_secret} =~ /hush hush/ ? "ok 1\n" : "not ok 1\n"; Tie-EncryptedHash-1.24/TODO 0000644 0000764 0000764 00000001117 07153521331 014500 0 ustar vipul vipul [done] test list ref assignments. [done] test scalar ref assignments. [done] undef Tie::Secret::VARn after decrypt_hex in decrypt. [done] delete $self->{__automatic}{$key} after first access at encrypt at FETCH. [done] should not croak when plaintext is assigned in opaque mode. should ignore. [done] write an empty subclass test. [done] merge __automatic under __scaffolding. [done] ciphertext assignments to an encryting fields in opaque mode should be possible. (check to see if the input contains a MD5 digest). add salt (random data) to value before encrypting and taking MD5. Tie-EncryptedHash-1.24/LICENSE 0000644 0000764 0000764 00000000103 11037167033 015010 0 ustar vipul vipul This module is distributed under the same license as Perl itself. Tie-EncryptedHash-1.24/Makefile.PL 0000644 0000764 0000764 00000000406 11037167056 015770 0 ustar vipul vipul use ExtUtils::MakeMaker; WriteMakefile( NAME => 'Tie::EncryptedHash', VERSION => '1.24', PREREQ_PM => { 'Crypt::CBC' => 0, 'Crypt::Blowfish' => 0, 'Crypt::DES' => 0, } ); Tie-EncryptedHash-1.24/README.html 0000644 0000764 0000764 00000030103 11037166714 015636 0 ustar vipul vipul
Tie::EncryptedHash - Hashes (and objects based on hashes) with encrypting fields.
use Tie::EncryptedHash;
my %s = (); tie %s, Tie::EncryptedHash, 'passwd';
$s{foo} = "plaintext"; # Normal field, stored in plaintext. print $s{foo}; # (plaintext) $s{_bar} = "signature"; # Fieldnames that begin in single # underscore are encrypted. print $s{_bar}; # (signature) Though, while the password # is set, they behave like normal fields. delete $s{__password}; # Delete password to disable access # to encrypting fields. print $s{_bar}; # (Blowfish NuRVFIr8UCAJu5AWY0w...)
$s{__password} = 'passwd'; # Restore password to gain access. print $s{_bar}; # (signature) $s{_baz}{a}{b} = 42; # Refs are fine, we encrypt them too.
Tie::EncryptedHash augments Perl hash semantics to build secure, encrypting
containers of data. Tie::EncryptedHash introduces special hash fields that
are coupled with encrypt/decrypt routines to encrypt assignments at STORE()
and decrypt retrievals at FETCH(). By design, encrypting fields are
associated with keys that begin in single underscore. The remaining
keyspace is used for accessing normal hash fields, which are retained
without modification.
While the password is set, a Tie::EncryptedHash behaves exactly like a standard Perl hash. This is its transparent mode of access. Encrypting and normal fields are identical in this mode. When password is deleted, encrypting fields are accessible only as ciphertext. This is Tie::EncryptedHash's opaque mode of access, optimized for serialization.
Encryption is done with Crypt::CBC(3) which encrypts in the cipher block chaining mode with Blowfish, DES or IDEA. Tie::EncryptedHash uses Blowfish by default, but can be instructed to employ any cipher supported by Crypt::CBC(3).
Tie::EncryptedHash was designed for storage and communication of key material used in public key cryptography algorithms. I abstracted out the mechanism for encrypting selected fields of a structured data record because of the sheer convenience of this data security method.
Quite often, applications that require data confidentiality eschew strong cryptography in favor of OS-based access control mechanisms because of the additional costs of cryptography integration. Besides cipher implementations, which are available as ready-to-deploy perl modules, use of cryptography in an application requires code to aid conversion and representation of encrypted data. This code is usually encapsulated in a data access layer that manages encryption, decryption, access control and re-structuring of flat plaintext according to a data model. Tie::EncryptedHash provides these functions under the disguise of a Perl hash so perl applications can use strong cryptography without the cost of implementing a complex data access layer.
tie %h, Tie::EncryptedHash, 'Password', 'Cipher';
Ties %h to Tie::EncryptedHash and sets the value of password and cipher to 'Password' and 'Cipher'. Both arguments are optional.
$h = new Tie::EncryptedHash __password =
'Password',
__cipher => 'Cipher';>
The new()
constructor returns an object that is both tied and blessed into
Tie::EncryptedHash. Both arguments are optional. When used in this manner,
Tie::EncryptedHash behaves like a class with encrypting data members.
The attributes __password, __cipher and __hide are reserved for communication with object methods. They are ``write-only'' from everywhere except the class to which the hash is tied. __scaffolding is inaccessible. Tie::EncryptedHash stores the current encryption password and some transient data structures in these fields and restricts access to them on need-to-know basis.
$h{__password} = "new password";
delete $h{__password};
The password is stored under the attribute __password
. In addition to
specifying a password at construction, assigning to the __password attribute
sets the current encryption password to the assigned value. Deleting the
__password unsets it and switches the hash into opaque mode.
$h{__cipher} = 'DES'; $h{__cipher} = 'Blowfish';
The cipher used for encryption/decryption is stored under the attribute __cipher. The value defaults to 'Blowfish'.
$h{__hide} = 1;
Setting this attribute hides encrypting fields in opaque mode. 'undef'
is returned at FETCH()
and EXISTS().
A reference stored in an encrypting field is serialized before encryption. The data structure represented by the reference is folded into a single line of ciphertext which is stored under the first level key. In the opaque mode, therefore, only the first level of keys of the hash will be visible.
The opaque mode introduces several other constraints on access of encrypting
fields. Encrypting fields return ciphertext on FETCH()
unless __hide
attribute is set, which forces Tie::EncryptedHash to behave as if encrypting
fields don't exist. Irrespective of __hide, however, DELETE()
and CLEAR()
fail in opaque mode. So does STORE()
on an existing encrypting field.
Plaintext assignments to encrypting fields are silently ignored, but
ciphertext assignments are fine. Ciphertext assignments can be used to move
data between different EncryptedHashes.
Modality of Tie::EncryptedHash's access system breaks down when more than one password is used to with different encrypting fields. This is a feature. Tie::EncryptedHash lets you mix passwords and ciphers in the same hash. Assign new values to __password and __cipher and create a new encrypting field. Transparent mode will be restricted to fields encrypted with the current password.
Tie::Encrypted silently ignores access errors. It doesn't carp/croak when you perform an illegal operation (like assign plaintext to an encrypting field in opaque mode). This is to prevent data lossage, the kind that results from abnormal termination of applications.
Due to the nature of autovivified references (which spring into existence when an undefined reference is dereferenced), references are stored as plaintext in transparent mode. Analogous ciphertext representations are maintained in parallel and restored to encrypting fields when password is deleted. This process is completely transparent to the user, though it's advisable to delete the password after the final assignment to a Tie::EncryptedHash. This ensures plaintext representations and scaffolding data structures are duly flushed.
Serialization of references is done with Data::Dumper, therefore the nature of data that can be assigned to encrypting fields is limited by what Data::Dumper can grok. We set $Data::Dumper::Purity = 1, so self-referential and recursive structures should be OK.
Tie::EncryptedHash'es keep their contents encrypted as much as possible, so
there's a rather severe speed penalty. With Blowfish, STORE()
on
EncryptedHash can be upto 70 times slower than a standard perl hash.
Reference STORE()'es will be quicker, but speed gain will be adjusted at
FETCH(). FETCH()
is about 35 times slower than a standard perl hash. DES
affords speed improvements of upto 2x, but is not considered secure for
long-term storage of data. These values were computed on a DELL PIII-300
Mhz notebook with 128 Mb RAM running perl 5.003 on Linux 2.2.16. Variations
in speed might be different on your machine.
The standard usage for this module would be something along the lines of: populate Tie::EncryptedHash with sensitive data, delete the password, serialize the encrypted hash with Data::Dumper, store the result on disk or send it over the wire to another machine. Later, when the sensitive data is required, procure the EncryptedHash, set the password and accesses the encrypted data fields.
Data::Dumper(3), Crypt::CBC(3), Crypt::DES(3), Crypt::Blowfish(3), Tie::SecureHash(3)
The framework of Tie::EncryptedHash derives heavily from Damian Conway's Tie::SecureHash. Objects that are blessed as well as tied are just one of the pleasant side-effects of stealing Damian's code. Thanks to Damian for this brilliant module.
PacificNet (http://www.pacificnet.net) loaned me the aforementioned notebook to hack from the comfort of my bed. Thanks folks!
Vipul Ved Prakash <mail@vipul.net>
This module is distributed under the same license as Perl itself.
Tie-EncryptedHash-1.24/MANIFEST 0000644 0000764 0000764 00000000377 11037167600 015151 0 ustar vipul vipul Makefile.PL lib/Tie/EncryptedHash.pm MANIFEST LICENSE TODO README.html Changes t/assignopaque.t t/construction.t t/emptysubclass.t t/hide.t t/opertations.t t/ref.t t/reforgy.t META.yml Module meta-data (added by MakeMaker) Tie-EncryptedHash-1.24/Changes 0000644 0000764 0000764 00000000640 11037167014 015303 0 ustar vipul vipul 1.24 (Tue Jul 15 14:35:35 EDT 2008) - Remove references to Artistic License from README. 1.23 (Tue Jul 15 05:18:37 EDT 2008) - Applied patch from ANDK@cpan.org to avoid failures in reforgy.t [http://rt.cpan.org/Ticket/Display.html?id=27585] - Turned off warnings in the test suite. It is supposed to generate warnings but it freaks out people. - License changed to Artistic 2.0 | GPL for Fedora folks. Tie-EncryptedHash-1.24/lib/ 0000755 0000764 0000764 00000000000 11037167600 014557 5 ustar vipul vipul Tie-EncryptedHash-1.24/lib/Tie/ 0000755 0000764 0000764 00000000000 11037167600 015300 5 ustar vipul vipul Tie-EncryptedHash-1.24/lib/Tie/EncryptedHash.pm 0000644 0000764 0000764 00000036620 11037064411 020402 0 ustar vipul vipul #!/usr/bin/perl -s ## ## Tie::EncryptedHash - A tied hash with encrypted fields. ## ## Copyright (c) 2000, Vipul Ved Prakash. All rights reserved. ## This code is based on Damian Conway's Tie::SecureHash. ## ## $Id: EncryptedHash.pm,v 1.8 2000/09/02 19:23:00 vipul Exp vipul $ ## vi:expandtab=1;ts=4; package Tie::EncryptedHash; use strict; use vars qw($VERSION $strict); use Digest::MD5 qw(md5_base64); use Crypt::CBC; use Data::Dumper; use Carp; ( $VERSION ) = '$Revision: 1.8 $' =~ /\s(\d+\.\d+)\s/; my $DEBUG = 0; sub debug { return undef unless $DEBUG; my ($caller, undef) = caller; my (undef,undef,$line,$sub) = caller(1); $sub =~ s/.*://; $sub = sprintf "%10s()%4d",$sub,$line; print "$sub " . (shift) . "\n"; } # sub new { # my ($class,%args) = @_; # my %self = (); tie %self, $class; # my $self = bless \%self, $class; # $self->{__password} = $args{__password} if $args{__password}; # $self->{__cipher} = $args{__cipher} || qq{Blowfish}; # return $self; # } sub new { my ($class,%args) = @_; my $self = {}; tie %$self, $class; bless $self, $class; $self->{__password} = $args{__password} if $args{__password}; $self->{__cipher} = $args{__cipher} || qq{Blowfish}; return $self; } sub _access { my ($self, $key, $caller, $file, $value, $delete) = @_; my $class = ref $self || $self; # SPECIAL ATTRIBUTE if ( $key =~ /(__password|__hide|__scaffolding|__cipher)$/ ) { my $key = $1; unless($value||$delete) {return undef unless $caller eq $class} if ($delete && ($key =~ /__password/)) { for (keys %{$$self{__scaffolding}}) { if ( ref $self->{$_} ) { $self->{$_} = encrypt($self->{$_}, $self->{__scaffolding}{$_}, $self->{__cipher}); delete $self->{__scaffolding}{$_}; } } } delete $$self{$key} if $delete; return $self->{$key} = $value if $value; return $self->{$key}; # SECRET FIELD } elsif ( $key =~ m/^(_{1}[^_][^:]*)$/ ||$key =~ m/.*?::(_{1}[^_][^:]*)/ ) { my $ctext = $self->{$1}; if ( ref $ctext && !($value)) { # ENCRYPT REF AT FETCH my $pass = $self->{__scaffolding}{$1} || $self->{__password}; return undef unless $pass; $self->{$1} = encrypt($ctext, $pass, $self->{__cipher}); return $self->FETCH ($1); } my $ptext = qq{}; my $isnot = !( exists $self->{$1} ); my $auth = verify($self,$1); return undef if !($auth) && ref $self->{$1}; return undef if !($auth) && $self->{__hide}; if ($auth && $auth ne "1") { $ptext = $auth } if ($value && $auth) { # STORE if ( ref $value ) { $self->{__scaffolding}{$1} = $self->{__password}; $ctext = $value; } else { my $key = $1; unless ($self->{__password}) { if ($value =~ m:^\S+\s\S{22}\s:) { return $self->{$key} = $value; } else { return undef } } $ctext = encrypt($value, $self->{__password}, $self->{__cipher}); } $self->{$1} = $ctext; return $value; } elsif ($auth && $delete) { # DELETE delete $$self{$1} } elsif ($isnot && (!($value))) { # DOESN'T EXIST return; } elsif ((!($auth)) && $ctext) { return $ctext; # FETCH return ciphertext } elsif ($auth && !($isnot)) { # FETCH return plaintext if (ref $ptext) { $self->{$1} = $ptext; $self->{__scaffolding}{$1} = $self->{__password}; # Ref counting mechanism return $self->{$1}; } } return undef unless $auth; return $ptext; # PUBLIC FIELD } elsif ( $key =~ m/([^:]*)$/ || $key =~ m/.*?::([^:]*)/ ) { $self->{$1} = $value if $value; delete $$self{$1} if $delete; return $self->{$1} if $self->{$1}; return undef; } } sub encrypt { # ($plaintext, $password, $cipher) $_[0] = qq{REF }. Data::Dumper->new([$_[0]])->Indent(0)->Terse(0)->Purity(1)->Dumpxs if ref $_[0]; return qq{$_[2] } . md5_base64($_[0]) .qq{ } . Crypt::CBC->new($_[1],$_[2])->encrypt_hex($_[0]) } sub decrypt { # ($cipher $md5sum $ciphertext, $password) return undef unless $_[1]; my ($m, $d, $c) = split /\s/,$_[0]; my $ptext = Crypt::CBC->new($_[1],$m)->decrypt_hex($c); my $check = md5_base64($ptext); if ( $d eq $check ) { if ($ptext =~ /^REF (.*)/is) { my ($VAR1,$VAR2,$VAR3,$VAR4,$VAR5,$VAR6,$VAR7,$VAR8); return eval qq{$1}; } return $ptext; } } sub verify { # ($self, $key) my ($self, $key) = splice @_,0,2; # debug ("$self->{__scaffolding}{$key}, $self->{__password}, $self->{$key}"); return 1 unless $key =~ m:^_:; return 1 unless exists $self->{$key}; return undef if ref $self->{$key} && ($self->{__scaffolding}{$key} ne $self->{__password}); my $ptext = decrypt($self->{$key}, $self->{__password}); return $ptext if $ptext; } sub each { CORE::each %{$_[0]} } sub keys { CORE::keys %{$_[0]} } sub values { CORE::values %{$_[0]} } sub exists { CORE::exists $_[0]->{$_[1]} } sub TIEHASH # ($class, @args) { my $class = ref($_[0]) || $_[0]; my $self = bless {}, $class; $self->{__password} = $_[1] if $_[1]; $self->{__cipher} = $_[2] || qq{Blowfish}; return $self; } sub FETCH # ($self, $key) { my ($self, $key) = @_; my $entry = _access($self,$key,(caller)[0..1]); return $entry if $entry; } sub STORE # ($self, $key, $value) { my ($self, $key, $value) = @_; my $entry = _access($self,$key,(caller)[0..1],$value); return $entry if $entry; } sub DELETE # ($self, $key) { my ($self, $key) = @_; return _access($self,$key,(caller)[0..1],'',1); } sub CLEAR # ($self) { my ($self) = @_; return undef if grep { ! $self->verify($_) } grep { ! /__/ } CORE::keys %{$self}; %{$self} = (); } sub EXISTS # ($self, $key) { my ($self, $key) = @_; my @context = (caller)[0..1]; return _access($self,$key,@context) ? 1 : ''; } sub FIRSTKEY # ($self) { my ($self) = @_; CORE::keys %{$self}; goto &NEXTKEY; } sub NEXTKEY # ($self) { my $self = $_[0]; my $key; my @context = (caller)[0..1]; while (defined($key = CORE::each %{$self})) { last if eval { _access($self,$key,@context) } } return $key; } sub DESTROY # ($self) { } 1; __END__ =head1 NAME Tie::EncryptedHash - Hashes (and objects based on hashes) with encrypting fields. =head1 SYNOPSIS use Tie::EncryptedHash; my %s = (); tie %s, Tie::EncryptedHash, 'passwd'; $s{foo} = "plaintext"; # Normal field, stored in plaintext. print $s{foo}; # (plaintext) $s{_bar} = "signature"; # Fieldnames that begin in single # underscore are encrypted. print $s{_bar}; # (signature) Though, while the password # is set, they behave like normal fields. delete $s{__password}; # Delete password to disable access # to encrypting fields. print $s{_bar}; # (Blowfish NuRVFIr8UCAJu5AWY0w...) $s{__password} = 'passwd'; # Restore password to gain access. print $s{_bar}; # (signature) $s{_baz}{a}{b} = 42; # Refs are fine, we encrypt them too. =head1 DESCRIPTION Tie::EncryptedHash augments Perl hash semantics to build secure, encrypting containers of data. Tie::EncryptedHash introduces special hash fields that are coupled with encrypt/decrypt routines to encrypt assignments at STORE() and decrypt retrievals at FETCH(). By design, I