PHP Doku:: Berechnet den SHA1-Hash eines Strings - function.sha1.html

Verlauf / Chronik / History: (1) anzeigen

Sie sind hier:
Doku-StartseitePHP-HandbuchFunktionsreferenzTextverarbeitungZeichenkettenString-Funktionensha1

Ein Service von Reinhard Neidl - Webprogrammierung.

String-Funktionen

<<sha1_file

similar_text>>

sha1

(PHP 4 >= 4.3.0, PHP 5)

sha1Berechnet den SHA1-Hash eines Strings

Beschreibung

string sha1 ( string $str [, bool $raw_output = false ] )

Berechnet den SHA1 Hash von str unter Verwendung des » US Secure Hash Algorithmus 1.

Parameter-Liste

str

Die Eingabezeichenkette.

raw_output

Ist der optionale Parameter raw_output TRUE, wird der SHA1-Extrakt im Raw-Binary-Format mit einer Länge von 20 Zeichen zurückgegeben. Ansonsten ist der Rückgabewert ein 40 Zeichen langer Hexadezimalwert.

Rückgabewerte

Gibt den SHA1-Hash als Zeichenkette zurück.

Changelog

Version Beschreibung
5.0.0 Der Parameter raw_output wurde hinzugefügt.

Beispiele

Beispiel #1 Ein sha1() Beispiel

<?php
$str 
'Apfel';

if (
sha1($str) === 'df589122eac0f6a7bd8795436e692e3675cadc3b') {
    echo 
"Hätten Sie gern einen grünen oder einen roten Apfel?";
    exit;
}
?>

Siehe auch

  • sha1_file() - Berechnet den SHA1-Hash einer Datei
  • crc32() - Berechnet den polynomischen CRC32-Wert eines Strings
  • md5() - Errechnet den MD5-Hash eines Strings
  • hash() - Berechnet den Hash einer Nachricht


36 BenutzerBeiträge:
- Beiträge aktualisieren...
rich dot sage at gmail dot com
25.11.2010 9:59
If you're using Dovecot for mail retrieval and you want to generate SHA1 passwords yourself, you'll need to set the raw_output value to true, then base64_encode the output:

<?php
function makeDovecotPassword($input)
{
  return
'{SHA}' . base64_encode(sha1($input, true));
}
?>
Anonymous
25.04.2010 3:03
I have created simple functions that allow you to create tokens that expire within a certain amount of time.

Call generateTimedHash with your data to encode and a salt:

<?php
$token
= generateTimedHash("This is data.", "SALTY");
?>

Now you can call verifyTimedHash, with your token, data, salt, and lifetime of the token. The function will return true if the token is correct and within the lifetime of the token. It will return false if the token is incorrect and/or the token has expired. To verify a token that expires 30 seconds after it is created, use this:

<?php
$valid
= verifyTimedHash($token, "This is data.", "SALTY", 30);
?>

The functions:

<?php
function generateTimedHash($data, $salt)
{
   
// Salt the token with a defined salt and the time
   
return sha1($data . $salt . time());   
}

function
verifyTimedHash($token, $data, $salt, $life)
{
   
// Get the current time
   
$time = time();

   
// Verify hash with current time
   
if ($token == sha1($data . $salt . $time))
        return
true;
       
   
// If that didn't work, try verifiying it, each time subtracting 1 from $time, $life times
   
for ($offset = 1; $offset <= $life; $offset++)
        if (
$token == sha1($data . $salt . ($time - $offset)))
            return
true;
   
    return
false;
}
?>
Anonymous
29.10.2009 2:48
Another solution to the salted hash with salt included directly in the hash, while keeping the same length of the result. If you want to generate a hash, call the function without the second argument. If you want to check a password against a hash, use the hash as the second argument. In this case, the function returns the hash itself on success, or boolean false on failure. You can also specify a hash algorithm as the third argument (otherwise SHA-1 will be used).

<?php
function __hash($password, $obscured = NULL, $algorithm = "sha1")
{
 
// whether to use user specified algorithm
 
$mode = in_array($algorithm, hash_algos());
 
// generate random salt
 
$salt = uniqid(mt_rand(), true);
 
// hash it
 
$salt = $mode ? hash($algorithm, $salt) : sha1($salt);
 
// get the length
 
$slen = strlen($salt);
 
// compute the actual length of salt we will use
  // 1/8 to 1/4 of the hash, with shorter passwords producing longer salts
 
$slen = max($slen >> 3, ($slen >> 2) - strlen($password));
 
// if we are checking password against a hash, harvest the actual salt from it, otherwise just cut the salt we already have to the proper size
 
$salt = $obscured ? __harvest($obscured, $slen, $password) : substr($salt, 0, $slen);
 
// hash the password - this is maybe unnecessary
 
$hash = $mode ? hash($algorithm, $password) : sha1($password);
 
// place the salt in it
 
$hash = __scramble($hash, $salt, $password);
 
// and hash it again
 
$hash = $mode ? hash($algorithm, $hash) : sha1($hash);
 
// cut the result so we can add salt and maintain the same length
 
$hash = substr($hash, $slen);
 
// ... do that
 
$hash = __scramble($hash, $salt, $password);
 
// and return the result
 
return $obscured && $obscured !== $hash ? false : $hash;
}
?>

It uses a random, variable length salt, depending on the length of the password. The functions __scramble() and __harvest() are used to place salt into the hash or pull it out respectively. You can write your own, and of course the strength of the result greatly depends on them. They can be relatively simple yet still quite secure:

<?php
function __scramble($hash, $salt, $password)
{
  return
substr($hash, 0, strlen($password)) . $salt . substr($hash, strlen($password));
}

function
__harvest($obscured, $slen, $password)
{
  return
substr($obscured, min(strlen($password), strlen($obscured) - $slen), $slen);
}
?>

Or they can be ridiculously complicated (my favourite kind):

