<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">Article 3269 of comp.lang.perl:
Xref: feenix.metronet.com comp.lang.perl:3269
Path: feenix.metronet.com!news.ecn.bgu.edu!wupost!cs.utexas.edu!sun-barr!koriel!male.EBay.Sun.COM!jethro.Corp.Sun.COM!eric.arnold@sun.com
From: eric.arnold@sun.com (eric.arnold )
Newsgroups: comp.lang.perl
Subject: Re: Hiding a password?
Date: 8 Jun 1993 20:45:17 GMT
Organization: Sun Microsystems, Inc.
Lines: 59
Distribution: world
Message-ID: &lt;1v2tot$k0l@jethro.Corp.Sun.COM&gt;
References: &lt;rstone.739557635@cunews&gt;
Reply-To: eric.arnold@sun.com
NNTP-Posting-Host: animus.corp.sun.com


I had to to put a password in a script a while ago.  What I did was to
first encrypt the password, and then store the encrypted password in
the script.  When the script ran, it compared the encrypted password
against the user input.

I needed two functions to accomplish this, "&amp;genpasswd" and "&amp;cmppasswd":

  print "input passwd: ";
  while ( &lt;STDIN&gt; )
  {
    if ( &amp;cmppasswd( $passwd, $_ ) ) {
      print "equal to last\n"; }
    else {
      print "not equal to last\n"; }
    print "passwd=", $passwd = &amp;genpasswd( $_ ), "\n";
    print "input passwd: ";
  }
  
  
  sub cmppasswd {
    local( $encrypted, $test ) = @_;
    return ( $encrypted eq crypt( $test, $encrypted ) );
  }
  
  sub genpasswd {
    local( $to_encrypt ) = @_;
    local( $salt_string, $len, $salt );
  
    $salt_string = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.";
    $len = length( $salt_string );
    srand( time() );
    $salt = substr( $salt_string, rand($len), 1 ) . substr( $salt_string, rand($len), 1 );
    return crypt( $to_encrypt, $salt );
  }


If you want to put the password "topsecret" into a readable script, you
would first get the encrypted equivalent of the desired password
string:

  print &amp;genpasswd( "topsecret" ), "\n";

and you might get:

  ab8erO8W0MyYo

from here you can edit it into the script like:

  $inp = &lt;STDIN&gt;;
  if ( &amp;cmppasswd( "ab8erO8W0MyYo", $inp ) ){
    # access granted ... }

I'll leave as extra credit to have the script modify itself
with the new encrypted password.

-Eric




</pre></body></html>