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

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

Introduction

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

Prototype

public int doFinal(byte[] out, int outOff) 

Source Link

Usage

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

License:Open Source License

/**
 * TODO: Kommentar/*from   w  ww.  j a  v  a2  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  w  w  .ja va 2 s  .  c  o 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;/* w  w w . j a  v  a2  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   www. j  a  va 2s.  com*/
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);/*  w  ww  .j a va 2s .  c  o m*/

        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);
    return bytesToHex(md5);
}

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

/**
 * Calculate the UUID for the machine this code is running on. To do this
 * the method lists all network interfaces that are real 'server' interfaces
 * (ignoring loop-back, virtual, and point-to-point interfaces). The method
 * takes each interface name (as a string) and hardware address into a MD5
 * digest one after the other and finally converts the resulting 128bit
 * digest into a UUID./* ww  w.j  a v  a2s .  co m*/
 * <p>
 * The method also feeds the local machine name into the digest.
 * <p>
 * This method relies on Java 6 methods, but also works with Java 5. However
 * the result will not be the same on Java 5 as on Java 6.
 * 
 * @return the UUID of the machine or null if the uuid can not be
 *         calculated.
 * @throws SocketException
 * @throws UnsupportedEncodingException
 * @throws UnknownHostException
 */
public UUID getMachineId() throws UnsupportedEncodingException, SocketException, UnknownHostException {
    final MD5Digest md5 = new MD5Digest();
    md5.reset();
    if (useNetwork) {
        updateWithNetworkData(md5);
    }
    if (useHostName) {
        updateWithHostName(md5);
    }
    if (useArchitecture) {
        updateWithArchitecture(md5);
    }
    final byte[] digest = new byte[16];
    md5.doFinal(digest, 0);
    return UUID.nameUUIDFromBytes(digest);
}

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 {//w w  w .  j  av  a 2 s .co  m
        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);
    }
}

From source file:dorkbox.build.Project.java

License:Apache License

/**
 * Generates checksums for the given path
 *///from   w  w w .  j  a v a 2s  .  c o  m
public static final String generateChecksums(Paths... paths) throws IOException {
    synchronized (Project.class) {
        // calculate the hash of all the files in the source path
        Set<String> names = new HashSet<String>(64);

        for (Paths path : paths) {
            names.addAll(path.getPaths());
        }

        // hash of hash of files. faster than using java to hash files
        MD5Digest md5_digest = new MD5Digest();

        boolean found = false;
        for (String name : names) {
            File file = new File(name);
            if (file.isFile() && file.canRead()) {
                found = true;

                byte[] hashBytes = MD5.getHash(file);
                md5_digest.update(hashBytes, 0, hashBytes.length);
            }
        }

        if (!found) {
            return null;
        }

        byte[] hashBytes = new byte[md5_digest.getDigestSize()];
        md5_digest.doFinal(hashBytes, 0);

        String fileChecksums = Base64Fast.encodeToString(hashBytes, false);
        return fileChecksums;
    }
}

From source file:freemail.AccountManager.java

License:Open Source License

public static void changePassword(FreemailAccount account, String newpassword) {
    MD5Digest md5 = new MD5Digest();

    md5.update(newpassword.getBytes(), 0, newpassword.getBytes().length);
    byte[] md5passwd = new byte[md5.getDigestSize()];
    md5.doFinal(md5passwd, 0);
    String strmd5 = new String(Hex.encode(md5passwd));

    account.getProps().put("md5passwd", strmd5);
}