use of java.security.NoSuchAlgorithmException in project android-async-http by loopj.
the class CustomCASample method onCreate.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {
InputStream is = null;
try {
// Configure the library to use a custom 'bks' file to perform
// SSL negotiation.
KeyStore store = KeyStore.getInstance(KeyStore.getDefaultType());
is = getResources().openRawResource(R.raw.store);
store.load(is, STORE_PASS.toCharArray());
getAsyncHttpClient().setSSLSocketFactory(new SecureSocketFactory(store, STORE_ALIAS));
} catch (IOException e) {
throw new KeyStoreException(e);
} catch (CertificateException e) {
throw new KeyStoreException(e);
} catch (NoSuchAlgorithmException e) {
throw new KeyStoreException(e);
} catch (KeyManagementException e) {
throw new KeyStoreException(e);
} catch (UnrecoverableKeyException e) {
throw new KeyStoreException(e);
} finally {
AsyncHttpClient.silentCloseInputStream(is);
}
} catch (KeyStoreException e) {
Log.e(LOG_TAG, "Unable to initialize key store", e);
showCustomCAHelp();
}
}
use of java.security.NoSuchAlgorithmException in project camel by apache.
the class XMLSecurityDataFormat method generateKeyEncryptionKey.
private Key generateKeyEncryptionKey(String algorithm) throws InvalidKeyException, NoSuchAlgorithmException, InvalidKeySpecException {
DESedeKeySpec keySpec;
Key secretKey;
try {
if (algorithm.equalsIgnoreCase("DESede")) {
keySpec = new DESedeKeySpec(passPhrase);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(algorithm);
secretKey = keyFactory.generateSecret(keySpec);
} else if (algorithm.equalsIgnoreCase("SEED")) {
secretKey = new SecretKeySpec(passPhrase, "SEED");
} else if (algorithm.equalsIgnoreCase("CAMELLIA")) {
secretKey = new SecretKeySpec(passPhrase, "CAMELLIA");
} else {
secretKey = new SecretKeySpec(passPhrase, "AES");
}
if (Arrays.equals(passPhrase, DEFAULT_KEY.getBytes())) {
LOG.warn("Using the default encryption key is not secure");
}
} catch (InvalidKeyException e) {
throw new InvalidKeyException("InvalidKeyException due to invalid passPhrase: " + Arrays.toString(passPhrase));
} catch (NoSuchAlgorithmException e) {
throw new NoSuchAlgorithmException("NoSuchAlgorithmException while using algorithm: " + algorithm);
} catch (InvalidKeySpecException e) {
throw new InvalidKeySpecException("Invalid Key generated while using passPhrase: " + Arrays.toString(passPhrase));
}
return secretKey;
}
use of java.security.NoSuchAlgorithmException in project camel by apache.
the class SignatureDigestMethodTest method getKeyPair.
public static KeyPair getKeyPair(String algorithm, int keylength) {
KeyPairGenerator keyGen;
try {
keyGen = KeyPairGenerator.getInstance(algorithm);
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
keyGen.initialize(keylength, new SecureRandom());
return keyGen.generateKeyPair();
}
use of java.security.NoSuchAlgorithmException in project camel by apache.
the class DefaultKeySelector method select.
public KeySelectorResult select(KeyInfo keyInfo, KeySelector.Purpose purpose, AlgorithmMethod method, XMLCryptoContext context) throws KeySelectorException {
if (keyStoreAndAlias.getKeyStore() == null) {
return getNullKeyResult();
}
if (keyStoreAndAlias.getAlias() == null) {
return getNullKeyResult();
}
if (KeySelector.Purpose.VERIFY.equals(purpose)) {
Certificate cert;
try {
cert = keyStoreAndAlias.getKeyStore().getCertificate(keyStoreAndAlias.getAlias());
} catch (KeyStoreException e) {
throw new KeySelectorException(e);
}
if (cert == null) {
return getNullKeyResult();
}
final Key key = cert.getPublicKey();
return getKeySelectorResult(key);
} else if (KeySelector.Purpose.SIGN.equals(purpose)) {
if (keyStoreAndAlias.getPassword() == null) {
return getNullKeyResult();
}
Key key;
try {
if (this.getCamelContext() != null && keyStoreAndAlias.getPassword() != null) {
try {
String passwordProperty = this.getCamelContext().resolvePropertyPlaceholders(new String(keyStoreAndAlias.getPassword()));
key = keyStoreAndAlias.getKeyStore().getKey(keyStoreAndAlias.getAlias(), passwordProperty.toCharArray());
} catch (Exception e) {
throw new RuntimeCamelException("Error parsing property value: " + new String(keyStoreAndAlias.getPassword()), e);
}
} else {
key = keyStoreAndAlias.getKeyStore().getKey(keyStoreAndAlias.getAlias(), keyStoreAndAlias.getPassword());
}
} catch (UnrecoverableKeyException e) {
throw new KeySelectorException(e);
} catch (KeyStoreException e) {
throw new KeySelectorException(e);
} catch (NoSuchAlgorithmException e) {
throw new KeySelectorException(e);
}
return getKeySelectorResult(key);
} else {
throw new IllegalStateException("Purpose " + purpose + " not supported");
}
}
use of java.security.NoSuchAlgorithmException in project databus by linkedin.
the class RegistrationIdGenerator method generateByteHash.
/**
* Generate a hash out of the String id
*
* @param id
* @return
*/
private static String generateByteHash(String id) {
try {
final MessageDigest messageDigest = MessageDigest.getInstance("MD5");
messageDigest.reset();
messageDigest.update(id.getBytes(Charset.forName("UTF8")));
final byte[] resultsByte = messageDigest.digest();
String hash = new String(Hex.encodeHex(resultsByte));
final int length = 8;
if (hash.length() > length)
hash = hash.substring(0, length);
return hash;
} catch (NoSuchAlgorithmException nse) {
LOG.error("Unexpected error : Got NoSuchAlgorithm exception for MD5");
return "";
}
}
Aggregations