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

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

Introduction

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

Prototype

public void update(byte[] in, int inOff, int len) 

Source Link

Usage

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

License:Open Source License

/**
 * TODO: Kommentar/*www  .  j a va 2 s .c  om*/
 * 
 * @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.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 w  ww .j a v  a  2s. c  om*/
@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  w w. 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;
    }
}

From source file:com.jrdp.core.remote.rdp.Secure.java

License:Apache License

/**
 * @param clientRandom random generated by the client
 * @param serverRandom random generated by the server
 * @param keyLength length of keys being requested (40, 56 or 128)
 * @see <a href=http://msdn.microsoft.com/en-us/library/cc240785(v=PROT.10).aspx>
 * 5.3.5.1 Non-FIPS Initial Key Generation</a>
 *///from  w  ww .  j  av a 2s  . co  m
public void setupForNonFipsEncryption(byte[] clientRandom, byte[] serverRandom, int keyLength) {
    if (keyLength != 40 && keyLength != 56 && keyLength != 128) {
        Logger.log(Logger.ERROR, "Received unsupported key length for key generation");
        return;
    }

    //PreMasterSecret = First192Bits(ClientRandom) + First192Bits(ServerRandom)
    byte[] preMasterSecret = new byte[48];
    System.arraycopy(clientRandom, 0, preMasterSecret, 0, 24);
    System.arraycopy(serverRandom, 0, preMasterSecret, 24, 24);

    //SaltedHash(S, I) = MD5(S + SHA(I + S + ClientRandom + ServerRandom))
    //PreMasterHash(I) = SaltedHash(PremasterSecret, I)
    //MasterSecret = PreMasterHash(0x41) + PreMasterHash(0x4242) + PreMasterHash(0x434343)
    byte[] masterSecret = saltedHash(clientRandom, serverRandom, preMasterSecret, masterSecretValues);

    //MasterHash(I) = SaltedHash(MasterSecret, I)
    //SessionKeyBlob = MasterHash(0x58) + MasterHash(0x5959) + MasterHash(0x5A5A5A)
    byte[] sessionKeyBlob = saltedHash(clientRandom, serverRandom, masterSecret, sessionKeyBlobValues);

    //MACKey128 = First128Bits(SessionKeyBlob)
    //FinalHash(K) = MD5(K + ClientRandom + ServerRandom)
    //InitialClientDecryptKey128 = FinalHash(Second128Bits(SessionKeyBlob))
    //InitialClientEncryptKey128 = FinalHash(Third128Bits(SessionKeyBlob))
    byte[] macKey = new byte[16];
    byte[] encryptKey = new byte[16];
    byte[] decryptKey = new byte[16];
    System.arraycopy(sessionKeyBlob, 0, macKey, 0, 16);
    MD5Digest md5 = new MD5Digest();
    md5.update(sessionKeyBlob, 16, 16);
    md5.update(clientRandom, 0, clientRandom.length);
    md5.update(serverRandom, 0, serverRandom.length);
    md5.doFinal(decryptKey, 0);
    md5.update(sessionKeyBlob, 32, 16);
    md5.update(clientRandom, 0, clientRandom.length);
    md5.update(serverRandom, 0, serverRandom.length);
    md5.doFinal(encryptKey, 0);

    if (keyLength == 40 || keyLength == 56) {
        //MACKey56 = 0xD1 + Last56Bits(First64Bits(MACKey128))
        //FinalHash(K) = MD5(K + ClientRandom + ServerRandom)
        //InitialClientEncryptKey56 = 0xD1 + Last56Bits(First64Bits(InitialClientEncryptKey128))
        //InitialClientDecryptKey56 = 0xD1 + Last56Bits(First64Bits(InitialClientDecryptKey128))
        macKey[0] = encryptKey[0] = decryptKey[0] = (byte) 0xd1;
        if (keyLength == 40) {
            //MACKey40 = 0xD1269E + Last40Bits(First64Bits(MACKey128))
            //FinalHash(K) = MD5(K + ClientRandom + ServerRandom)
            //InitialClientEncryptKey40 = 0xD1269E + Last40Bits(First64Bits(InitialClientEncryptKey128))
            //InitialClientDecryptKey40 = 0xD1269E + Last40Bits(First64Bits(InitialClientDecryptKey128))
            macKey[1] = encryptKey[1] = decryptKey[1] = 0x26;
            macKey[2] = encryptKey[2] = decryptKey[2] = (byte) 0x9e;
        }
        byte[] tempMac = new byte[8];
        byte[] tempEncrypt = new byte[8];
        byte[] tempDecrypt = new byte[8];
        System.arraycopy(macKey, 0, tempMac, 0, 8);
        System.arraycopy(encryptKey, 0, tempEncrypt, 0, 8);
        System.arraycopy(decryptKey, 0, tempDecrypt, 0, 8);
        encryption = new RC4Session(tempEncrypt, tempDecrypt, tempMac, encryptionMethod);
    } else if (keyLength == 128) {
        encryption = new RC4Session(encryptKey, decryptKey, macKey, encryptionMethod);
    }
}

