Example usage for org.bouncycastle.crypto.digests MD5Digest MD5Digest

List of usage examples for org.bouncycastle.crypto.digests MD5Digest MD5Digest

Introduction

In this page you can find the example usage for org.bouncycastle.crypto.digests MD5Digest MD5Digest.

Prototype

public MD5Digest() 

Source Link

Document

Standard constructor

Usage

From source file:aff4.storage.zip.StreamWriter.java

License:Open Source License

public StreamWriter(WritableZipVolume v, ZipOutputStream zipfile) {
    URN = Node.createURI("urn:aff4:" + UUID.randomUUID().toString());
    volume = v;/*from   w  ww.  j av  a 2  s  .com*/
    this.zipfile = zipfile;

    /*
     * String chunk_size_s = volume.queryValue(urn, "chunk_size"); if
     * (chunk_size_s != null) { chunk_size = Integer.parseInt(chunk_size_s);
     * } String chunks_in_segment_s = volume.queryValue(urn,
     * "chunks_in_segment"); if (chunks_in_segment_s != null) {
     * chunks_in_segment = Integer.parseInt(chunks_in_segment_s); }
     */

    bevy_size = chunk_size * chunks_in_segment;
    buf = ByteBuffer.allocate((int) bevy_size);
    bevvy = ByteBuffer.allocate((int) bevy_size);
    bevvy_index = ByteBuffer.allocate((chunks_in_segment + 2) * 4);
    hash = new HashDigestAdapter(new MD5Digest());
}

From source file:android.core.CryptoTest.java

License:Apache License

/**
 * Tests the MD5 implementation.// w w w  .  jav a2 s  .  c om
 */
@MediumTest
public void testMD5() {
    Digest oldDigest = new MD5Digest();
    Digest newDigest = OpenSSLMessageDigest.getInstance("MD5");
    doTestMessageDigest(oldDigest, newDigest);
}

From source file:ch.rgw.io.FileTool.java

License:Open Source License

/**
 * TODO: Kommentar/*  ww  w  . ja v a2s .  co  m*/
 * 
 * @param file
 * @return
 */
public static byte[] checksum(File file) {
    MD5Digest md5 = new MD5Digest();
    try {
        FileInputStream in = new FileInputStream(file);
        byte[] arr = new byte[65535];
        int num;
        do {
            num = in.read(arr);
            if (num == -1) {
                break;
            }
            md5.update(arr, 0, num);
        } while (num == arr.length);
        byte[] ret = new byte[16];
        md5.doFinal(ret, 0);
        return ret;
    } catch (Exception ex) {
        ExHandler.handle(ex);
        return null;
    }
}

From source file:com.DSC.crypto.Cipher.java

License:Open Source License

/**
 * /*from  ww  w .j a v  a 2 s .  c  o m*/
 * @param passphrase
 * @param data
 * @return
 */
public static BigInteger[] generateHMAC(String passphrase, byte[] data) {
    HMac hmac = new HMac(new MD5Digest());
    byte[] buf = new byte[hmac.getMacSize()];
    BigInteger[] hmacBigInt = new BigInteger[1];

    /* Initializes and generate HMAC for message */
    hmac.init(new KeyParameter(passphrase.getBytes()));
    hmac.update(data, 0, data.length);
    hmac.doFinal(buf, 0);

    /* Convert the HMAC to a big integer representation */
    hmacBigInt[0] = new BigInteger(buf);
    return hmacBigInt;
}

From source file:com.DSC.crypto.Cipher.java

License:Open Source License

/**
 * //  w ww. ja  v a  2s.c om
 * @param passphrase
 * @param HMAC
 * @param data
 * @return
 * @throws InvalidCipherTextException
 */
public static boolean verifyHMAC(String passphrase, BigInteger[] HMAC, byte[] data)
        throws InvalidCipherTextException {
    HMac hmac = new HMac(new MD5Digest());
    byte[] expHMAC = new byte[hmac.getMacSize()];
    byte[] recHMAC = new byte[hmac.getMacSize()];

    /* Initializes and generate the expected HMAC for message */
    hmac.init(new KeyParameter(passphrase.getBytes()));
    hmac.update(data, 0, data.length);
    hmac.doFinal(expHMAC, 0);

    /* Convert the received HMAC to a byte representation */
    recHMAC = HMAC[0].toByteArray();

    /* Compare the HMAC received to the expected HMAC */
    if (Arrays.equals(expHMAC, recHMAC)) {
        return true;
    } else {
        throw new InvalidCipherTextException("Message HMAC failed!");
    }
}

From source file:com.healthmarketscience.jackcess.impl.MSISAMCryptCodecHandler.java

License:Apache License

