tags:

views:

276

answers:

6

On Linux I am used to using mkpasswd to generate random passwords to use, on OS X however I don't have this command. Instead of sshing in to my vps every time, I wanted to re implement it using Java. What I have done is pick at random 4 lower case letters, 2 upper case letters, 2 symbols (/ . , etc) and 2 numbers. Then I create a vector and shuffle that too.

Do you think this is good enough randomization?

A: 

Depends on where your entropy comes from. Using rand() or similar functions that your particular language comes with may not be secure.

On OSX you can use /dev/random I think.

Inshallah
+1  A: 

yes, it is. If you are using java.util.Random:

An instance of this class is used to generate a stream of pseudorandom numbers. The class uses a 48-bit seed, which is modified using a linear congruential formula. (See Donald Knuth, The Art of Computer Programming, Volume 2, Section 3.2.1.)

The algorithms implemented by class Random use a protected utility method that on each invocation can supply up to 32 pseudorandomly generated bits.

EDIT

in response to a comment:

/**
 * Creates a new random number generator. This constructor sets
 * the seed of the random number generator to a value very likely
 * to be distinct from any other invocation of this constructor.
 */
public Random() { 
    this(++seedUniquifier + System.nanoTime()); 
}

private static volatile long seedUniquifier = 8682522807148012L;
dfa
And where does the seed come from? :)
Inshallah
There are likely not to be any conflicts, but doesn't make it secure. If someone can guess the value of System.nanoTime(), at the time the generator is being seeded, it is likely that he can find out what passwords are being generated. That is a real problem; I don't think you can use java.util.Random as it is, you'll have to get your seed from somewhat more unpredictable sources like, for example, /dev/random.
Inshallah
you are right :-)
dfa
A: 

It might be OK, but you should allow some randomization in password lengths perhaps.

If your program became popular it would become a weakness that the password length was public knowledge. Also randomize the exact ratio of lowercase:uppercase:symbols:numbers a little.

Sean A.O. Harney
I'll randomize those for now i was just copying what mkpasswd without any options produce.
Hamza Yerlikaya
A: 

Why not just compile mkpasswd on your OS X host?

Thorbjørn Ravn Andersen
A: 

There is a similar pwgen command available in the Mac Ports.

Alexander Gladysh
+2  A: 

If you use java.security.SecureRandom instead of java.util.Random then it's probably secure. SecureRandom provides a "cryptographically strong pseudo-random number generator (PRNG)". I.e. it ensures that the seed cannot easily be guessed and that the numbers generated have high entropy.

Yrlec
I dug that up somewhere: ... letting the SecureRandom seed itself from its own internal source of randomness. By default, this is configured to be the device /dev/random on Mac OS X.
Inshallah