| 910 | | |
| 911 | | =item crypt |
| 912 | | |
| 913 | | [I don't think this should be a built-in anymore. -law] |
| 914 | | |
| 915 | | our Str multi method p5crypt ( Str $plaintext: Str $salt ) is export(:P5) |
| 916 | | |
| 917 | | Encrypts the string using a one-way hash function. This yields a string |
| 918 | | which cannot be "decrypted". This is used for verifying strings such |
| 919 | | as passwords. The C<$plaintext> is the text to be encrypted. The |
| 920 | | C<$salt> is a string which controls how the encryption is done. Typically, |
| 921 | | this is a random string of two characters that matches the following pattern: |
| 922 | | |
| 923 | | token Str::CryptBasicSalt { <[./0-9A-Za-z]> **{2} } |
| 924 | | |
| 925 | | There are other formats of salt as well, though their use is not universal. |
| 926 | | Check your operating system's B<crypt> function for more details. Often |
| 927 | | these other functions are used when strings of length greater than eight |
| 928 | | are to be used (the default crypt mode only recognizes the first eight |
| 929 | | characters of the string as significant). |
| 930 | | |
| 931 | | If you have a previously encrypted string and a plaintext password, you |
| 932 | | can check to see of the password is correct like so: |
| 933 | | |
| 934 | | sub checkpw ( $encrypted, $plaintext ) { |
| 935 | | return crypt($plaintext, $encrypted) ~~ $encrypted; |
| 936 | | } |
| 937 | | |
| 938 | | This works because the first part of the encrypted form of the |
| 939 | | password is the salt, so an encrypted text can be passed in place of |
| 940 | | the salt (everything after the salt is ignored). |