<?php
function __scramble($hash, $salt, $password)
{
 
$k = strlen($password); $j = $k = $k > 0 ? $k : 1; $p = 0; $index = array(); $out = ""; $m = 0;
  for (
$i = 0; $i < strlen($salt); $i++)
  {
   
$c = substr($password, $p, 1);
   
$j = pow($j + ($c !== false ? ord($c) : 0), 2) % (strlen($hash) + strlen($salt));
    while (
array_key_exists($j, $index))
     
$j = ++$j % (strlen($hash) + strlen($salt));
   
$index[$j] = $i;
   
$p = ++$p % $k;
  }
  for (
$i = 0; $i < strlen($hash) + strlen($salt); $i++)
   
$out .= array_key_exists($i, $index) ? $salt[$index[$i]] : $hash[$m++];
  return
$out;
}

function
__harvest($obscured, $slen, $password)
{
 
$k = strlen($password); $j = $k = $k > 0 ? $k : 1; $p = 0; $index = array(); $out = "";
  for (
$i = 0; $i < $slen; $i++)
  {
   
$c = substr($password, $p, 1);
   
$j = pow($j + ($c !== false ? ord($c) : 0), 2) % strlen($obscured);
    while (
in_array($j, $index))
     
$j = ++$j % strlen($obscured);
   
$index[$i] = $j;
   
$p = ++$p % $k;
  }
  for (
$i = 0; $i < $slen; $i++)
   
$out .= $obscured[$index[$i]];
  return
$out;
}
?>
paul
15.04.2009 15:57
I believe this offers best amount of protection using a random salt, that has to be stored so it can be used later for verification.

If no salt is given (which can be retrieved by halving the output and taking the first half), then it will generate a random salt, hash it, place it in a position relative to the length of password (between 0 and length of hash type(sha1? md5?)) within the hashed password, and then hash the complete string.

This results in a password hash using a salt that is dynamically placed dependant on password length. The salt used is then appended to the front of the finished hash so it can be retrieved later on for verifying.

Seeing as users will choose a typical password of between 5 and say 15 characters long, this gives them an extra 10 times the amount of dictionary attacks to try out with the hash as it could be placed in any position, because this is a random generated salt too, it means at least 10 dictionary attacks (with possiblity of upto 40) for each instance a password is created, to try and work out your sha1 encrypted password.

If you change your password say every month, even if someone gets a look in at your file through a local exploit, the amount of time to work out your password would far outweigh the frequency at which you change it.

Nothing is secure, but this should take them longer to work out then the time you change it. That is at least by todays technologies.

Paul

<?php
   
function createHash($inText, $saltHash=NULL, $mode='sha1'){
       
// hash the text //
       
$textHash = hash($mode, $inText);
       
// set where salt will appear in hash //
       
$saltStart = strlen($inText);
       
// if no salt given create random one //
       
if($saltHash == NULL) {
           
$saltHash = hash($mode, uniqid(rand(), true));
        }
       
// add salt into text hash at pass length position and hash it //
       
if($saltStart > 0 && $saltStart < strlen($saltHash)) {
           
$textHashStart = substr($textHash,0,$saltStart);
           
$textHashEnd = substr($textHash,$saltStart,strlen($saltHash));
           
$outHash = hash($mode, $textHashEnd.$saltHash.$textHashStart);
        } elseif(
$saltStart > (strlen($saltHash)-1)) {
           
$outHash = hash($mode, $textHash.$saltHash);
        } else {
           
$outHash = hash($mode, $saltHash.$textHash);
        }
       
// put salt at front of hash //
       
$output = $saltHash.$outHash;
        return
$output;
    }
?>
mVamer
8.01.2009 2:49
If I correctly understand what ranko84 is on about, this would be a simpler function with roughly the same result.

<?php
function obscure($password, $algorythm = "sha1")
{
       
// Get some random salt, or verify a salt.
        // Added by (grosbedo AT gmail DOT com)
       
if ($salt == NULL)
        {
           
$salt = hash($algorythm, uniqid(rand(), true));
        }

   
// Determine the length of the hash.
   
$hash_length = strlen($salt);

   
// Determine the length of the password.
   
$password_length = strlen($password);

   
// Determine the maximum length of password. This is only needed if
    // the user enters a very long password. In any case, the salt will
    // be a maximum of half the end result. The longer the hash, the
    // longer the password/salt can be.
   
$password_max_length = $hash_length / 2;

   
// Shorten the salt based on the length of the password.
   
if ($password_length >= $password_max_length)
    {
       
$salt = substr($salt, 0, $password_max_length);
    }
    else
    {
       
$salt = substr($salt, 0, $password_length);
    }

   
// Determine the length of the salt.
   
$salt_length = strlen($salt);

   
// Determine the salted hashed password.
   
$salted_password = hash($algorythm, $salt . $password);

   
// If we add the salt to the hashed password, we would get a hash that
    // is longer than a normally hashed password. We don't want that; it
    // would give away hints to an attacker. Because the password and the
    // length of the password are known, we can just throw away the first
    // couple of characters of the salted password. That way the salt and
    // the salted password together are the same length as a normally
    // hashed password without salt.
   
$used_chars = ($hash_length - $salt_length) * -1;
   
$final_result = $salt . substr($salted_password, $used_chars);

    return
$final_result;
}
?>
ranko84 at gmail dot com
17.12.2008 5:11
Small update..., well more like fix to the obscure function, replace
<?php
if ($keepLength != NULL)
{
    if (
$hSLength != 0)
    {
       
$hPassHash = substr($hPassHash, $hLPosition, -$hRPosition);
    }
}
?>

with

<?php
if ($keepLength != NULL)
{
    if (
$hSLength != 0)
    {
        if (
$hRPosition == 0)
        {
           
$hPassHash = substr($hPassHash, $hLPosition);
        }
        else
        {
           
$hPassHash = substr($hPassHash, $hLPosition, -$hRPosition);
        }
    }
}
?>

I've been getting few requests to explain how it's used so, this might be little long.

