use of java.security.MessageDigest 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.MessageDigest in project glide by bumptech.
the class EngineKeyTest method testDiffersIfSignatureDiffers.
@Test
public void testDiffersIfSignatureDiffers() throws UnsupportedEncodingException, NoSuchAlgorithmException {
EngineKey first = harness.build();
Key signature = mock(Key.class);
doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocationOnMock) throws Throwable {
MessageDigest digest = (MessageDigest) invocationOnMock.getArguments()[0];
digest.update("signature".getBytes("UTF-8"));
return null;
}
}).when(signature).updateDiskCacheKey(any(MessageDigest.class));
harness.signature = signature;
EngineKey second = harness.build();
KeyAssertions.assertDifferent(first, second, false);
}
use of java.security.MessageDigest in project RPlay by bencall.
the class RTSPResponder method md5Hash.
/**
* Generates md5 hash of a string.
* @param plaintext string
* @return hash string
*/
public String md5Hash(String plaintext) {
String hashtext = "";
try {
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(plaintext.getBytes());
byte[] digest = md.digest();
BigInteger bigInt = new BigInteger(1, digest);
hashtext = bigInt.toString(16);
// Now we need to zero pad it if you actually want the full 32 chars.
while (hashtext.length() < 32) {
hashtext = "0" + hashtext;
}
} catch (java.security.NoSuchAlgorithmException e) {
//
}
return hashtext;
}
use of java.security.MessageDigest 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);
}
}
use of java.security.MessageDigest in project binnavi by google.
the class FileUtils method calcMD5.
/**
* Calculates the MD5 value of a file.
*
* @param file The file in question.
* @return A string that holds the MD5 sum of the file.
*
* @throws IOException
*/
public static String calcMD5(final File file) throws IOException {
// TODO: This method read the entire file in RAM. Make it iterative and
// use a BufferedReader
final FileInputStream reader = new FileInputStream(file);
final byte[] data = new byte[(int) file.length()];
reader.read(data);
reader.close();
final MessageDigest md;
try {
md = MessageDigest.getInstance("MD5");
} catch (final NoSuchAlgorithmException e) {
assert false : "MD5 not in list of algorithms";
throw new RuntimeException(e);
}
md.update(data);
final byte[] digest = md.digest();
final StringBuilder md5 = new StringBuilder();
for (final byte b : digest) {
md5.append(String.format("%02X", b));
}
return md5.toString();
}
Aggregations