0

New to stackoverflow :)

I just now started to use a bcrypt function I've found on some site about security. I've never really worried about the output from this until our technician at work said this to me:

The salt seems to always be in the front of every password.

Is this correct or have I made a major boo boo? :)

The code I use is this:

<?php
function bcrypt($password, $salt, $rounds=12) {
    // Check if bcrypt is available on the server
    if (CRYPT_BLOWFISH != 1) {
        throw new Exception("bcrypt stöds inte. Se http://php.net/crypt");
        return;
    }

    // Check that rounds are within the allowed range
    if ($rounds < 4)
        $rounds = 4;
    else if ($rounds > 12) 
        $rounds = 12;

    // Create a prefix to tell the crypt that we want to use bcrypt
    $salt_prefix = sprintf('$2a$%02d$', $rounds);

    // Check if the salt contains invalid characters:
    if (!preg_match('#^[A-Za-z0-9./]{22}$#', $salt)) {

        // The salt is not bcrypt-safe. Redo to 22 characters (A-Za-z0-9. /)
        $new_salt = base64_encode($salt);

        if (strlen($new_salt) < 22)

            $new_salt .= base64_encode(md5($salt));
            $salt = substr($new_salt, 0, 22);
            $salt = str_replace(array('+', '-'), '.', $salt);    
            $salt = str_replace(array('=', '_'), '/', $salt);
    }

    // hash the password with bcrypt
    return crypt($password, $salt_prefix.$salt);
 }

 // Examples :
 echo "Bcrypt: ". bcrypt('abc', 'QyrjMQfjgGIb4ymtdKQXIr', 12);
?>

This will output:

Bcrypt: $2a$12$QyrjMQfjgGIb4ymtdKQXIewDBqhA3eNppF8qOrMhidnEbzNvmHqhy

As you can see the salt is inside the password now "bold text":

Salt = QyrjMQfjgGIb4ymtdKQXIr

pass = $2a$12$QyrjMQfjgGIb4ymtdKQXI

ewDBqhA3eNppF8qOrMhidnEbzNvmHqhy

This seem to be the same every time regardless of salt. Salt is always included except the last character?

CodesInChaos
  • 106,488
  • 23
  • 218
  • 262
Joakim
  • 1
  • 3
  • The answer to your question can be found here. http://stackoverflow.com/questions/13037271/the-point-with-the-salt-in-bcrypt – castis Aug 08 '13 at 21:06
  • 1
    Please use [this library](https://github.com/ircmaxell/password_compat) instead of rolling your own. PHP has strong and simple password hash functions since version 5.5, and this library backports them starting with PHP 5.3.7. Also please note that this library tries to mitigate timing attacks by always comparing the WHOLE string, and not stop upon the first difference. Bottom line: Simply use it. – Sven Aug 08 '13 at 21:09
  • Regarding the salt’s length: [Length of salt in CRYPT_BLOWFISH](http://stackoverflow.com/a/15727563/53114) – Gumbo Aug 08 '13 at 21:10
  • thanks for the really fast answers :) Now I know a little more. I will check out the library. But as I understand it the output is correct?. I do have to use random salts of course. – Joakim Aug 08 '13 at 21:25
  • Hi, please avoid `
    ` tags for formatting, and avoid quote devices `>` for your code. Posts can be (and often are) edited/improved here by higher-rep readers, and either of those make it hard work. Use paragraphs and four-space indents (code button) instead. Thanks!
    – halfer Aug 08 '13 at 21:28
  • No probs, welcome to the site! – halfer Aug 08 '13 at 22:25
  • Just to answer your question: yes it is correct that the salt is part of the resulting hash string, and it is ok that it is plaintext. This salt is necessary for verifying the password later. – martinstoeckli Aug 09 '13 at 22:45

1 Answers1

0

You can test an existing bcrypt hash with lots of tools online, like this bcrypt generator

Oli
  • 1,335
  • 1
  • 9
  • 10