Problems:
1. In most solutions with hash and salt, you were bound to have one extra row in your database that would state, preferably random, salt for that hashed data. If attacker would manage to get drop of your database he would get hashed data and salt that is used with plain data to make it obscure, and then cracking that hashed data would be same as if you didn't add any salt to it.
2. I stumbled upon some functions that would hash data, then input salt into random places in hash and store it in database, but they would still have to write down random parameter used to scramble salt so they could reuse it when validating data. Getting simple database drop wouldn't help much here, but if they would manage to get their hands on obscuring function too, they could easily see what is salt and what hash.

Solutions:
1. Why use extra row to store salt when you can input it in hash. I'm not sure how attackers determine what type of hash are they facing, but I guess it has connection to hash length. In that case, why make attackers job easier and store in database data_hash+salt where they could assume just by it's length it has salt in there.
Reason behind $keepLength. If it's set to 1, strlen of hashed data plus salt would be equal to strlen of hashed data leading attacker to believe there is no salt.
If you leave $keepLength on NULL, strlen of final result would be strlen(used_hash_algorithm)+$hSLength.
$minhPass is there to reserve enough place for string that has to be hashed, so someone using this function wouldn't accidentally delete it by setting too high salt length ($hSLength), for example... if you set it 30000 it will keep working normal.

2. If you think about it, constant, but variable value when making input would be same data that is being input.
In case we're trying to hash password, and have user A with password "notme", password strlen equals to 5, and if we use default parameters of the function, with $keepLength set to 1, process would be:
random salt, hash it, add first 5 characters of hashed_salt at beginning of plain password, add last 5 characters of hashed_salt at end of plain password, hash it. Replace first 5 characters of hashed_password with first 5 character of hashed_salt, do same with last 5 characters of hashed_password, return hashed_password.
In case that string is longer than 10 characters function would use simple mathematics to reduce it to numbers lower than 10, well... lower than number that is stated in $hSLength.
And good thing is that every time user enters correct password it has same length so it's not necessary to write it anywhere.

So what is achieved in the end?
1. Attacker might not know that hash is salted, and you don't have that extra row in your database stating THIS IS SALT FOR THIS HASH.
2. If he does find out that it is, he wouldn't know what is hashed password and what is salt.
3. If he manages to get access to obscure function, only thing that might help him is value of $hSLength, where if $hSLength is set to 10 he would have to crack 10 variations of hashed string since he doesn't know how long password of user he's trying to crack is.
For example first variation would be hashed_password without last 10 characters, second variation would be hashed_password without first character and last 9 characters...
4. Even in case he has enough power to crack all 10 variations, resulting string that he might get doesn't necessarily has to be exactly long as password of original user in which case, attacker fails again.
Andre D
9.10.2008 3:28
Here's a better version of the getDigestNotation() function I posted earlier. (The first version had a bug in the argument checking.)

<?php
function getDigestNotation($rawDigest, $bitsPerCharacter, $chars = NULL)
{
    if (
$chars === NULL || strlen($chars) < 2) {
       
$chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-,';
    }

    if (
$bitsPerCharacter < 1) {
       
// $bitsPerCharacter must be at least 1
       
$bitsPerCharacter = 1;

    } elseif (
strlen($chars) < pow(2, $bitsPerCharacter)) {
       
// Character length of $chars is too small for $bitsPerCharacter
        // Set $bitsPerCharacter to greatest value allowed by length of $chars
       
$bitsPerCharacter = 1;
       
$minCharLength = 2;

        while (
strlen($chars) >= ($minCharLength *= 2)) {
           
$bitsPerCharacter++;
        }

        unset(
$minCharLength);
    }

   
$bytes = unpack('C*', $rawDigest);
   
$byteCount = count($bytes);

   
$out = '';
   
$byte = array_shift($bytes);
   
$bitsRead = 0;

    for (
$i = 0; $i < $byteCount * 8 / $bitsPerCharacter; $i++) {

        if (
$bitsRead + $bitsPerCharacter > 8) {
           
// Not enough bits remain in this byte for the current character
            // Get remaining bits and get next byte
           
$oldBits = $byte - ($byte >> 8 - $bitsRead << 8 - $bitsRead);

            if (
count($bytes) == 0) {
               
// Last bits; match final character and exit loop
               
$out .= $chars[$oldBits];
                break;
            }

           
$oldBitCount = 8 - $bitsRead;
           
$byte = array_shift($bytes);
           
$bitsRead = 0;

        } else {
           
$oldBitCount = 0;
        }

       
// Read only the needed bits from this byte
       
$bits = $byte >> 8 - ($bitsRead + ($bitsPerCharacter - $oldBitCount));
       
$bits = $bits - ($bits >> $bitsPerCharacter - $oldBitCount << $bitsPerCharacter - $oldBitCount);
       
$bitsRead += $bitsPerCharacter - $oldBitCount;

        if (
$oldBitCount > 0) {
           
// Bits come from seperate bytes, add $oldBits to $bits
           
$bits = ($oldBits << $bitsPerCharacter - $oldBitCount) | $bits;
        }

       
$out .= $chars[$bits];
    }

    return
$out;
}
?>
Andr D
6.10.2008 21:01
Sometimes you want the digest in both readable notation (such as hexadecimal) and raw binary. At other times you want the digest in a notation other than hexadecimal.

The following getDigestNotation() function takes a binary string and returns it in base 2, 4, 8, 16, 32, or 64 notation. It works with sha1(), md5(), hash(), or anything else that can output a raw binary string.

It works similar to the session.hash_bits_per_character php.ini configuration option.

You can specify which characters to use for each position, or use the default, which matches session.hash_bits_per_character (0-9, a-z, A-Z, "-", ","). The practical range of bits to use per character ($bitsPerCharacter) is 1 to 6; you may use more, but you will have to provide your own base character string ($chars) that is at least pow(2, $bitsPerCharacter) characters long. So even with 7 bits per character you need to specify a value for $chars that is 128 characters long, which exceeds the number of printable ASCII characters.

The output's radix relates to the value of $bitsPerCharacter as follows:
1: base-2 (binary)
2: base-4
3: base-8 (octal)
4: base-16 (hexadecimal)
5: base-32
6: base-64

