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;
}
}
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;
}
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;
}
}
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;
}
}
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);
}
Aggregations