blob: cee5623528e5711636eafba937621fa3ee2fb27c (
plain) (
tree)
|
|
package org.uic.barcode.test.utils;
import java.security.InvalidAlgorithmParameterException;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.SecureRandom;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.bouncycastle.jce.ECNamedCurveTable;
import org.bouncycastle.jce.spec.ECParameterSpec;
public class TestUtils {
final protected static char[] hexArray = "0123456789ABCDEF".toCharArray();
public static String hexStringFromBytes(byte[] bytes) {
char[] hexChars = new char[bytes.length * 2];
for (int j = 0; j < bytes.length; j++) {
int v = bytes[j] & 0xFF;
hexChars[j * 2] = hexArray[v >>> 4];
hexChars[j * 2 + 1] = hexArray[v & 0x0F];
}
return new String(hexChars);
}
/* s must be an even-length string. */
public static byte[] hexStringToByteArray(String s) {
int len = s.length();
byte[] data = new byte[len / 2];
for (int i = 0; i < len; i += 2) {
data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
+ Character.digit(s.charAt(i+1), 16));
}
return data;
}
public static Date parseDate (String source){
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
try {
return formatter.parse(source);
} catch (ParseException e) {
try {
return formatter.parse("2001-01-01");
} catch (ParseException e1) {
return null;
}
}
}
/**
* Generate DSA keys.
*
* @return the key pair
* @throws NoSuchAlgorithmException the no such algorithm exception
* @throws NoSuchProviderException the no such provider exception
* @throws InvalidAlgorithmParameterException the invalid algorithm parameter exception
*/
public static KeyPair generateDSAKeys(int keySize) throws NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException{
KeyPairGenerator g = KeyPairGenerator.getInstance("DSA", "BC");
g.initialize(keySize, new SecureRandom());
return g.generateKeyPair();
}
public static KeyPair generateECKeys(String keyAlgorithmOid, String curve) throws Exception{
String keyAlgorithmName = "ECDSA";
ECParameterSpec ecSpec = ECNamedCurveTable.getParameterSpec(curve);
KeyPairGenerator g = KeyPairGenerator.getInstance(keyAlgorithmName, "BC");
g.initialize(ecSpec, new SecureRandom());
return g.generateKeyPair();
}
}
|