Example usage for java.security MessageDigest getDigestLength

List of usage examples for java.security MessageDigest getDigestLength

Introduction

In this page you can find the example usage for java.security MessageDigest getDigestLength.

Prototype

public final int getDigestLength() 

Source Link

Document

Returns the length of the digest in bytes, or 0 if this operation is not supported by the provider and the implementation is not cloneable.

Usage

From source file:com.github.joulupunikki.math.random.BitsStreamGenerator64.java

/**
 * Will hash the seed with the SHA-512 digest. Enough bits are generated to
 * fill all the state bits of the generator. The digests will be chained so
 * that each new set of 512 bits receives the digest value of the previous
 * set as initial digest data. This should provide well mixed and
 * uncorrelated initial states with all seeds. Secure hashing is however
 * slower than less involved methods of state initialization.
 *
 * @param seed_in the seed//from  w w  w  . j a v a 2  s.  c  om
 * @return hashed state array of longs
 */
public long[] hashSeed(long[] seed_in) {
    // prepare to hash seed
    int seed_len = seed_in.length;
    long[] seed_out = new long[STATE_WORDS];
    MessageDigest md = null;
    try {
        md = MessageDigest.getInstance("SHA-512");
    } catch (Exception e) { //SHA-512 should be available in java 1.5+
        throw new RuntimeException(null, e);
    }
    int digest_count = (STATE_BITS - 1) / md.getDigestLength() + 1;
    int digest_bytes = md.getDigestLength() / Byte.SIZE;
    if (seed_len > digest_count) {
        seed_len = digest_count;
    }
    byte[] hashed_seed = new byte[STATE_WORDS * Long.BYTES];
    int s = 0;
    byte[] r = null;
    // hash seed
    for (; s < seed_len; s++) {
        r = md.digest(PrimitiveConversion.longToByteArray(seed_in[s]));
        System.arraycopy(r, 0, hashed_seed, s * digest_bytes, digest_bytes);
        md.update(r); // chain digests
    }
    // if out of seed just chain digests
    for (; s < digest_count; s++) {
        r = md.digest();
        System.arraycopy(r, 0, hashed_seed, s * digest_bytes, digest_bytes);
        md.update(r);

    }
    // convert digest bytes to longs
    byte[] t = new byte[Long.BYTES];
    for (int i = 0; i < STATE_WORDS; i++) {
        System.arraycopy(hashed_seed, i * Long.BYTES, t, 0, Long.BYTES);
        seed_out[i] = PrimitiveConversion.byteArrayToLong(t);
    }
    return seed_out;
}

From source file:org.panbox.desktop.common.sharemgmt.ShareManagerImpl.java

private byte[] getOwnerFpFromMessageDigest(File ownerFile, MessageDigest md) throws IOException {
    byte[] ownerFp = new byte[md.getDigestLength()];
    try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(ownerFile))) {
        bis.read(ownerFp);/*w w w.  j  a  v a 2s  . co  m*/
    }
    return ownerFp;
}

From source file:org.lockss.hasher.TestSimpleHasher.java

public void testMakeDigest() throws Exception {
    HasherResult result = new HasherResult();
    MessageDigest digest = new SimpleHasher(null)
            .makeDigestAndRecordStream(LcapMessage.getDefaultHashAlgorithm(), false, result);

    assertTrue(digest instanceof MessageDigest);
    assertFalse(digest instanceof RecordingMessageDigest);
    assertEquals(LcapMessage.getDefaultHashAlgorithm(), digest.getAlgorithm());
    assertEquals(20, digest.getDigestLength());
    assertNull(result.getRecordFile());//  ww  w.j  a v a2  s.c o m
    assertNull(result.getRecordStream());

    result = new HasherResult();
    digest = new SimpleHasher(null).makeDigestAndRecordStream(LcapMessage.getDefaultHashAlgorithm(), true,
            result);

    assertTrue(digest instanceof MessageDigest);
    assertTrue(digest instanceof RecordingMessageDigest);
    assertEquals("SHA-1", digest.getAlgorithm());
    assertEquals(20, digest.getDigestLength());
    assertTrue(result.getRecordFile().getName().startsWith("HashCUS"));
    assertTrue(result.getRecordFile().getName().endsWith(".tmp"));
    assertNotNull(result.getRecordStream());
    // Clean up the result file.
    result.getRecordFile().delete();
    IOUtil.safeClose(result.getRecordStream());

    result = new HasherResult();
    digest = new SimpleHasher(null).makeDigestAndRecordStream("SHA", false, result);

    assertTrue(digest instanceof MessageDigest);
    assertFalse(digest instanceof RecordingMessageDigest);
    assertEquals("SHA", digest.getAlgorithm());
    assertEquals(20, digest.getDigestLength());
    assertNull(result.getRecordFile());
    assertNull(result.getRecordStream());

    result = new HasherResult();
    digest = new SimpleHasher(null).makeDigestAndRecordStream("SHA1", false, result);

    assertTrue(digest instanceof MessageDigest);
    assertFalse(digest instanceof RecordingMessageDigest);
    assertEquals("SHA1", digest.getAlgorithm());
    assertEquals(20, digest.getDigestLength());
    assertNull(result.getRecordFile());
    assertNull(result.getRecordStream());

    result = new HasherResult();
    digest = new SimpleHasher(null).makeDigestAndRecordStream("MD5", false, result);

    assertTrue(digest instanceof MessageDigest);
    assertFalse(digest instanceof RecordingMessageDigest);
    assertEquals("MD5", digest.getAlgorithm());
    assertEquals(16, digest.getDigestLength());
    assertNull(result.getRecordFile());
    assertNull(result.getRecordStream());

    result = new HasherResult();
    digest = new SimpleHasher(null).makeDigestAndRecordStream("SHA-256", false, result);

    assertTrue(digest instanceof MessageDigest);
    assertFalse(digest instanceof RecordingMessageDigest);
    assertEquals("SHA-256", digest.getAlgorithm());
    assertEquals(32, digest.getDigestLength());
    assertNull(result.getRecordFile());
    assertNull(result.getRecordStream());

    try {
        digest = new SimpleHasher(null).makeDigestAndRecordStream(null, false, result);
        fail("Null algorithm should throw NullPointerException");
    } catch (NullPointerException npe) {
        // Expected.
    }

    try {
        digest = new SimpleHasher(null).makeDigestAndRecordStream("SHA256", false, result);
        fail("Invalid algorithm should throw NoSuchAlgorithmException");
    } catch (NoSuchAlgorithmException nsae) {
        // Expected.
    }

    try {
        digest = new SimpleHasher(null).makeDigestAndRecordStream("FGL", false, result);
        fail("Invalid algorithm should throw NoSuchAlgorithmException");
    } catch (NoSuchAlgorithmException nsae) {
        // Expected.
    }
}