Created
May 8, 2015 21:56
-
-
Save lidio601/ba0a90491733b1a284e6 to your computer and use it in GitHub Desktop.
Sample Java class to test the keystore support
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Sample Java class to test the keystore support | |
*/ | |
package net.fabiocigliano.test.keystore; | |
import java.io.File; | |
import java.io.FileInputStream; | |
import java.io.FileNotFoundException; | |
import java.io.FileOutputStream; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.InputStreamReader; | |
import java.math.BigInteger; | |
import java.nio.ByteBuffer; | |
/** | |
* @author fabiocigliano | |
* | |
*/ | |
public final class TestKeyStore { | |
private final static int LENGTH = 20; | |
private final static int ncol = 4; | |
public static byte cast(int num) { | |
return (byte) ( num & 0xFF ); | |
} | |
public static void writeTestKeystore(File filename) throws FileNotFoundException, IOException { | |
FileOutputStream out = new FileOutputStream(filename); | |
for(int i=0; i<LENGTH; i++) { | |
out.write( cast(i) ); | |
} | |
out.flush(); | |
out.close(); | |
} | |
public static void dumpBytes(InputStream is, int length) throws IOException { | |
int buf; | |
int cont = 0; | |
ByteBuffer buffer = ByteBuffer.allocate( length ); | |
while( | |
// (buf=is.read()) != -1 && | |
cont<length | |
) { | |
buf=is.read(); | |
buffer.put( (byte) buf ); | |
// cast(buf) ); | |
cont++; | |
} | |
// System.out.println("reached EOF - End of Stream "+buf); | |
dumpBytes(buffer.array(), length); | |
} | |
private static void dumpBytes(byte[] array, int limit) { | |
StringBuilder b; | |
String temp; | |
for(int i=0; i<array.length && i<limit; ) { | |
b = new StringBuilder(); | |
int tmpi = 0; | |
for(int j=0; j<ncol && i+j<array.length; j++) { | |
b.append( String.format("%1$#" + 12 + "s", array[i+j] ) ); | |
b.append(" "); | |
tmpi = (int) ( (tmpi<<8) | (array[i+j] & 0xff) ); | |
} | |
temp = "0000000000000"+Integer.toHexString( new Integer(tmpi) ); | |
temp = "0x"+temp.substring(temp.length()-8); | |
b.append( String.format("%1$#" + 20 + "s", temp ) ); | |
i = i + ncol; | |
System.out.println(b.toString()); | |
} | |
} | |
private static void dumpInteger(InputStream is, int length) throws IOException { | |
InputStreamReader isr = new InputStreamReader(is); | |
char[] buf = new char[8]; | |
int i=0; | |
while( isr.read(buf)>0 && i < length ) { | |
String temp = new String(buf); | |
BigInteger big = new BigInteger(temp, 16); | |
String s = Integer.toString( big.intValue() ); | |
System.out.print( String.format("%1$#" + 51 + "s", s ) ); | |
System.out.print(" "); | |
System.out.println( String.format("%1$#" + 20 + "s", "0x"+temp ) ); | |
i++; | |
} | |
} | |
/** | |
* @param args | |
*/ | |
public static void main(String[] args) throws Exception { | |
String filename; | |
filename = "cacert.org.bks"; | |
File f = new File("/tmp",filename); | |
if( !f.exists() ) { | |
writeTestKeystore(f); | |
} | |
System.out.println("File di input:"); | |
System.out.println("size: "+f.length()); | |
FileInputStream fis = new FileInputStream(f); | |
dumpBytes(fis, LENGTH); | |
System.out.println("File codificato:"); | |
KeyStoreEncoder encoder = new KeyStoreEncoder(f); | |
System.out.println("size: "+encoder.size()); | |
dumpInteger(encoder, LENGTH/4); | |
FileOutputStream out1 = new FileOutputStream("/tmp/cert_enc.txt"); | |
encoder.reset(); | |
encoder.writeStream(out1); | |
out1.close(); | |
System.out.println("File decodificato:"); | |
encoder.reset(); | |
KeyStoreDecoder decoder = new KeyStoreDecoder(encoder); | |
System.out.println("size: "+decoder.size()); | |
dumpBytes(decoder, LENGTH); | |
FileOutputStream out = new FileOutputStream("/tmp/cert_decr.bks"); | |
decoder.reset(); | |
decoder.writeStream(out); | |
out.close(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment