use of java.security.NoSuchAlgorithmException in project hazelcast by hazelcast.
the class MD5Util method toMD5String.
/**
* Converts given string to MD5 hash
*
* @param str str to be hashed with MD5
*/
@SuppressWarnings("checkstyle:magicnumber")
public static String toMD5String(String str) {
try {
MessageDigest md = MessageDigest.getInstance("MD5");
if (md == null || str == null) {
return "NULL";
}
byte[] byteData = md.digest(str.getBytes(Charset.forName("UTF-8")));
StringBuilder sb = new StringBuilder();
for (byte aByteData : byteData) {
sb.append(Integer.toString((aByteData & 0xff) + 0x100, 16).substring(1));
}
return sb.toString();
} catch (NoSuchAlgorithmException ignored) {
EmptyStatement.ignore(ignored);
return null;
}
}
use of java.security.NoSuchAlgorithmException in project buck by facebook.
the class Dex method computeSignature.
/**
* Returns the signature of all but the first 32 bytes of this dex. The
* first 32 bytes of dex files are not specified to be included in the
* signature.
*/
public byte[] computeSignature() throws IOException {
MessageDigest digest;
try {
digest = MessageDigest.getInstance("SHA-1");
} catch (NoSuchAlgorithmException e) {
throw new AssertionError();
}
byte[] buffer = new byte[8192];
// positioned ByteBuffers aren't thread safe
ByteBuffer data = this.data.duplicate();
data.limit(data.capacity());
data.position(SIGNATURE_OFFSET + SIGNATURE_SIZE);
while (data.hasRemaining()) {
int count = Math.min(buffer.length, data.remaining());
data.get(buffer, 0, count);
digest.update(buffer, 0, count);
}
return digest.digest();
}
use of java.security.NoSuchAlgorithmException in project spring-security-oauth by spring-projects.
the class HMAC_SHA1SignatureMethod method sign.
/**
* Sign the signature base string. The signature is the digest octet string, first base64-encoded per RFC2045, section 6.8, then URL-encoded per
* OAuth Parameter Encoding.
*
* @param signatureBaseString The signature base string.
* @return The signature.
*/
public String sign(String signatureBaseString) {
try {
Mac mac = Mac.getInstance(MAC_NAME);
mac.init(key);
byte[] text = signatureBaseString.getBytes("UTF-8");
byte[] signatureBytes = mac.doFinal(text);
signatureBytes = Base64.encodeBase64(signatureBytes);
String signature = new String(signatureBytes, "UTF-8");
if (LOG.isDebugEnabled()) {
LOG.debug("signature base: " + signatureBaseString);
LOG.debug("signature: " + signature);
}
return signature;
} catch (NoSuchAlgorithmException e) {
throw new IllegalStateException(e);
} catch (InvalidKeyException e) {
throw new IllegalStateException(e);
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
}
use of java.security.NoSuchAlgorithmException in project spring-security by spring-projects.
the class CryptoAssumptions method assumeAes256.
private static void assumeAes256(CipherAlgorithm cipherAlgorithm) {
boolean aes256Available = false;
try {
Cipher.getInstance(cipherAlgorithm.toString());
aes256Available = Cipher.getMaxAllowedKeyLength("AES") >= 256;
} catch (NoSuchAlgorithmException e) {
throw new AssumptionViolatedException(cipherAlgorithm + " not available, skipping test", e);
} catch (NoSuchPaddingException e) {
throw new AssumptionViolatedException(cipherAlgorithm + " padding not available, skipping test", e);
}
Assume.assumeTrue("AES key length of 256 not allowed, skipping test", aes256Available);
}
use of java.security.NoSuchAlgorithmException in project spring-security-oauth by spring-projects.
the class DefaultAuthenticationKeyGenerator method generateKey.
protected String generateKey(Map<String, String> values) {
MessageDigest digest;
try {
digest = MessageDigest.getInstance("MD5");
byte[] bytes = digest.digest(values.toString().getBytes("UTF-8"));
return String.format("%032x", new BigInteger(1, bytes));
} catch (NoSuchAlgorithmException nsae) {
throw new IllegalStateException("MD5 algorithm not available. Fatal (should be in the JDK).", nsae);
} catch (UnsupportedEncodingException uee) {
throw new IllegalStateException("UTF-8 encoding not available. Fatal (should be in the JDK).", uee);
}
}
Aggregations