<?xml version="1.0" encoding="windows-1252"?>
<node id="308" title="perlfunc:crypt" created="1999-08-24 18:43:08" updated="2005-08-14 11:24:20">
<type id="119">
perlfunc</type>
<author id="114">
gods</author>
<data>
<field name="doctext">
</field>
<field name="name">

&lt;P&gt;
crypt - one-way passwd-style encryption

&lt;P&gt;
&lt;HR&gt;
</field>
<field name="synopsis">

&lt;P&gt;
crypt 
&lt;FONT SIZE=-1&gt;PLAINTEXT,SALT&lt;/FONT&gt;

&lt;P&gt;
&lt;HR&gt;
</field>
<field name="description">

&lt;P&gt;
Encrypts a string exactly like the 
&lt;CODE&gt;crypt(3)&lt;/CODE&gt; function in the 
&lt;FONT SIZE=-1&gt;C&lt;/FONT&gt; library (assuming that you actually have a version there that has not been extirpated as a potential munition). This can prove useful for checking the password file for lousy passwords, amongst other things. Only the guys wearing white hats should do this.

&lt;P&gt;
Note that [perlfunc:crypt|crypt()] is intended to be a one-way function, much like breaking eggs to make an omelette. There is no (known) corresponding decrypt function. As a result, this function isn't all that useful for cryptography. (For that, see your nearby 
&lt;FONT SIZE=-1&gt;CPAN&lt;/FONT&gt; mirror.)

&lt;P&gt;
Here's an example that makes sure that whoever runs this program knows
their own password:

&lt;P&gt;
&lt;PRE&gt;    $pwd = (getpwuid($&amp;lt;))&amp;#091;1&amp;#093;;
    $salt = substr($pwd, 0, 2);
&lt;/PRE&gt;
&lt;P&gt;
&lt;PRE&gt;    system &amp;quot;stty -echo&amp;quot;;
    print &amp;quot;Password: &amp;quot;;
    chop($word = &amp;lt;STDIN&amp;gt;);
    print &amp;quot;\n&amp;quot;;
    system &amp;quot;stty echo&amp;quot;;
&lt;/PRE&gt;
&lt;P&gt;
&lt;PRE&gt;    if (crypt($word, $salt) ne $pwd) {
        die &amp;quot;Sorry...\n&amp;quot;;
    } else {
        print &amp;quot;ok\n&amp;quot;;
    }
&lt;/PRE&gt;
&lt;P&gt;
Of course, typing in your own password to whoever asks you for it is
unwise.

&lt;HR&gt;
</field>
</data>
</node>
