public static final void main(String[] args) throws Exception { // Generate a PBE key SecretKeyFactory skFac = SecretKeyFactory.getInstance("PBE"); SecretKey skey = skFac.generateSecret(new PBEKeySpec("test123".toCharArray())); // Initialize the PBE cipher Cipher cipher = Cipher.getInstance(ALGO); cipher.init(Cipher.ENCRYPT_MODE, skey); // Permit access to the Cipher.spi field (a CipherSpi object) Field spi = Cipher.class.getDeclaredField("spi"); spi.setAccessible(true); Object value = spi.get(cipher); // Permit access to the CipherSpi.engineGetKeySize method Method engineGetKeySize = PKCS12PBECipherCore$PBEWithSHA1AndDESede.class.getDeclaredMethod( "engineGetKeySize", Key.class); engineGetKeySize.setAccessible(true); // Check the key size int keySize = (int) engineGetKeySize.invoke(value, skey); if (keySize == KEYSIZE) { System.out.println(ALGO + ".engineGetKeySize returns " + keySize + " bits, as expected"); System.out.println("OK"); } else { throw new Exception("ERROR: " + ALGO + " key size is incorrect"); } }
/** * Copy from the copy method in StructUtil. Did not want to drag that code in. maybe this actually * should go to struct. * * @param from * @param to * @param excludes * @return * @throws Exception */ public static <T extends struct> T xcopy(struct from, T to, String... excludes) throws Exception { Arrays.sort(excludes); for (Field f : from.fields()) { if (Arrays.binarySearch(excludes, f.getName()) >= 0) continue; Object o = f.get(from); if (o == null) continue; Field tof = to.getField(f.getName()); if (tof != null) try { tof.set(to, Converter.cnv(tof.getGenericType(), o)); } catch (Exception e) { System.out.println( "Failed to convert " + f.getName() + " from " + from.getClass() + " to " + to.getClass() + " value " + o + " exception " + e); } } return to; }
/** * Parses a field type definition * * @param docField * @return */ protected static Field ParseField(FieldDoc docField) { assert (docField != null); Field xmlField = new Field(); xmlField.name = docField.name(); xmlField.comment = docField.commentText(); xmlField.type = ParseType(docField.type()); xmlField.isFinal = docField.isFinal(); if (xmlField.isFinal) { xmlField.finalExpression = docField.constantValueExpression(); } else if (docField.constantValueExpression() != null) { // how would a non-final field have a constant value expression? // my understanding is that this field is only != null when is not final assert (false); } xmlField.isStatic = docField.isStatic(); xmlField.isVolatile = docField.isVolatile(); xmlField.isTransient = docField.isTransient(); xmlField.scope = DetermineScope(docField); // parse annotations from the field xmlField.annotationInstances = ParseAnnotationInstances(docField.annotations(), docField.qualifiedName()); return xmlField; }
public static Object getAdapterSelf(Class<?> adapterClass, Object adapter) throws NoSuchFieldException, IllegalAccessException { Field self = adapterClass.getDeclaredField("self"); return self.get(adapter); }