Home > Archive > Java Security > October 2004 > SecureRandom.toString() gives same value!, BASE64Encoder.encode gives different value
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
SecureRandom.toString() gives same value!, BASE64Encoder.encode gives different value
|
|
|
| Hi all:
I was trying to verify if I am a getting different value each time I
run a program that uses SecureRandom. To my surprise, if I use
toString(), the value printed is same! What is the problem.
I have given the code, followed by two sample runs.
//----------------------------------------
import java.io.*;
import java.security.*;
import sun.misc.BASE64Encoder;
class t
{
public static void main(String args[])
{
byte[] s;
SecureRandom sr = null;
try {
sr = SecureRandom.getInstance("SHA1PRNG");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
s = new byte[160];
sr.nextBytes(s);
System.out.println("s using encoder = "+print(s));
System.out.println("s using toString = "+s);
}
static String print(byte[] raw)
{
BASE64Encoder encoder = new BASE64Encoder();
String base64 = encoder.encode(raw);
return base64;
}
}
//----------------------------------------
sample run 1
s using encoder = YR+6m8+Dm1M+pyT3VO6uLByuXXikEGevGKczeC9J
4AOCFPiZT+bLkSmRTtE/xh9rAtKkmVvKK6D4
gNmBB6uAV7yLsRe0o+43tBdk9MPvhVWkL7empDRE
gJM/FAexGF8hZKk07SXvPwMBCAcNiXHVJnha
SfvWhHG84acqd4zPxukQW658XJXDSr7ALJJ3yGPr
PCVj96896rlAn1t8LH0EEw==
s using toString = [B@1888759
sample run 2
s using encoder = KLfWEDX1zDCrVQ4IkdR6HIoSVq2HtxIJGt+pEYnY
fZ/8fYMqLytZlMhvwGz2BMuFBX3kbBO4Vd2Q
3JNcHbOEkfI+lxoSOH4wt0Ky/ BvFcyQGBGh3gZxSOv8GrVHXaltJr0zYzY2pMRAwq
ktioFWvE0UQ
2/JizSqm04mGOljEaMar/93i94pwODOe/Dds2XHjFJ7BE2eMnPrmGScrI8l9wA==
s using toString = [B@1888759
Any suggestions are most welcome.
Thanks
Amit
| |
|
| Thanks Mike for clearing my doubt. Indeed, I was incorrectly printing
the class name and hash code while I wanted to print values. Probably
the ease of using TreeSet and other collections, which allow for using
toString to print values did me in. Thanks again
Amit
|
|
|
|
|