Search in sources :

Example 11 with DigestException

use of java.security.DigestException in project buck by facebook.

the class DexFile method calcSignature.

/**
     * Calculates the signature for the {@code .dex} file in the
     * given array, and modify the array to contain it.
     *
     * @param bytes {@code non-null;} the bytes of the file
     */
private static void calcSignature(byte[] bytes) {
    MessageDigest md;
    try {
        md = MessageDigest.getInstance("SHA-1");
    } catch (NoSuchAlgorithmException ex) {
        throw new RuntimeException(ex);
    }
    md.update(bytes, 32, bytes.length - 32);
    try {
        int amt = md.digest(bytes, 12, 20);
        if (amt != 20) {
            throw new RuntimeException("unexpected digest write: " + amt + " bytes");
        }
    } catch (DigestException ex) {
        throw new RuntimeException(ex);
    }
}
Also used : DigestException(java.security.DigestException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest)

Example 12 with DigestException

use of java.security.DigestException in project poi by apache.

the class CryptoFunctions method hashPassword.

/**
     * Generalized method for read and write protection hash generation.
     * The difference is, read protection uses the order iterator then hash in the hash loop, whereas write protection
     * uses first the last hash value and then the current iterator value
     *
     * @param password
     * @param hashAlgorithm
     * @param salt
     * @param spinCount
     * @param iteratorFirst if true, the iterator is hashed before the n-1 hash value,
     *        if false the n-1 hash value is applied first
     * @return the hashed password
     */
public static byte[] hashPassword(String password, HashAlgorithm hashAlgorithm, byte[] salt, int spinCount, boolean iteratorFirst) {
    // If no password was given, use the default
    if (password == null) {
        password = Decryptor.DEFAULT_PASSWORD;
    }
    MessageDigest hashAlg = getMessageDigest(hashAlgorithm);
    hashAlg.update(salt);
    byte[] hash = hashAlg.digest(StringUtil.getToUnicodeLE(password));
    byte[] iterator = new byte[LittleEndianConsts.INT_SIZE];
    byte[] first = (iteratorFirst ? iterator : hash);
    byte[] second = (iteratorFirst ? hash : iterator);
    try {
        for (int i = 0; i < spinCount; i++) {
            LittleEndian.putInt(iterator, 0, i);
            hashAlg.reset();
            hashAlg.update(first);
            hashAlg.update(second);
            // don't create hash buffer everytime new
            hashAlg.digest(hash, 0, hash.length);
        }
    } catch (DigestException e) {
        throw new EncryptedDocumentException("error in password hashing");
    }
    return hash;
}
Also used : EncryptedDocumentException(org.apache.poi.EncryptedDocumentException) DigestException(java.security.DigestException) MessageDigest(java.security.MessageDigest)

Example 13 with DigestException

use of java.security.DigestException in project hbase by apache.

the class Encryption method hash256.

/**
   * Return the SHA-256 digest of the concatenation of the supplied arguments.
   */
public static byte[] hash256(byte[]... args) {
    byte[] result = new byte[32];
    try {
        MessageDigest md = MessageDigest.getInstance("SHA-256");
        for (byte[] arg : args) {
            md.update(arg);
        }
        md.digest(result, 0, result.length);
        return result;
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    } catch (DigestException e) {
        throw new RuntimeException(e);
    }
}
Also used : DigestException(java.security.DigestException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest)

Example 14 with DigestException

use of java.security.DigestException in project hbase by apache.

the class Encryption method hash256.

/**
   * Return the SHA-256 digest of the concatenation of the supplied arguments.
   */
public static byte[] hash256(String... args) {
    byte[] result = new byte[32];
    try {
        MessageDigest md = MessageDigest.getInstance("SHA-256");
        for (String arg : args) {
            md.update(Bytes.toBytes(arg));
        }
        md.digest(result, 0, result.length);
        return result;
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    } catch (DigestException e) {
        throw new RuntimeException(e);
    }
}
Also used : DigestException(java.security.DigestException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest)

Example 15 with DigestException

use of java.security.DigestException in project j2objc by google.

the class MessageDigest1Test method testSHAProvider.

/**
     * Tests SHA MessageDigest provider
     */
public void testSHAProvider() {
    MessageDigest md = null;
    try {
        md = MessageDigest.getInstance("SHA");
    } catch (NoSuchAlgorithmException e) {
        fail("unexpected exception: " + e);
    }
    byte[] bytes = new byte[] { 1, 1, 1, 1, 1 };
    // testing combination with provider
    try {
        // offset < 0
        md.update(bytes, -1, 1);
        fail("No expected IndexOutOfBoundsException");
    } catch (IndexOutOfBoundsException e) {
    }
    try {
        md.update(bytes, 1, -1);
        fail("No expected IndexOutOfBoundsException");
    } catch (IndexOutOfBoundsException e) {
    }
    //Regression for Harmony-1148
    try {
        md = MessageDigest.getInstance("SHA");
    } catch (NoSuchAlgorithmException e) {
        fail("unexpected exception: " + e);
    }
    try {
        // offset < 0
        md.digest(bytes, 0, -1);
        fail("No expected DigestException");
    } catch (DigestException e) {
    }
    try {
        // len < 0
        md.digest(bytes, -1, 0);
        fail("No expected DigestException");
    } catch (DigestException e) {
    }
    try {
        md = MessageDigest.getInstance("UnknownDigest");
        fail("expected NoSuchAlgorithmException");
    } catch (NoSuchAlgorithmException e) {
    // ok
    }
}
Also used : DigestException(java.security.DigestException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest)

Aggregations

DigestException (java.security.DigestException)30 MessageDigest (java.security.MessageDigest)18 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)14 IOException (java.io.IOException)7 DataSource (com.android.apksigner.core.util.DataSource)3 ByteBuffer (java.nio.ByteBuffer)2 MyMessageDigest1 (org.apache.harmony.security.tests.support.MyMessageDigest1)2 LimitedLengthInputStream (android.content.pm.LimitedLengthInputStream)1 MacAuthenticatedInputStream (android.content.pm.MacAuthenticatedInputStream)1 ParcelFileDescriptor (android.os.ParcelFileDescriptor)1 ByteBufferDataSource (com.android.apksigner.core.internal.util.ByteBufferDataSource)1 MessageDigestSink (com.android.apksigner.core.internal.util.MessageDigestSink)1 BufferedInputStream (java.io.BufferedInputStream)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 InputStream (java.io.InputStream)1 GeneralSecurityException (java.security.GeneralSecurityException)1 SignatureException (java.security.SignatureException)1 HashMap (java.util.HashMap)1