From source file:com.jrdp.core.remote.rdp.Secure.java

License:Apache License

private static byte[] saltedHash(byte[] clientRandom, byte[] serverRandom, byte[] salt, byte[][] in) {
    byte[] saltedHash = new byte[MD5_DIGEST_LENGTH * in.length];
    for (int i = 0; i < in.length; i++) {
        SHA1Digest sha1 = new SHA1Digest();
        sha1.update(in[i], 0, in[i].length);
        sha1.update(salt, 0, salt.length);
        sha1.update(clientRandom, 0, clientRandom.length);
        sha1.update(serverRandom, 0, serverRandom.length);
        byte[] sha1Digest = new byte[SHA1_DIGEST_LENGTH];
        sha1.doFinal(sha1Digest, 0);/*from  ww w  .  j  a  va  2s .  com*/

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

        System.arraycopy(md5Digest, 0, saltedHash, i * MD5_DIGEST_LENGTH, MD5_DIGEST_LENGTH);
    }
    return saltedHash;
}

From source file:com.pmovil.codenameone.nativeshare.Share.java

License:Open Source License

private static String md5(String text) {
    MD5Digest digest = new MD5Digest();
    byte[] data = text.getBytes();
    digest.update(data, 0, data.length);
    byte[] md5 = new byte[digest.getDigestSize()];
    digest.doFinal(md5, 0);//from   ww  w.j  av a2  s.  c  o m
    return bytesToHex(md5);
}

From source file:com.verhas.licensor.HardwareBinder.java

private void updateWithNetworkData(final MD5Digest md5, final NetworkInterfaceData[] networkInterfaces)
        throws UnsupportedEncodingException {
    for (final NetworkInterfaceData ni : networkInterfaces) {
        md5.update(ni.name.getBytes("utf-8"), 0, ni.name.getBytes("utf-8").length);
        if (ni.hwAddress != null) {
            md5.update(ni.hwAddress, 0, ni.hwAddress.length);
        }//w  w w.  j  ava 2 s  . com
    }
}

From source file:com.verhas.licensor.HardwareBinder.java

private void updateWithHostName(final MD5Digest md5) throws UnknownHostException, UnsupportedEncodingException {
    final String hostName = java.net.InetAddress.getLocalHost().getHostName();
    md5.update(hostName.getBytes("utf-8"), 0, hostName.getBytes("utf-8").length);
}

From source file:com.verhas.licensor.HardwareBinder.java

private void updateWithArchitecture(final MD5Digest md5) throws UnsupportedEncodingException {
    final String architectureString = System.getProperty("os.arch");
    md5.update(architectureString.getBytes("utf-8"), 0, architectureString.getBytes("utf-8").length);
}

From source file:com.yacme.ext.oxsit.cust_it.comp.security.cert.X509CertDisplayBase_IT.java

License:Open Source License

protected void initThumbPrints() {
    //obtain a byte block of the entire certificate data
    ByteArrayOutputStream bOut = new ByteArrayOutputStream();
    DEROutputStream dOut = new DEROutputStream(bOut);
    try {//from w  ww  .  ja  va 2 s  . c om
        dOut.writeObject(m_aX509);
        byte[] certBlock = bOut.toByteArray();

        //now compute the certificate SHA1 & MD5 digest
        SHA1Digest digsha1 = new SHA1Digest();
        digsha1.update(certBlock, 0, certBlock.length);
        byte[] hashsha1 = new byte[digsha1.getDigestSize()];
        digsha1.doFinal(hashsha1, 0);
        m_sSHA1Thumbprint = Helpers.printHexBytes(hashsha1);
        MD5Digest digmd5 = new MD5Digest();
        digmd5.update(certBlock, 0, certBlock.length);
        byte[] hashmd5 = new byte[digmd5.getDigestSize()];
        digmd5.doFinal(hashmd5, 0);
        m_sMD5Thumbprint = Helpers.printHexBytes(hashmd5);
    } catch (IOException e) {
        m_aLogger.severe("initThumbPrints", e);
    }
}