use of java.security.NoSuchAlgorithmException in project keywhiz by square.
the class ContentCryptographer method computeHmac.
public String computeHmac(byte[] data) {
SecretKey hmacKey = deriveKey(32, "hmackey");
try {
Mac mac = Mac.getInstance("HmacSHA256");
mac.init(hmacKey);
return BaseEncoding.base16().encode(mac.doFinal(data));
} catch (NoSuchAlgorithmException | InvalidKeyException e) {
logger.warn("Error computing HMAC: ", e);
return null;
}
}
use of java.security.NoSuchAlgorithmException in project keywhiz by square.
the class ContentCryptographer method gcm.
private byte[] gcm(Mode mode, String info, byte[] nonce, byte[] data) {
try {
Cipher cipher = Cipher.getInstance(ENCRYPTION_ALGORITHM, encryptionProvider);
SecretKey derivedKey = deriveKey(cipher.getBlockSize(), info);
GCMParameterSpec gcmParameters = new GCMParameterSpec(TAG_BITS, nonce);
cipher.init(mode.cipherMode, derivedKey, gcmParameters);
return cipher.doFinal(data);
} catch (IllegalBlockSizeException | InvalidAlgorithmParameterException | NoSuchPaddingException | NoSuchAlgorithmException | InvalidKeyException | BadPaddingException e) {
throw Throwables.propagate(e);
}
}
use of java.security.NoSuchAlgorithmException in project keywhiz by square.
the class ExpirationExtractor method expirationFromKeystore.
@Nullable
public static Instant expirationFromKeystore(String type, String password, byte[] content) {
KeyStore ks;
try {
ks = KeyStore.getInstance(type);
} catch (KeyStoreException e) {
// Should never occur (assuming JCE is installed)
throw Throwables.propagate(e);
}
try {
ks.load(new ByteArrayInputStream(content), password.toCharArray());
} catch (IOException | NoSuchAlgorithmException | CertificateException e) {
// Failed to parse
logger.info("Failed to parse keystore", e);
return null;
}
Instant earliest = null;
try {
for (String alias : list(ks.aliases())) {
Certificate[] chain = ks.getCertificateChain(alias);
if (chain == null) {
Certificate certificate = ks.getCertificate(alias);
if (certificate == null) {
// No certs in this entry
continue;
}
chain = new Certificate[] { certificate };
}
for (Certificate cert : chain) {
if (cert instanceof X509Certificate) {
X509Certificate c = (X509Certificate) cert;
if (earliest == null || c.getNotAfter().toInstant().isBefore(earliest)) {
earliest = c.getNotAfter().toInstant();
}
}
}
}
} catch (KeyStoreException e) {
// Should never occur (ks was initialized)
throw Throwables.propagate(e);
}
return earliest;
}
use of java.security.NoSuchAlgorithmException in project keywhiz by square.
the class HttpClients method testSslClient.
/**
* Create a {@link OkHttpClient} for tests.
*
* @param keyStore Use a client certificate from keystore if present. Client certs disabled if null.
* @param keyStorePassword keyStore password. Client certs disabled if null.
* @param requestInterceptors Any request interceptors to register with client.
* @return new http client
*/
private static OkHttpClient testSslClient(@Nullable KeyStore keyStore, @Nullable String keyStorePassword, KeyStore trustStore, List<Interceptor> requestInterceptors) {
boolean usingClientCert = keyStore != null && keyStorePassword != null;
SSLContext sslContext;
try {
SSLContextBuilder sslContextBuilder = new SSLContextBuilder().useProtocol("TLSv1.2").loadTrustMaterial(trustStore);
if (usingClientCert) {
sslContextBuilder.loadKeyMaterial(keyStore, keyStorePassword.toCharArray());
}
sslContext = sslContextBuilder.build();
} catch (NoSuchAlgorithmException | KeyStoreException | UnrecoverableKeyException | KeyManagementException e) {
throw Throwables.propagate(e);
}
OkHttpClient.Builder client = new OkHttpClient().newBuilder().sslSocketFactory(sslContext.getSocketFactory()).connectionSpecs(Arrays.asList(ConnectionSpec.MODERN_TLS)).followSslRedirects(false);
client.followRedirects(false);
client.retryOnConnectionFailure(false);
// Won't use cookies and a client certificate at once.
if (!usingClientCert) {
CookieManager cookieManager = new CookieManager();
cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
client.cookieJar(new JavaNetCookieJar(cookieManager));
}
for (Interceptor interceptor : requestInterceptors) {
client.networkInterceptors().add(interceptor);
}
return client.build();
}
use of java.security.NoSuchAlgorithmException in project android-common by litesuits.
the class MD5Util method md5.
public static byte[] md5(byte[] bytes) {
try {
MessageDigest digest = getDigest("MD5");
digest.update(bytes);
return digest.digest();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return null;
}
Aggregations