<?php
$raw
= sha1(uniqid(mt_rand(), TRUE), TRUE);

echo
getDigestNotation($raw, 6);

function
getDigestNotation($rawDigest, $bitsPerCharacter, $chars = NULL)
{
    if (
$chars === NULL || strlen($chars) < 2) {
       
$chars '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-,';
    }

    if (
$bitsPerCharacter < 1) {
       
// $bitsPerCharacter must be at least 1
       
$bitsPerCharacter = 1;

    } elseif (
strlen($chars) < pow(2, $bitsPerCharacter)) {
       
// Character length of $chars is too small for $bitsPerCharacter
        // Set $bitsPerCharacter to greatest value allowed by length of $chars
       
$bitsPerCharacter = 1;

        do {
           
$bitsPerCharacter++;
        } while (
strlen($chars) > pow(2, $bitsPerCharacter));
    }

   
$bytes = unpack('C*', $rawDigest);
   
$byteCount = count($bytes);

   
$out = '';
   
$byte = array_shift($bytes);
   
$bitsRead = 0;

    for (
$i = 0; $i < $byteCount * 8 / $bitsPerCharacter; $i++) {

        if (
$bitsRead + $bitsPerCharacter > 8) {
           
// Not enough bits remain in this byte for the current character
            // Get remaining bits and get next byte
           
$oldBits = $byte - ($byte >> 8 - $bitsRead << 8 - $bitsRead);

            if (
count($bytes) == 0) {
               
// Last bits; match final character and exit loop
               
$out .= $chars[$oldBits];
                break;
            }

           
$oldBitCount = 8 - $bitsRead;
           
$byte = array_shift($bytes);
           
$bitsRead = 0;

        } else {
           
$oldBitCount = 0;
        }

       
// Read only the needed bits from this byte
       
$bits = $byte >> 8 - ($bitsRead + ($bitsPerCharacter - $oldBitCount));
       
$bits = $bits - ($bits >> $bitsPerCharacter - $oldBitCount << $bitsPerCharacter - $oldBitCount);
       
$bitsRead += $bitsPerCharacter - $oldBitCount;

        if (
$oldBitCount > 0) {
           
// Bits come from seperate bytes, add $oldBits to $bits
           
$bits = ($oldBits << $bitsPerCharacter - $oldBitCount) | $bits;
        }

       
$out .= $chars[$bits];
    }

    return
$out;
}
?>

Lastly, depending on the digest length, there may be fewer bits remaining for the last character than $bitsPerCharacter, so the last character will be smaller. The same thing happens with PHP's session ID generator, when 5 or 6 is used for session.hash_bits_per_character.
ranko84 at gmail dot com
9.05.2008 4:12
Thanks for the feedback. This should do the trick, I hope.
I think that I haven't understood this sentence completely "In this case you will need the salt to reside in the database along with the username and password." As in, were you refering to previous method, this method or this function.
Salt already resides in database along with username, password, or any string you decide to hash. This function just scrambles it depending on length of string (password) user enters so that attacker has trouble finding out what is salt and what is hash, if attacker even suspects that there is salt (reasons behind $keepLength, or defining $hSLength where you could set it to 24 leading attacker to believe he's facing sha256, not sha1).

<?php
function obscure ($hString, $hDecode = NULL, $hSLength = 10, $keepLength = NULL, $minhPass = 10, $hMethod = sha1)
{
    if (
$hDecode == NULL)
    {
        for (
$i = 0; $i<16; $i++)
        {
           
           
$hSalt = rand(33, 255);
           
$hRandomSalt .= chr($hSalt);
        }
       
$hRandomSalt = hash($hMethod, $hRandomSalt);
    }
    else
    {
       
$hRandomSalt = $hDecode;
    }

    if (
$keepLength != NULL)
    {
       
        if (
$hSLength > (strlen($hRandomSalt) - $minhPass))
        {
           
$hSLength = (strlen($hRandomSalt) - $minhPass);
        }
    }
    else if (
$hSLength < 0)
    {
       
$hSLength = 0;
    }

   
$hLPosition = strlen($hString);

    while (
$hLPosition > $hSLength)
    {
       
$hNumber = substr($hLPosition, -1);
       
       
$hLPosition = $hLPosition * ($hNumber/10);
    }

   
$hLPosition = (integer)$hLPosition;
   
$hRPosition = $hSLength - $hLPosition;

   
$hFSalt = substr($hRandomSalt, 0, $hLPosition);
   
$hLSalt = substr($hRandomSalt, -$hRPosition, $hRPosition);

   
$hPassHash = hash($hMethod, ($hLSalt . $hString . $hFSalt));

    if (
$keepLength != NULL)
    {
        if (
$hSLength != 0)
        {
           
$hPassHash = substr($hPassHash, $hLPosition, -$hRPosition);
        }
    }

    return
$hFSalt . $hPassHash . $hLSalt;
}
?>
Murali
8.05.2008 21:18
I think ranko's method is tougher to crack if there is a directory attack. However there is one small change I would suggest. Instead of having the hsalt as a limited array, it is better to generate a random string and use that as the salt. In this case you will need the salt to reside in the database along with the username and password.

Thanks Ranko...
ranko84 at gmail dot com
8.05.2008 1:17
If your still using sha1, here is a function that will hopefully help a little.

Sorry for any grammatical mistakes and for naming convention, I'm not very original and in most cases get myself confused after some time with them too :(

Benefits: even if attacker manages to generate string that will result in same hash, if that string doesn't have same amount of characters as original, it won't validate.

<?php
function obscure ($hString, $hDecode = NULL, $hSLength = 10, $keepLength = NULL, $minhPass = 10, $hMethod = sha1)
{
    if (
$hDecode == NULL)
    {
       
$hSalt = array("\\", "!", "0", "\"", "1", "#", "2", "$", "3", "%", "4", "&", "5", "/", "6", "(", "7", ")", "8", "=", "9", "?", "*");
       
        for (
$i = 0; $i<16; $i++)
        {
           
$hRandomIndex = array_rand($hSalt);
           
$hRandomSalt .= $hSalt[$hRandomIndex];
        }
       
$hRandomSalt = hash($hMethod, $hRandomSalt);
    }
    else
    {
       
$hRandomSalt = $hDecode;
    }

    if (
$keepLength != NULL)
    {
       
        if (
$hSLength > (strlen($hRandomSalt) - $minhPass))
        {
           
$hSLength = (strlen($hRandomSalt) - $minhPass);
        }
    }
    else if (
$hSLength < 0)
    {
       
$hSLength = 0;
    }

   
$hLPosition = strlen($hString);

    while (
$hLPosition > $hSLength)
    {
       
$hNumber = substr($hLPosition, -1);
       
       
$hLPosition = $hLPosition * ($hNumber/10);
    }

   
$hLPosition = (integer)$hLPosition;
   
$hRPosition = $hSLength - $hLPosition;

   
$hFSalt = substr($hRandomSalt, 0, $hLPosition);
   
$hLSalt = substr($hRandomSalt, -$hRPosition, $hRPosition);

   
$hPassHash = hash($hMethod, ($hLSalt . $hString . $hFSalt));

    if (
$keepLength != NULL)
    {
        if (
$hSLength != 0)
        {
           
$hPassHash = substr($hPassHash, $hLPosition, -$hRPosition);
        }
    }

    return
$hFSalt . $hPassHash . $hLSalt;
}
?>

How to use?
$hString is string you want to hash, probably password.
$hDecode is already hashed string (password). When you use function obscure() to generate hash, you will probably enter it in database. When user wants to log in, you pass plain password with $hString, and hashed password from database in $hDecode.
$hSLength is length of salt. It's caped by method you use to hash, so in case you use sha1, you can pass value 100, but it will still use 40 characters.
$keepLength can be used to cap final output at max characters that hash function gives. In case sha1, hash would give output of 40 characters. If you set $keepLength to '1' final outputted hash would have 40 characters by cutting pieces of hashed password. Why would you want to do it? So that attacker doesn't know it's salted :)
$minhPass - it is usually defined only if you decide to set $keepLength so that you can define minimum amount of characters of hashed password you want to keep. Default is 10. It's used because you can accidentally set, in combination with $keepLength, $hSLength too high which would cause that all characters of hashed password are cut out and leave only salt which would always validate true without regard what password user enters.
$hMethod defines which method you want to use to hash plain text since this function is build using hash(). If you want to use sha512, function will adapt to it.

