Saturday, August 10, 2013

Very Bad Password Advice

This post on How-To-Geek about generating passwords from the command line advocates generating passwords from the current date and time.


The first command they give is (note double fail: base64-encoding hex):
 date +%s | sha256sum | base64 | head -c 32 ; echo  
They do provide a command that gives a good alphanumeric password:
 tr -cd '[:alnum:]' < /dev/urandom | fold -w30 | head -n1  
 However, they also mention this one:
 date | md5sum  
 About the above command, they say, "I'm sure that some people will complain that it’s not as random as some of the other options, but honestly, it’s random enough if you’re going to be using the whole thing." That is absolutely wrong and demonstrates a complete lack of understanding what a hash function is.

Lessons Learned:
  • Hashing does not add randomness. The output of a hash is as random as its input.
  • Use a cryptographically-secure random number generator to generate passwords. 

7 comments:

  1. Other bugs.
    a) the command is "sha256" not "sha256sum"
    bash: sha256sum: command not found
    $echo a| sha256
    87428fc522803d31065e7bce3cf03fe475096631e5e07bbd7a0fde60c4cf25c7

    b) the second command gives "Illegal byte sequence"
    %tr -cd '[:alnum:]' < /dev/urandom | fold -w30 | head -n1
    tr: Illegal byte sequence

    ReplyDelete
    Replies
    1. My system has a sha256sum command (from GNU Coreutils), and no sha256 command.

      Delete
    2. what batshit insane distro are you on that it's `sha256`?

      Delete
    3. (Anon #3 here).
      FreeBSD has sha256 as its own command and permits installing GNU Coreutils from packages.

      Delete
  2. atually, bug B above was my fault. I needed
    %LANG=C tr -cd '[:alnum:]' < /dev/urandom | fold -w30 | head -n1

    ReplyDelete
  3. Could someone explain why base64-encoding hex is failing?

    ReplyDelete
    Replies
    1. Passwords generated this way have less entropy per character.

      A truly random base64 character has 6 bits of entropy (since there are 2^6 possible characters). So a truly random 20-character base64 string would contain 120 bits of entropy.

      A truly random hex string has 4 bits of entropy per character. To encode 3 bytes (24 bits) in base64, it takes 24/6=4 base64 characters. In a random hex string, there are 3*4=12 bits of entropy in 3 bytes, so there are 12 bits of entropy for every 4 base64 characters, or 12/4 = 3 bits per character. A 20-character base64 string generated this way would contain only 60 bits of entropy.

      Delete