Search in sources :

Example 86 with MessageDigest

use of java.security.MessageDigest in project carat by amplab.

the class SamplingLibrary method getTimeBasedUuid.

/**
	 * Generate a time-based, random identifier.
	 * 
	 * @param c
	 *            the app's Context
	 * @return a time-based, random identifier.
	 */
public static String getTimeBasedUuid(Context c, boolean includeTimestamp) {
    String aID = getAndroidId(c);
    String wifiMac = getWifiMacAddress(c);
    String devid = getDeviceId(c);
    String concat = "";
    if (aID != null)
        concat = aID;
    else
        concat = "0000000000000000";
    if (wifiMac != null)
        concat += wifiMac;
    else
        concat += "00:00:00:00:00:00";
    // a space if length is less than 15:
    if (devid != null) {
        concat += devid;
        if (devid.length() < 15)
            concat += " ";
    } else
        concat += "000000000000000";
    if (includeTimestamp) {
        long timestamp = System.currentTimeMillis();
        concat += timestamp;
    }
    // "AID="+aID+" wifiMac="+wifiMac+" devid="+devid+" rawUUID=" +concat );
    try {
        MessageDigest md = MessageDigest.getInstance("SHA-512");
        md.update(concat.getBytes());
        byte[] mdbytes = md.digest();
        StringBuilder hexString = new StringBuilder();
        for (int i = 0; i < mdbytes.length; i++) {
            String hx = Integer.toHexString(0xFF & mdbytes[i]);
            if (hx.equals("0"))
                hexString.append("00");
            else
                hexString.append(hx);
        }
        String uuid = hexString.toString().substring(0, UUID_LENGTH);
        // FlurryAgent.logEvent("ANDROID_ID=" + aID +" UUID=" + uuid);
        return uuid;
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
        return aID;
    }
}
Also used : NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest)

Example 87 with MessageDigest

use of java.security.MessageDigest in project carat by amplab.

the class SamplingLibrary method getSignatures.

public static List<String> getSignatures(PackageInfo pak) {
    List<String> sigList = new LinkedList<String>();
    String[] pmInfos = pak.requestedPermissions;
    if (pmInfos != null) {
        byte[] bytes = getPermissionBytes(pmInfos);
        String hexB = convertToHex(bytes);
        sigList.add(hexB);
    }
    Signature[] sigs = pak.signatures;
    for (Signature s : sigs) {
        MessageDigest md = null;
        try {
            md = MessageDigest.getInstance("SHA-1");
            md.update(s.toByteArray());
            byte[] dig = md.digest();
            // Add SHA-1
            sigList.add(convertToHex(dig));
            CertificateFactory fac = CertificateFactory.getInstance("X.509");
            if (fac == null)
                continue;
            X509Certificate cert = (X509Certificate) fac.generateCertificate(new ByteArrayInputStream(s.toByteArray()));
            if (cert == null)
                continue;
            PublicKey pkPublic = cert.getPublicKey();
            if (pkPublic == null)
                continue;
            String al = pkPublic.getAlgorithm();
            if (al.equals("RSA")) {
                md = MessageDigest.getInstance("SHA-256");
                RSAPublicKey rsa = (RSAPublicKey) pkPublic;
                byte[] data = rsa.getModulus().toByteArray();
                if (data[0] == 0) {
                    byte[] copy = new byte[data.length - 1];
                    System.arraycopy(data, 1, copy, 0, data.length - 1);
                    md.update(copy);
                } else
                    md.update(data);
                dig = md.digest();
                // Add SHA-256 of modulus
                sigList.add(convertToHex(dig));
            } else if (al.equals("DSA")) {
                DSAPublicKey dsa = (DSAPublicKey) pkPublic;
                md = MessageDigest.getInstance("SHA-256");
                byte[] data = dsa.getY().toByteArray();
                if (data[0] == 0) {
                    byte[] copy = new byte[data.length - 1];
                    System.arraycopy(data, 1, copy, 0, data.length - 1);
                    md.update(copy);
                } else
                    md.update(data);
                dig = md.digest();
                // Add SHA-256 of public key (DSA)
                sigList.add(convertToHex(dig));
            } else {
                Log.e("SamplingLibrary", "Weird algorithm: " + al + " for " + pak.packageName);
            }
        } catch (NoSuchAlgorithmException e) {
        // Do nothing
        } catch (CertificateException e) {
        // Do nothing
        }
    }
    return sigList;
}
Also used : RSAPublicKey(java.security.interfaces.RSAPublicKey) PublicKey(java.security.PublicKey) DSAPublicKey(java.security.interfaces.DSAPublicKey) CertificateException(java.security.cert.CertificateException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) CertificateFactory(java.security.cert.CertificateFactory) LinkedList(java.util.LinkedList) X509Certificate(java.security.cert.X509Certificate) DSAPublicKey(java.security.interfaces.DSAPublicKey) RSAPublicKey(java.security.interfaces.RSAPublicKey) ByteArrayInputStream(java.io.ByteArrayInputStream) Signature(android.content.pm.Signature) MessageDigest(java.security.MessageDigest)