private static byte[] createPasswordDigest(ByteBuffer buffer, String password, Charset charset) {
    Digest digest = (((buffer.get(ENCRYPTION_FLAGS_OFFSET) & USE_SHA1) != 0) ? new SHA1Digest()
            : new MD5Digest());

    byte[] passwordBytes = new byte[PASSWORD_LENGTH];

    if (password != null) {
        ByteBuffer bb = ColumnImpl.encodeUncompressedText(password.toUpperCase(), charset);
        bb.get(passwordBytes, 0, Math.min(passwordBytes.length, bb.remaining()));
    }/*w w w  . j a  v a 2s.  co m*/

    // Get digest value
    byte[] digestBytes = hash(digest, passwordBytes, PASSWORD_DIGEST_LENGTH);
    return digestBytes;
}

From source file:com.healthmarketscience.jackcess.impl.office.OfficeBinaryDocRC4Provider.java

License:Apache License

@Override
protected Digest initDigest() {
    return new MD5Digest();
}

From source file:com.johnhunsley.events.domain.Hash.java

License:Apache License

/**
 * <p>/*from   www .  j  av a2s. c  o  m*/
 *     Creates a new Hash object implementing the encryption algorithm denoted by the given type.
 *     Will default to SHA1
 * </p>
 * @param type
 */
public Hash(final int type) {
    switch (type) {
    case SHA1_TYPE:
        digest = new SHA1Digest();
        break;
    case MD5_TYPE:
        digest = new MD5Digest();
        break;
    default:
        digest = new SHA1Digest();
    }
}

From source file:com.jrdp.core.encryption.RC4Session.java

License:Apache License

/**
 * Generates a Salted MAC for the current packet
 * @see <a href=http://msdn.microsoft.com/en-us/library/cc240789(v=PROT.10).aspx>
 * 5.3.6.1.1 Salted MAC Generation</a>
 *//*from www.j  a va 2 s .  co m*/
@Override
public byte[] createMac(byte[] data) {
    SHA1Digest sha1 = new SHA1Digest();
    sha1.update(macKey, 0, macKey.length);
    sha1.update(Constants.pad36, 0, Constants.pad36.length);
    byte[] length = new byte[4];
    BitManip.setLittleEndian(length, 0, data.length);
    sha1.update(length, 0, length.length);
    sha1.update(data, 0, data.length);
    byte[] sha1Digest = new byte[Constants.SHA1_DIGEST_LENGTH];
    sha1.doFinal(sha1Digest, 0);

    MD5Digest md5 = new MD5Digest();
    md5.update(macKey, 0, macKey.length);
    md5.update(Constants.pad5c, 0, Constants.pad5c.length);
    md5.update(sha1Digest, 0, sha1Digest.length);
    byte[] md5Digest = new byte[Constants.MD5_DIGEST_LENGTH];
    md5.doFinal(md5Digest, 0);

    byte[] mac = new byte[8];
    System.arraycopy(md5Digest, 0, mac, 0, 8);

    return mac;
}

From source file:com.jrdp.core.encryption.RC4Session.java

License:Apache License

private byte[] updateRc4Key(byte[] originalKey, byte[] currentKey) {
    int keySize = 0;
    switch (encryptionMethod) {
    case Constants.ENCRYPTION_128BIT:
        keySize = 16;//from   w  ww  .  j  a va2  s. c  o  m
        break;
    case Constants.ENCRYPTION_40BIT:
    case Constants.ENCRYPTION_56BIT:
        keySize = 8;
        break;
    case Constants.ENCRYPTION_NONE:
    case Constants.ENCRYPTION_FIPS:
        //Should never happen...
        return null;
    }

    SHA1Digest sha1 = new SHA1Digest();
    sha1.update(originalKey, 0, keySize);
    sha1.update(Constants.pad36, 0, Constants.pad36.length);
    sha1.update(currentKey, 0, currentKey.length);
    byte[] shaComponent = new byte[Rdp.SHA1_DIGEST_LENGTH];
    sha1.doFinal(shaComponent, 0);

    //StringManip.print(shaComponent, "SHA1:");

    MD5Digest md5 = new MD5Digest();
    md5.update(originalKey, 0, keySize);
    md5.update(Constants.pad5c, 0, Constants.pad5c.length);
    md5.update(shaComponent, 0, shaComponent.length);
    byte[] tempKey = new byte[Rdp.MD5_DIGEST_LENGTH];
    md5.doFinal(tempKey, 0);

    //StringManip.print(tempKey, "MD5:");

    RC4Engine rc4 = new RC4Engine();
    if (keySize == 16) {
        byte[] newKey = new byte[tempKey.length];
        rc4.init(true, new KeyParameter(tempKey));
        rc4.processBytes(tempKey, 0, tempKey.length, newKey, 0);
        return newKey;
    } else {
        byte[] newKey = new byte[8];
        byte[] smallerTmpKey = new byte[8];
        System.arraycopy(tempKey, 0, smallerTmpKey, 0, 8);
        rc4.init(true, new KeyParameter(smallerTmpKey));
        rc4.processBytes(smallerTmpKey, 0, 8, newKey, 0);
        newKey[0] = (byte) 0xd1;
        if (encryptionMethod == Constants.ENCRYPTION_40BIT) {
            newKey[1] = 0x26;
            newKey[2] = (byte) 0x9e;
        }
        return newKey;
    }
}