API15

JUserHelper/getSalt

From Joomla! Documentation

< API15:JUserHelper

The "API15" namespace is an archived namespace. This page contains information for a Joomla! version which is no longer supported. It exists only as a historical reference, it will not be improved and its content may be incomplete and/or contain broken links.

Description[edit]

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.

[<! removed edit link to red link >]

<! removed transcluded page call, red link never existed >

Syntax[edit]

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[edit]

string The generated or extracted salt.

Defined in[edit]

libraries/joomla/user/helper.php

Importing[edit]

jimport( 'joomla.user.helper' );

Source Body[edit]

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;
        }
}

[<! removed edit link to red link >] <! removed transcluded page call, red link never existed >

Examples[edit]

Code Examples[edit]