// Create HMAC of the current time, user, and uid import java.io.*; import javax.crypto.*; import javax.crypto.spec.*; public class IOticket { /* hex conversion code adapted from cryptix.util.core.Hex */ private static final char[] hexDigits = { '0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f' }; // convert byte array to hex // public static String hex(byte[] ba) { char[] buf = new char[ba.length * 2]; int j = 0; int k; for (int i = 0; i < ba.length; i++) { k = ba[i]; buf[j++] = hexDigits[(k >>> 4) & 0x0F]; buf[j++] = hexDigits[ k & 0x0F]; } return new String(buf); } public static void main( String[] args) throws Exception { if( args.length != 2) { System.err.println( "Usage: IOticket user uid"); System.exit(1); } String user = args[0]; String uid = args[1]; if( user.length() < 1 || uid.length() < 1) throw new IllegalArgumentException( "user or uid is empty"); FileInputStream fin = new FileInputStream( "secret.dat"); byte secret[] = new byte[64]; int n = fin.read( secret); fin.close(); if( n < 64) throw new SecurityException( "secret.dat is too small"); SecretKeySpec KS = new SecretKeySpec( secret, "HmacSHA256"); Mac mac = Mac.getInstance( "HmacSHA256"); mac.init( KS); String time = Long.toString(System.currentTimeMillis()); mac.update( time.getBytes()); mac.update( user.getBytes()); mac.update( uid.getBytes()); String ticket = hex( mac.doFinal()); System.out.println( ""); System.out.println( ""); System.out.println( ""); System.out.println( ""); } }