KeyStore, HttpClient, and HTTPS: Can someone explain this code to me?

Posted by stormin986 on Stack Overflow See other posts from Stack Overflow or by stormin986
Published on 2010-04-29T23:44:45Z Indexed on 2010/04/30 0:17 UTC
Read the original article Hit count: 260

Filed under:
|
|
|

I'm trying to understand what's going on in this code.

KeyStore trustStore  = KeyStore.getInstance(KeyStore.getDefaultType());        
FileInputStream instream = new FileInputStream(new File("my.keystore")); 
try {
    trustStore.load(instream, "nopassword".toCharArray());
} finally {
    instream.close();
}

SSLSocketFactory socketFactory = new SSLSocketFactory(trustStore);
Scheme sch = new Scheme("https", socketFactory, 443);
httpclient.getConnectionManager().getSchemeRegistry().register(sch);

My Questions:

trustStore.load(instream, "nopassword".toCharArray()); is doing what exactly? From reading the documentation load() will load KeyStore data from an input stream (which is just an empty file we just created), using some arbitrary "nopassword". Why not just load it with null as the InputStream parameter and an empty string as the password field?

And then what is happening when this empty KeyStore is being passed to the SSLSocketFactory constructor? What's the result of such an operation?

Or -- is this simply an example where in a real application you would have to actually put a reference to an existing keystore file / password?

© Stack Overflow or respective owner

Related posts about java

Related posts about keystore