SecureRandom Generator

I am trying to create Salt fo my passwords. This is my function

public byte[] generateSalt() throws Exception{

random = SecureRandom.getInstance(SECURE_RANDOM_ALGORITHM);

random.setSeed(System.currentTimeMillis());

byte[] salt = new byte[8];

random.nextBytes(salt);

return salt;

}

I called this function three times in my main method , I got follwing output

Salt is [B@7d772e

Salt is [B@11b86e7

Salt is [B@35ce36

I run my program again and got same output. Every time I run my program, I get same output. What is happening, I can't understand. I set seed current time so every time I run my program I should get different numbers.

[710 byte] By [namon20a] at [2007-11-27 3:44:55]
# 1

> Salt is [B@7d772e

> Salt is [B@11b86e7

> Salt is [B@35ce36

There is no issue to worry about, what you are seeing is that the hash codes of the generated byte arrays are the same over subsequent executions of the same program. Apparently you are passing the byte[] reference to System.out.println(), which calls the default toString() method of the Object class:

public class Object {

...

public String toString() {

return getClass().getName() + "@" + Integer.toHexString(hashCode());

}

Fortunately, the hash code of a byte[] array has nothing to do with the actual contents. Here are two subsequent executions in my machine, which generated the same hashCodes but different content each time:

1st try:

[B@fd13b5

48, -113, -77, -79, -75, 99, 36, -44

[B@118f375

50, -63, 72, -4, 13, -42, 35, -5

[B@117a8bd

0, -37, 115, 46, 22, 40, -88, -98

2nd try:

[B@fd13b5

-1, -42, 80, -12, 81, -81, 98, -40

[B@118f375

22, 49, -62, -86, 82, -84, 103, -46

[B@117a8bd

74, 77, 118, -120, 113, -93, -76, -20

Try printing each byte of the returned array and you'll see that it'll probably be the same case with your code, too.

Kind Regards,

Anestis

mrAnesta at 2007-7-12 8:48:36 > top of Java-index,Security,Other Security APIs, Tools, and Issues...