Search in sources :

Example 56 with MessageDigest

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();
}
Also used : NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest) ByteBuffer(java.nio.ByteBuffer)

Example 57 with MessageDigest

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);
}
Also used : InvocationOnMock(org.mockito.invocation.InvocationOnMock) MessageDigest(java.security.MessageDigest) Key(com.bumptech.glide.load.Key) Test(org.junit.Test)

Example 58 with MessageDigest

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;
}
Also used : BigInteger(java.math.BigInteger) MessageDigest(java.security.MessageDigest)

Example 59 with MessageDigest

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);
    }
}
Also used : BigInteger(java.math.BigInteger) UnsupportedEncodingException(java.io.UnsupportedEncodingException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest)

Example 60 with MessageDigest

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();
}
Also used : NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest) FileInputStream(java.io.FileInputStream)

Aggregations

MessageDigest (java.security.MessageDigest)1237 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)613 IOException (java.io.IOException)176 UnsupportedEncodingException (java.io.UnsupportedEncodingException)102 BigInteger (java.math.BigInteger)101 InputStream (java.io.InputStream)72 FileInputStream (java.io.FileInputStream)70 File (java.io.File)62 DigestInputStream (java.security.DigestInputStream)61 Test (org.junit.Test)61 ByteArrayOutputStream (java.io.ByteArrayOutputStream)51 DigestOutputStream (java.security.DigestOutputStream)45 ArrayList (java.util.ArrayList)37 ByteArrayInputStream (java.io.ByteArrayInputStream)31 X509Certificate (java.security.cert.X509Certificate)29 OutputStream (java.io.OutputStream)28 GeneralSecurityException (java.security.GeneralSecurityException)25 Cipher (javax.crypto.Cipher)25 SecretKeySpec (javax.crypto.spec.SecretKeySpec)25 Provider (java.security.Provider)22