Quick example:
Hashing a password - obscure ($plain_password);
Validating password - obscure ($plain_password, $hashedPassword);

I tried to be as brief as possible, best way to see how it's used... test it, otherwise mail me if you have questions.
nathan
26.02.2008 5:11
The suggestion below to double-hash your password is not a good idea.  You are much much better off adding a variable salt to passwords before hashing (such as the username or other field that is dissimilar for every account).

Double hashing is *worse* security than a regular hash.  What you're actually doing is taking some input $passwd, converting it to a string of exactly 32 characters containing only the characters [0-9][A-F], and then hashing *that*. You have just *greatly* increased the odds of a hash collision (ie. the odds that I can guess a phrase that will hash to the same value as your password).

sha1(md5($pass)) makes even less sense, since you're feeding in 128-bits of information to generate a 256-bit hash, so 50% of the resulting data is redundant.  You have not increased security at all.
brian at realm3 dot com
15.02.2008 15:47
Those wishing to generate an HMAC-SHA1 type key can now use hash_hmac()

http://us2.php.net/manual/en/function.hash-hmac.php
ERASEthemasterswordsman at gmail dot com
11.02.2008 3:09
Please note that hashing a has is not completely secure, but can be used as an extra precaution (but not recommended):

<?php

sha1
(md5($string));

?>
NoName
14.09.2007 0:26
Regarding the twistSTR - the problem is that currently it is relatively easy to generate a collision for any alphanumeric plaintext of a given, short length via e.g. a rainbow table. You're bettter off using a sufficiently lengthy and random salt.
novum123 at ribbonbazaar dot com
29.11.2006 17:54
So far as the dictionary attacks are concerned, I thought up the following function:

<?php
function twistSTR($array){
 
$twisted="";
 
$array_strlen=array();

  foreach (
$array as $element){
   
$array_strlen[]=strlen($element);
  }

  for (
$i=0; $i<max($array_strlen); $i++){
    foreach (
$array as $element){
      if (
$i<strlen($element)){
       
$twisted=$twisted.$element{$i};
      }
    }
  }

  return
$twisted;
}
?>

The twistSTR function basically takes an array input of strings and alternates each character of each string among all the other strings.  For example:

<?php
echo twistSTR(array("this","and","that"));//output: tathnhidast
?>

It can be applied in the following manner:

