Actions

API15

JUserHelper/getSalt

From Joomla! Documentation

< API15:JUserHelper
Revision as of 17:10, 22 March 2010 by Doxiki (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Replacement filing cabinet.png
This Namespace has been archived - Please Do Not Edit or Create Pages in this namespace. Pages contain information for a Joomla! version which is no longer supported. It exists only as a historical reference, will not be improved and its content may be incomplete.

Contents

Description

Returns a salt for the appropriate kind of password encryption. Optionally takes a seed and a plaintext password, to extract the seed of an existing password, or for encryption types that use the plaintext in the generation of the salt.

[Edit Descripton]

Description:JUserHelper/getSalt

Syntax

getSalt($encryption= 'md5-hex', $seed= '', $plaintext= '')
Parameter Name Default Value Description
$encryption 'md5-hex' $encryption The kind of pasword encryption to use. Defaults to md5-hex.
$seed $seed The seed to get the salt from (probably a previously generated password). Defaults to generating a new seed.
$plaintext $plaintext The plaintext password that we're generating a salt for. Defaults to none.

Returns

string The generated or extracted salt.

Defined in

libraries/joomla/user/helper.php

Importing

jimport( 'joomla.user.helper' );

Source Body

function getSalt($encryption = 'md5-hex', $seed = '', $plaintext = '')
{
        // Encrypt the password.
        switch ($encryption)
        {
                case 'crypt' :
                case 'crypt-des' :
                        if ($seed) {
                                return substr(preg_replace('|^{crypt}|i', '', $seed), 0, 2);
                        } else {
                                return substr(md5(mt_rand()), 0, 2);
                        }
                        break;
 
                case 'crypt-md5' :
                        if ($seed) {
                                return substr(preg_replace('|^{crypt}|i', '', $seed), 0, 12);
                        } else {
                                return '$1$'.substr(md5(mt_rand()), 0, 8).'$';
                        }
                        break;
 
                case 'crypt-blowfish' :
                        if ($seed) {
                                return substr(preg_replace('|^{crypt}|i', '', $seed), 0, 16);
                        } else {
                                return '$2$'.substr(md5(mt_rand()), 0, 12).'$';
                        }
                        break;
 
                case 'ssha' :
                        if ($seed) {
                                return substr(preg_replace('|^{SSHA}|', '', $seed), -20);
                        } else {
                                return mhash_keygen_s2k(MHASH_SHA1, $plaintext, substr(pack('h*', md5(mt_rand())), 0, 8), 4);
                        }
                        break;
 
                case 'smd5' :
                        if ($seed) {
                                return substr(preg_replace('|^{SMD5}|', '', $seed), -16);
                        } else {
                                return mhash_keygen_s2k(MHASH_MD5, $plaintext, substr(pack('h*', md5(mt_rand())), 0, 8), 4);
                        }
                        break;
 
                case 'aprmd5' :
                        /* 64 characters that are valid for APRMD5 passwords. */
                        $APRMD5 = './0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
 
                        if ($seed) {
                                return substr(preg_replace('/^\$apr1\$(.{8}).*/', '\\1', $seed), 0, 8);
                        } else {
                                $salt = '';
                                for ($i = 0; $i < 8; $i ++) {
                                        $salt .= $APRMD5 {
                                                rand(0, 63)
                                                };
                                }
                                return $salt;
                        }
                        break;
 
                default :
                        $salt = '';
                        if ($seed) {
                                $salt = $seed;
                        }
                        return $salt;
                        break;
        }
}

[Edit See Also] SeeAlso:JUserHelper/getSalt

Examples

<CodeExamplesForm />