#!/usr/bin/perl -w

use Crypt::OpenSSL::Random;
use Crypt::OpenSSL::RSA;

  Crypt::OpenSSL::RSA->import_random_seed();

  if(-f "vendor-sec.pem")
  {
    print "Loading existing vendor key ...\n";
    open IN,"<vendor-sec.pem";
    undef $/;
    my $sec_string=<IN>;
    close IN;
    $rsa = Crypt::OpenSSL::RSA->new_private_key($sec_string);

    open IN,"<vendor-pub.pem";
    undef $/;
    my $pub_string=<IN>;
    close IN;
    $rsa_pub = Crypt::OpenSSL::RSA->new_public_key($pub_string);
  }
  else
  {
    print "Generating secret vendor key ...\n";
    $rsa = Crypt::OpenSSL::RSA->generate_key(1024);


    print "private key is:\n", $rsa->get_private_key_string();
    open OUT,">vendor-sec.pem";
    print OUT $rsa->get_private_key_string();
    close OUT;

    print "public key (in PKCS1 format) is:\n", $rsa->get_public_key_string();

    open OUT,">vendor-pub.pem";
    print OUT $rsa->get_public_key_string();
    close OUT;

    print "public key (in X509 format) is:\n", $rsa->get_public_key_x509_string();

    $rsa_pub = Crypt::OpenSSL::RSA->new_public_key($rsa->get_public_key_string());

  }