<?php
if ($un===$_POST["username"] && $pwd===sha1(twistSTR(array($salt,$_POST["password"])))){
?>

It's not amazingly difficult to reverse engineer the actual output, but then again, that's not the point.  The point is that when a password is entered into one of those databases, they are going to enter for example "thisandthat", not "tathnhidast".
Dan Casey
16.11.2006 17:54
For all the php4 users who thought you were limited to sha1.

<?php

$phrase
= "Hello World";

$sha1a base64_encode(sha1($phrase));
$sha1b base64_encode(bin2hex(mhash(MHASH_SHA1,$phrase)));
$sha256b= base64_encode(bin2hex(mhash(MHASH_SHA256,$phrase)));

echo (
"SHA1..:" . $sha1a . "\n");
echo (
"SHA1..:" . $sha1b . "\n");
echo (
"SHA256:" . $sha256b . "\n");
?>
# php sha.php
SHA1..:MGE0ZDU1YThkNzc4ZTUwMjJmYWI3MDE5NzdjNWQ4NDBiYmM0ODZkMA==
SHA1..:MGE0ZDU1YThkNzc4ZTUwMjJmYWI3MDE5NzdjNWQ4NDBiYmM0ODZkMA==
SHA256:YTU5MWE2ZDQwYmY0MjA0MDRhMDExNzMzY2ZiN2IxOTBkNjJjNjV...........
Gregory Boshoff
18.10.2006 13:23
Note that the sha1 algorithm has been compromised and is no longer being used by government agencies.

As of PHP 5.1.2 a new set of hashing functions are available.

http://www.php.net/manual/en/function.hash.php

The new function hash() supports a new range of hashing methods.

echo hash('sha256', 'The quick brown fox jumped over the lazy dog.');

It is recommended that developers start to future proof their applications by using the stronger sha-2, hashing methods such as sha256, sha384, sha512 or better.

As of PHP 5.1.2 hash_algos() returns an array of system specific or registered hashing algorithms methods that are available to PHP.

print_r(hash_algos());
erling dot westenvik at gmail dot com
17.05.2006 15:15
Regarding php at REMOVEMEkennel17 dot co dot uk's note below:

The phrase: "To get the correct behaviour", would perhaps be better off if it read: "To get the wanted (but not recommended) behaviour".

Always honor the expected data types for functions: sha1 expects a string as input, and returns a string on exit. NULL, TRUE and FALSE are not string data types. The string "" is a string as good as "any". By following the "logic" that sha1("") should return "", then what should sha1("a") return? "b"? "c"?

An authentication system that allows for blank passwords is not really an authentication system in the first place. What you are describing is merely a way to tell the application that you want to see data in some specific context, like sorted by user name, etc. Create other tools for this purpose and leave the authentication system to deal with what it is supposed to do: Granting users access to restricted data and blocking other users from seeing the same data.

Don't store passwords in clear text, but salt and encrypt them. That way it makes perfect sense having <?php $sStoredPwd === sha1($sStoredSalt . $_POST["sTypedPwd"]); ?>, even with a blank "password". No other person than the user itself, not even the programmer, should know the password or be able to guess it. If the user forgets the password, a new one must be generated.

Regards,
Erling
spam at barad-dur dot nl
23.10.2005 13:59
For servers that don't have the mhash or crypto++-php extensions I wrote an implementation of the SHA256 algorithm as found in the FIPS 180-2 standard.
Source code can be found at http://dev.barad-dur.nl/sha256/
php at REMOVEMEkennel17 dot co dot uk
20.09.2005 3:52
It should be noted that sha1("") does not return an empty string.  This means that if you are running a system that does not require users to have a password, the following code will not work as expected:

<?php
if ($StoredPassword == sha1($NewPassword))
   
// Password good
?>

If $StoredPassword and $NewPassword are both blank, then the password should be treated as good, but because sha1("") != "" it will be treated as bad. To get the correct behaviour you need to use:

<?php
if (($StoredPassword == "" && $NewPassword == "") || ($StoredPassword == sha1($NewPassword)))
   
// Password good
?>

(Note: I use a custom IsBlank() function instead of comparison against the empty string, so NULL values are also matched.)

For reference, here are a couple of special values put through sha1().  Note that sha1("") == sha1(NULL) == sha1(false), and also that sha1(0) != sha1(false)

"" -> da39a3ee5e6b4b0d3255bfef95601890afd80709
NULL -> da39a3ee5e6b4b0d3255bfef95601890afd80709
0 -> b6589fc6ab0dc82cf12099d1c2d40ab994e8410c
1 -> 356a192b7913b04c54574d18c28d46e6395428ab
false -> da39a3ee5e6b4b0d3255bfef95601890afd80709
true -> 356a192b7913b04c54574d18c28d46e6395428ab
Dan
5.09.2005 19:12
I've noticed websites are now starting to require passwords of a certain length that MUST contain at least 1 non-alphanumeric character. This in itself makes dictionary attacks kind of useless. My web site requires that as well. It uses md5, and appends a site code into the md5 as well. And the include file that contains that site key is outside the public folders. I sure hope I've done enough to keep the bad boys out.
arachnid at notdot dot net
31.08.2005 22:12
WTM's post below is misinformed. The purpose of adding the random data (known as a 'salt', as multiple posters have pointed out further below) is to prevent dictionary based attacks, allowing the attacker use a pre-prepared database of passwords and hashes against all your stored passwords, instead of having to recalculate for each user.

Adding the salt in a more 'complicated' way is a poor security tactic - if the attacker discovers your method, the security advantage is lost, and you have no way of changing it without rehashing all your passwords.
WTM
3.08.2005 16:30
Actually, the post by Helpful Harry won't improve your security except for the most simple break in attempts.  Since the random seed is attached to the end of the password hash, if you steal the hashed password, you steal the seed.

That means you can write a simple php program to call the pw_check function Harry included from a loop, feeding it dictionary words or random characters.

Of course, if you modified the program to use the seed in a more complicated way, "they" would have to know the new function's operation.  But then again, if someone can steal your password database, they can probably steal your website code (or guess it).
Helpful Harry
6.07.2005 19:21
check out these randomized sha1 password storage functions, they output a string of 50 characters, the first 40 characters being a sha1 output based on the last 10 characters - those being a random seed

to encode a password run pw_encode with the password, it'll return a different pseudo-random string every time - store this value.

to check a password run pw_check with the password attempt and the stored value, it'll return true on a match and false otherwise

these functions eliminate the pesky problem of dictionary matches being run on your password lists

<?php

function pw_encode($password)
{
   for (
$i = 1; $i <= 10; $i++)
      
$seed .= substr('0123456789abcdef', rand(0,15), 1);
   return
sha1($seed.$password.$seed).$seed;
}

function
pw_check($password, $stored_value)
{
   if (
strlen($stored_value) != 50)
      return
FALSE;
  
$stored_seed = substr($stored_value,40,10);
   if (
sha1($stored_seed.$password.$stored_seed).$stored_seed == $stored_value)
     return
TRUE;
   else
     return
FALSE;
}

?>
alex at milivojevic dot org
28.04.2005 23:12
Regarding my previous comment, if you want to be on the safe side and use only ASCII printable seeds (shouldn't matter for SSHA seeds), something like this could be used:

<?php
$salt
= substr(base64_encode(pack("H*", sha1(mt_rand()))), 0, 4);
?>
alex at milivojevic dot org
28.04.2005 19:45
If you don't have mhash library and/or PHP module (for example, Red Hat systems, which includes Fedora), and you don't feel like adding it, you can use something like this to generate and verify SSHA hashes.

<?php

$password
= "test";

// Generate SSHA hash
mt_srand((double)microtime()*1000000);
$salt = pack("CCCC", mt_rand(), mt_rand(), mt_rand(), mt_rand());
$hash = "{SSHA}" . base64_encode(pack("H*", sha1($password . $salt)) . $salt);
echo
$hash . "\n";

// Verify SSHA hash
$ohash = base64_decode(substr($hash, 6));
$osalt = substr($ohash, 20);
$ohash = substr($ohash, 0, 20);
$nhash = pack("H*", sha1($password . $osalt));
if (
$ohash == $nhash) {
   echo
"Password OK\n";
 } else {
   echo
"Password verifiaction failed\n";
 }

?>
svn at datapirate dot de
20.02.2005 14:26
Wanna use SHA-2 algorithm? Try this:

Download Tar-Ball from http://www.adg.us/computers/sha.html
Compile (may occur some warnings) and test it:

cc -O2 -DSHA2_UNROLL_TRANSFORM -Wall -o sha2 sha2prog.c sha2.c
./sha2test.pl

Copy it to /usr/local/bin/ (don't forget to check permissions)

Here are two functions that could be used with:

function sha2($bits, $string){
    $sha2bin="/usr/local/bin/sha2";
    $echocmd="echo";
    if(!in_array($bits, array(256, 384, 512)))return(false);
    $r=exec($echocmd." ".escapeshellarg($string)."|".$sha2bin." -q -".$bits, $sha2);
    return($sha2[0]);
}

function sha2_file($bits, $filename){
    $sha2bin="/usr/local/bin/sha2";
    if(!in_array($bits, array(256, 384, 512)))return(false);
    if(!file_exists($filename)||!is_readable($filename))return(false);
    $r=exec($sha2bin." -q -".$bits." ".escapeshellarg($filename), $sha2);
    return($sha2[0]);
}

and use it like below:

<?php
$str
= 'apple';
if (
sha2(256, $str) === '303980bcb9e9e6cdec515230791af8b0ab1aaa244b58a8d99152673aa22197d0') {
   echo
"Would you like a green or red apple?";
   exit;
}
?>
hans at pennywaffer dot net
19.12.2004 17:18
You can also use sha1() to create a fixed sized ID based on the folderpath containing the performing script. Imagine an environment where multiple instances of a script are running simultaneously, making use of session variables (a good example would be thumbnaildirectories that each contain an instance of a thumbnailmanager). To prevent your $_SESSION array from getting influenced by other scripts that are on the same server and run from the same browserwindow (and thus having the same session ID), use the following approach:

session_start();
$myDir = sha1(dirname(__FILE__));
$_SESSION[$myDir]['var'] = etc;

instead of just using $_SESSION['var'] = etc;

This way your userauthentication and shoppingcart data won't get mixed up. The use of sha1() also prevents strange symbols or spaces in the folderpath messing up the ID, and makes sure the ID has a constant size  regardless the length of the path.
sinatosk at gmail dot com
22.11.2004 21:43
Heres an SHA1 function that will work on it's own completely. This is for users who are using below PHP 4.3.0. it works same as PHP5 ( being able to return raw output ).

<?php

/*
** Date modified: 1st October 2004 20:09 GMT
*
** PHP implementation of the Secure Hash Algorithm ( SHA-1 )
*
** This code is available under the GNU Lesser General Public License:
** http://www.gnu.org/licenses/lgpl.txt
*
** Based on the PHP implementation by Marcus Campbell
** http://www.tecknik.net/sha-1/
*
** This is a slightly modified version by me Jerome Clarke ( sinatosk@gmail.com )
** because I feel more comfortable with this
*/

function sha1_str2blks_SHA1($str)
{
   
$strlen_str = strlen($str);
   
   
$nblk = (($strlen_str + 8) >> 6) + 1;
   
    for (
$i=0; $i < $nblk * 16; $i++) $blks[$i] = 0;
   
    for (
$i=0; $i < $strlen_str; $i++)
    {
       
$blks[$i >> 2] |= ord(substr($str, $i, 1)) << (24 - ($i % 4) * 8);
    }
   
   
$blks[$i >> 2] |= 0x80 << (24 - ($i % 4) * 8);
   
$blks[$nblk * 16 - 1] = $strlen_str * 8;
   
    return
$blks;
}

function
sha1_safe_add($x, $y)
{
   
$lsw = ($x & 0xFFFF) + ($y & 0xFFFF);
   
$msw = ($x >> 16) + ($y >> 16) + ($lsw >> 16);
   
    return (
$msw << 16) | ($lsw & 0xFFFF);
}

function
sha1_rol($num, $cnt)
{
    return (
$num << $cnt) | sha1_zeroFill($num, 32 - $cnt);   
}

function
sha1_zeroFill($a, $b)
{
   
$bin = decbin($a);
   
   
$strlen_bin = strlen($bin);
   
   
$bin = $strlen_bin < $b ? 0 : substr($bin, 0, $strlen_bin - $b);
   
    for (
$i=0; $i < $b; $i++) $bin = '0'.$bin;
   
    return
bindec($bin);
}

function
sha1_ft($t, $b, $c, $d)
{
    if (
$t < 20) return ($b & $c) | ((~$b) & $d);
    if (
$t < 40) return $b ^ $c ^ $d;
    if (
$t < 60) return ($b & $c) | ($b & $d) | ($c & $d);
   
    return
$b ^ $c ^ $d;
}

function
sha1_kt($t)
{
    if (
$t < 20) return 1518500249;
    if (
$t < 40) return 1859775393;
    if (
$t < 60) return -1894007588;
   
    return -
899497514;
}

function
sha1($str, $raw_output=FALSE)
{
    if (
$raw_output === TRUE ) return pack('H*', sha1($str, FALSE));
   
   
$x = sha1_str2blks_SHA1($str);
   
$a 1732584193;
   
$b = -271733879;
   
$c = -1732584194;
   
$d 271733878;
   
$e = -1009589776;
   
   
$x_count = count($x);
   
    for (
$i = 0; $i < $x_count; $i += 16)
    {
       
$olda = $a;
       
$oldb = $b;
       
$oldc = $c;
       
$oldd = $d;
       
$olde = $e;
       
        for (
$j = 0; $j < 80; $j++)
        {
           
$w[$j] = ($j < 16) ? $x[$i + $j] : sha1_rol($w[$j - 3] ^ $w[$j - 8] ^ $w[$j - 14] ^ $w[$j - 16], 1);
           
           
$t = sha1_safe_add(sha1_safe_add(sha1_rol($a, 5), sha1_ft($j, $b, $c, $d)), sha1_safe_add(sha1_safe_add($e, $w[$j]), sha1_kt($j)));
           
$e = $d;
           
$d = $c;
           
$c = sha1_rol($b, 30);
           
$b = $a;
           
$a = $t;
        }
       
       
$a = sha1_safe_add($a, $olda);
       
$b = sha1_safe_add($b, $oldb);
       
$c = sha1_safe_add($c, $oldc);
       
$d = sha1_safe_add($d, $oldd);
       
$e = sha1_safe_add($e, $olde);
    }
   
    return
sprintf('%08x%08x%08x%08x%08x', $a, $b, $c, $d, $e);
}

?>
rsemirag at yahoo dot com
2.11.2004 19:34
If you're struggling to generate an SHA encoded password for LDAP (PHP < 5.0), what you end up needing is this:

$userpassword = base64_encode(pack("H*", sha1($pass)));

I found this in the OpenLDAP FAQ (many thanks to Google and Ace), though I'm using the iPlanet LDAP server.

Ray Semiraglio
brian_bisaillon at rogers dot com
26.02.2004 5:19
Source code to create SSHA passwords...

public function HashPassword($password)
{
  mt_srand((double)microtime()*1000000);
  $salt = mhash_keygen_s2k(MHASH_SHA1, $password, substr(pack('h*', md5(mt_rand())), 0, 8), 4);
  $hash = "{SSHA}".base64_encode(mhash(MHASH_SHA1, $password.$salt).$salt);
  return $hash;
}

Source code to validate SSHA passwords...

public function ValidatePassword($password, $hash)
{
  $hash = base64_decode(substr($hash, 6));
  $original_hash = substr($hash, 0, 20);
  $salt = substr($hash, 20);
  $new_hash = mhash(MHASH_SHA1, $password . $salt);
   if (strcmp($original_hash, $new_hash) == 0)
     ... do something because your password is valid ...
  else
    echo 'Unauthorized: Authorization has been refused for the credentials you provided. Please login with a valid username and password.';
    ... be sure to clear your session data ...
}

Note: The format is compatible with OpenLDAP's SSHA scheme if I'm not mistaken.
mark at dot BANSPAM dot pronexus dot nl
30.01.2004 15:28
Looking for a simple function to implement HMAC-SHA1 but don't want to use the entire PEAR Message lib?

//Calculate HMAC-SHA1 according to RFC2104
// http://www.ietf.org/rfc/rfc2104.txt
function hmacsha1($key,$data) {
    $blocksize=64;
    $hashfunc='sha1';
    if (strlen($key)>$blocksize)
        $key=pack('H*', $hashfunc($key));
    $key=str_pad($key,$blocksize,chr(0x00));
    $ipad=str_repeat(chr(0x36),$blocksize);
    $opad=str_repeat(chr(0x5c),$blocksize);
    $hmac = pack(
                'H*',$hashfunc(
                    ($key^$opad).pack(
                        'H*',$hashfunc(
                            ($key^$ipad).$data
                        )
                    )
                )
            );
    return bin2hex($hmac);
}

It is very useful for client-authentication. see also http://cookies.lcs.mit.edu/pubs/webauth:tr.pdf
Optionally you can change $hashfunc to 'md5' to make this an HMAC-MD5 function ;-)
If you want raw or base64 output instead of hexadecimal, just change the last return line.

Cheers,
Mark

p.s. the "$hmac =" line used to be 1 line but I had to cut it up in order to fit it here ;)
labarks
15.11.2003 12:06
Append this to the your sha1lib file to make it more portable.  If your version of php does support sha1() then it will try to use Mhash or else it will use the sha1lib.  Use $sha1 if you want to display which is being used.

if ( function_exists('sha1') )
    $sha1 = "sha1";

if ( !function_exists('sha1') && function_exists('mhash'))
{
    function sha1($hash_source)
    {
           $hash = mhash(MHASH_SHA1, $hash_source);
          $hex_hash = bin2hex($hash);
           return $hex_hash;
    }
    $sha1 = "Mhash";
}
if ( !function_exists('sha1') && !function_exists('mhash'))
{
    function sha1( $string, $raw_output = false )
    {
        $library = new Sha1Lib();
       
        return $raw_output ? $library->str_sha1($string) : $library->hex_sha1($string);
    }
    $sha1 = "sha1lib";
}
andreas at schmeiler dot de
16.10.2003 20:50
To use the sha1 function in php versions <4.3 do the following:

install the mhash library (see http://mhash.sourceforge.net)

then, sha1 can be implemented as follows:

function sha1($hash_source) {
   $hash = mhash(MHASH_SHA1, $hash_source);
   $hex_hash = bin2hex($hash);
   return $hex_hash;
}
bobm at hp dot com
23.04.2003 18:12
To achieve raw binary format prior to PHP5, you can do this...

$raw = pack("H*", sha1($str));

Regards,

Bob Mader



PHP Powered Diese Seite bei php.net
The PHP manual text and comments are covered by the Creative Commons Attribution 3.0 License © the PHP Documentation Group - Impressum - mail("TO:Reinhard Neidl",...)