Example 88 with MessageDigest

use of java.security.MessageDigest in project SmartZPN by andforce.

the class TableEncryptor method passwordToInt64.

long passwordToInt64(String password) {
    try {
        byte[] passwordBytes = password.getBytes("UTF-8");
        MessageDigest md5 = MessageDigest.getInstance("MD5");
        byte[] hashPwd = md5.digest(passwordBytes);
        long a = bytesToInt64(hashPwd);
        return a;
    } catch (Exception e) {
        e.printStackTrace();
        return 0;
    }
}
Also used : MessageDigest(java.security.MessageDigest)

Example 89 with MessageDigest

use of java.security.MessageDigest in project freeline by alibaba.

the class FreelineCore method generateStringMD5.

private static String generateStringMD5(String input) {
    try {
        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] bytes = md.digest(input.getBytes());
        StringBuilder sb = new StringBuilder();
        for (byte aByte : bytes) {
            sb.append(Integer.toHexString((aByte & 0xFF) | 0x100).substring(1, 3));
        }
        return sb.toString();
    } catch (NoSuchAlgorithmException e) {
        Log.e(TAG, "MD5 algorithm not found.");
        return input;
    }
}
Also used : NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest)

Example 90 with MessageDigest

use of java.security.MessageDigest in project hadoop by apache.

the class StripedBlockChecksumReconstructor method reconstruct.

public void reconstruct() throws IOException {
    MessageDigest digester = MD5Hash.getDigester();
    long maxTargetLength = getMaxTargetLength();
    while (requestedLen > 0 && getPositionInBlock() < maxTargetLength) {
        long remaining = maxTargetLength - getPositionInBlock();
        final int toReconstructLen = (int) Math.min(getStripedReader().getBufferSize(), remaining);
        // step1: read from minimum source DNs required for reconstruction.
        // The returned success list is the source DNs we do real read from
        getStripedReader().readMinimumSources(toReconstructLen);
        // step2: decode to reconstruct targets
        reconstructTargets(toReconstructLen);
        // step3: calculate checksum
        checksumDataLen += checksumWithTargetOutput(targetBuffer.array(), toReconstructLen, digester);
        updatePositionInBlock(toReconstructLen);
        requestedLen -= toReconstructLen;
        clearBuffers();
    }
    byte[] digest = digester.digest();
    md5 = new MD5Hash(digest);
    md5.write(checksumWriter);
}
Also used : MD5Hash(org.apache.hadoop.io.MD5Hash) MessageDigest(java.security.MessageDigest)

Aggregations

MessageDigest (java.security.MessageDigest)1237 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)613 IOException (java.io.IOException)176 UnsupportedEncodingException (java.io.UnsupportedEncodingException)102 BigInteger (java.math.BigInteger)101 InputStream (java.io.InputStream)72 FileInputStream (java.io.FileInputStream)70 File (java.io.File)62 DigestInputStream (java.security.DigestInputStream)61 Test (org.junit.Test)61 ByteArrayOutputStream (java.io.ByteArrayOutputStream)51 DigestOutputStream (java.security.DigestOutputStream)45 ArrayList (java.util.ArrayList)37 ByteArrayInputStream (java.io.ByteArrayInputStream)31 X509Certificate (java.security.cert.X509Certificate)29 OutputStream (java.io.OutputStream)28 GeneralSecurityException (java.security.GeneralSecurityException)25 Cipher (javax.crypto.Cipher)25 SecretKeySpec (javax.crypto.spec.SecretKeySpec)25 Provider (java.security.Provider)22