/** * Verify the exception type either it is expected to occur or not. * @param alg Algorithm name * @param param DRBG parameter * @param e Exception to verify * @throws NoSuchAlgorithmException */ private static void checkException(String alg, SecureRandomParameters param, NoSuchAlgorithmException e) throws NoSuchAlgorithmException { int strength = ((Instantiation) param).getStrength(); boolean error = true; switch (alg) { case INVALID_ALGO: error = false; break; case "SHA-224": case "SHA-512/224": if (strength > 192) { error = false; } break; case "SHA-256": case "SHA-512/256": case "SHA-384": case "SHA-512": if (strength > 256) { error = false; } break; case "AES-128": case "AES-192": case "AES-256": int algoStrength = Integer.parseInt(alg.substring("AES-".length())); int maxAESStrength = Cipher.getMaxAllowedKeyLength("AES"); if (strength > algoStrength || algoStrength > maxAESStrength) { error = false; } break; } if (error) { throw new RuntimeException("Unknown :", e); } }
/** * Verify the exception type either it is expected to occur or not. * @param alg Algorithm name * @param param DRBG parameter * @param e Exception to verify * @throws NoSuchAlgorithmException */ private static void checkException(String alg, SecureRandomParameters param, NoSuchAlgorithmException e) throws NoSuchAlgorithmException { int strength = ((Instantiation) param).getStrength(); boolean error = true; switch (alg) { case INVALID_ALGO: error = false; break; case "SHA-224": case "SHA-512/224": if (strength > 192) { error = false; } break; case "SHA-256": case "SHA-512/256": case "SHA-384": case "SHA-512": if (strength > 256) { error = false; } break; case "AES-128": case "AES-192": case "AES-256": int algoStrength = Integer.parseInt(alg.replaceAll("AES-", "")); int maxStrengthSupported = Cipher.getMaxAllowedKeyLength("AES"); if (strength > maxStrengthSupported || algoStrength > maxStrengthSupported) { error = false; } break; } if (error) { throw new RuntimeException("Unknown :", e); } }
public static void main(String[] args) throws NoSuchAlgorithmException { Instantiation instantiation = DrbgParameters.instantiation(128, RESEED_ONLY, null); SecureRandom random = SecureRandom.getInstance("DRBG", instantiation); byte[] bytes = new byte[20]; random.nextBytes(bytes); for (byte b : bytes) { System.out.print(b + " "); } System.out.println(); }
private static boolean isSupportPR(String mech, SecureRandom random) { return (isDRBG(mech) && ((Instantiation) random.getParameters()) .getCapability() .supportsPredictionResistance()); }