This node How to Make Crypt::DSA use a your key to sign something got me thinking about how to extend Crypt::DSA's potential, to make public and private keys, for signing verification. The script below, will verify 2 ways, once with scalars inside the script, and once from files. The generated sig, is actually binary, so note the base64 encoding of it, for attachment to emails, etc. I must thank sisyphus for the trick of reblessing a hash back into an object. This is the critical part of the script, where the public and private keys are generated. The docs for Crypt::DSA don't show how to separate them,( and you wouldn't want to be distributing your private key in the public PEM file. :-)
#!/usr/bin/perl use warnings; use strict; use Crypt::DSA; use Crypt::DSA::Key; use MIME::Base64 qw( encode_base64 decode_base64 ); my $dsa = Crypt::DSA->new; # use to generate your keys below # my $key = $dsa->keygen(Size => 512, Verbosity => 1); # print ref $key; foreach my $k (keys %{$key}) { # print $k . "=" . $key->$k . " "; # } # hard code in the above generated numbers. my %key_priv; $key_priv{'priv_key'} = '86493656493674673971107878679102456068174181 +0216'; $key_priv{'p'} = '103389578900933505962259092511631192256092079077336 +779855585334041474848850089585441001014183922540716749810264398731049 +49464720186311018367111795638927603'; $key_priv{'g'} = '295468312836227549163017971559304985039133262378345 +815627315465892672058916494919915988882646491840732863667119568864913 +0131612326212886632087281832722137'; $key_priv{'q'} = '1001831165198829652697436093010198645746872403487'; + # $key_priv{'pub_key'} = ''; #don't include in private key my %key_pub; # $key_pub{'priv_key'} = ''; #don't include in public key $key_pub{'p'} = $key_priv{'p'}; $key_pub{'g'} = $key_priv{'g'}; $key_pub{'q'} = $key_priv{'q'}; $key_pub{'pub_key'} = '1323460927720994618070448727574663888688993575 +715081267508071818636049670044713302408747578850296432590020771929699 +912452512506924349344334420749206004502'; my $msg = "xxxxxxxxxxxxxxxxxx\n" x 40; open(FH,">msg") or die "$!\n"; print FH $msg; close FH; my $k_priv = \%key_priv; bless($k_priv, "Crypt::DSA::Key"); my $sig = $dsa->sign(Message=>$msg, Key => $k_priv); my $sig_ser = $sig->serialize; open(FH,">sig") or die "$!\n"; print FH encode_base64($sig_ser); #need base64 encoding close FH; #of binary sig my $pem_priv = $k_priv->write( Type => 'PEM' ); open(FH,">key_priv") or die "$!\n"; print FH $pem_priv; close FH; ############################################### #verify with pub_key from internal variables my $k_pub = \%key_pub; bless($k_pub, "Crypt::DSA::Key"); my $pem_pub = $k_pub->write( Type => 'PEM' ); open(FH,">key_pub") or die "$!\n"; print FH $pem_pub; close FH; my $verified = $dsa->verify( Message => $msg, Signature => $sig, Key => $k_pub, ); print "verified internally->$verified\n"; ############################################# #verify with pub_key from files my $msg_in; open (FH,"< msg"); read( FH, $msg_in, -s FH ); close FH; my $sig_in; open (FH,"< sig"); read( FH, $sig_in, -s FH ); close FH; my $dsa1 = Crypt::DSA->new; my $key_pub = Crypt::DSA::Key->new( Type => 'PEM', Filename => 'key_pub'); my $sig_o = Crypt::DSA::Signature->new( Content => decode_base64( $sig +_in ) ); my $verified1 = $dsa1->verify( Message => $msg_in, Signature => $sig_o, Key => $key_pub, ); print "verified thru files->$verified1\n"; #############################################

I'm not really a human, but I play one on earth. flash japh

Replies are listed 'Best First'.
Re: Roll your DSA signatures
by zentara (Cardinal) on Feb 24, 2006 at 21:53 UTC
    The script above was for demonstrating the basics. Here are a set of scripts, derived from the above, to do it in a more useful manner...... gen_keys, sign-a-file,verify-a-file,check-key

    I'm not really a human, but I play one on earth. flash japh