KeychainStore, more fun with Runtime, for the archives
Subject : KeychainStore, more fun with Runtime, for the archives
From: Michael Hall <email@hidden >
Date: Sun, 8 Jan 2006 18:02:59 -0600
Delivered-to: email@hidden
Delivered-to: email@hidden
As near as I can tell the documentation on this pretty much consists
of this single line...
KeyStore ks = KeyStore.getInstance("KeychainStore", "Apple");
A little searching and I came up with...
import java.io.*;
import java.security.*;
import java.security.cert.*;
import java.util.Enumeration;
import java.util.StringTokenizer;
public class KeychainStoreTest {
public static void main(String[] args) {
try {
KeyStore ks = KeyStore.getInstance("KeychainStore","Apple");
String path = rtexec(new String[] { "Security","default-keychain" });
path = path.substring(path.indexOf("\"")+1,path.lastIndexOf("\""));
System.out.println(path);
ks.load(new FileInputStream(path),null);
Enumeration e = ks.aliases();
while (e.hasMoreElements())
System.out.println(e.nextElement());
}
catch (Exception ex) { ex.printStackTrace(); }
}
private static String rtexec(String[] args) {
try {
StringBuffer execout = new StringBuffer();
Process proc = Runtime.getRuntime().exec(args);
proc.waitFor();
InputStream inout = proc.getInputStream();
InputStream inerr = proc.getErrorStream();
byte []buffer = new byte[256];
while (true) {
int stderrLen = inerr.read(buffer, 0, buffer.length);
if (stderrLen > 0) {
execout.append(new String(buffer,0,stderrLen));
}
int stdoutLen = inout.read(buffer, 0, buffer.length);
if (stdoutLen > 0) {
execout.append(new String(buffer,0,stdoutLen));
}
if (stderrLen < 0 && stdoutLen < 0)
break;
}
return execout.toString();
}
catch(Throwable tossed) { tossed.printStackTrace(); }
return "-";
}
}
To fill it out a bit.
The security command looks like it is intended to replace certtool
eventually, it also looks like a little more meta in it's handling of
the keychain files themselves.
Anyhow, possibly having this in archives will save someone a little
of the same searching.
I doesn't look to handle the internationalization characters real
nicely. Any quick suggestions to clean that up would be appreciated.
Mike Hall mikehall at spacestar dot net
http://www.spacestar.net/users/mikehall
http://sourceforge.net/projects/macnative
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Java-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/java-dev/email@hidden
This email sent to email@hidden
Visit the Apple Store online or at retail locations.
1-800-MY-APPLE
Contact Apple | Terms of Use | Privacy Policy
Copyright © 2007 Apple Inc. All rights reserved.