use of java.security.NoSuchAlgorithmException in project Anki-Android by Ramblurr.
the class Utils method convertStreamToString.
// /**
// * MD5 sum of file.
// * Equivalent to checksum(open(os.path.join(mdir, file), "rb").read()))
// *
// * @param path The full path to the file
// * @return A string of length 32 containing the hexadecimal representation of the MD5 checksum of the contents
// * of the file
// */
// public static String fileChecksum(String path) {
// byte[] bytes = null;
// try {
// File file = new File(path);
// if (file != null && file.isFile()) {
// bytes = new byte[(int)file.length()];
// FileInputStream fin = new FileInputStream(file);
// fin.read(bytes);
// }
// } catch (FileNotFoundException e) {
// Log.e(AnkiDroidApp.TAG, "Can't find file " + path + " to calculate its checksum");
// } catch (IOException e) {
// Log.e(AnkiDroidApp.TAG, "Can't read file " + path + " to calculate its checksum");
// }
// if (bytes == null) {
// Log.w(AnkiDroidApp.TAG, "File " + path + " appears to be empty");
// return "";
// }
// MessageDigest md = null;
// byte[] digest = null;
// try {
// md = MessageDigest.getInstance("MD5");
// digest = md.digest(bytes);
// } catch (NoSuchAlgorithmException e) {
// Log.e(AnkiDroidApp.TAG, "Utils.checksum: No such algorithm. " + e.getMessage());
// throw new RuntimeException(e);
// }
// BigInteger biginteger = new BigInteger(1, digest);
// String result = biginteger.toString(16);
// // pad with zeros to length of 32
// if (result.length() < 32) {
// result = "00000000000000000000000000000000".substring(0, 32 - result.length()) + result;
// }
// return result;
// }
/**
* Tempo files
* ***********************************************************************************************
*/
// tmpdir
// tmpfile
// namedtmp
/**
* Converts an InputStream to a String.
* @param is InputStream to convert
* @return String version of the InputStream
*/
public static String convertStreamToString(InputStream is) {
String contentOfMyInputStream = "";
try {
BufferedReader rd = new BufferedReader(new InputStreamReader(is), 4096);
String line;
StringBuilder sb = new StringBuilder();
while ((line = rd.readLine()) != null) {
sb.append(line);
}
rd.close();
contentOfMyInputStream = sb.toString();
} catch (Exception e) {
e.printStackTrace();
}
return contentOfMyInputStream;
}
use of java.security.NoSuchAlgorithmException in project Anki-Android by Ramblurr.
the class Utils method checksum.
/**
* Checksums
* ***********************************************************************************************
*/
/**
* SHA1 checksum.
* Equivalent to python sha1.hexdigest()
*
* @param data the string to generate hash from
* @return A string of length 40 containing the hexadecimal representation of the MD5 checksum of data.
*/
public static String checksum(String data) {
String result = "";
if (data != null) {
MessageDigest md = null;
byte[] digest = null;
try {
md = MessageDigest.getInstance("SHA1");
digest = md.digest(data.getBytes("UTF-8"));
} catch (NoSuchAlgorithmException e) {
Log.e(AnkiDroidApp.TAG, "Utils.checksum: No such algorithm. " + e.getMessage());
throw new RuntimeException(e);
} catch (UnsupportedEncodingException e) {
Log.e(AnkiDroidApp.TAG, "Utils.checksum: " + e.getMessage());
e.printStackTrace();
}
BigInteger biginteger = new BigInteger(1, digest);
result = biginteger.toString(16);
// not 32.
if (result.length() < 40) {
String zeroes = "0000000000000000000000000000000000000000";
result = zeroes.substring(0, zeroes.length() - result.length()) + result;
}
}
return result;
}
use of java.security.NoSuchAlgorithmException in project StickerCamera by Skykai521.
the class MD5Util method getMD5.
/**
* MD5加密
*
* @param val
* @return
* @throws NoSuchAlgorithmException
*/
public static String getMD5(String val) {
MessageDigest md5;
try {
md5 = MessageDigest.getInstance("MD5");
md5.update(val.getBytes());
// 加密
byte[] m = md5.digest();
return getString(m);
} catch (NoSuchAlgorithmException e) {
return val;
}
}
use of java.security.NoSuchAlgorithmException in project SeriesGuide by UweTrottmann.
the class Security method verify.
/**
* Verifies that the signature from the server matches the computed
* signature on the data. Returns true if the data is correctly signed.
*
* @param publicKey public key associated with the developer account
* @param signedData signed data from server
* @param signature server signature
* @return true if the data and signature match
*/
public static boolean verify(PublicKey publicKey, String signedData, String signature) {
Signature sig;
try {
sig = Signature.getInstance(SIGNATURE_ALGORITHM);
sig.initVerify(publicKey);
sig.update(signedData.getBytes());
if (!sig.verify(Base64.decode(signature))) {
Timber.e("Signature verification failed.");
return false;
}
return true;
} catch (NoSuchAlgorithmException | InvalidKeyException | SignatureException | Base64DecoderException e) {
Timber.e(e, "Signature verification aborted.");
}
return false;
}
use of java.security.NoSuchAlgorithmException in project Signal-Android by WhisperSystems.
the class ConversationActivity method handleInviteLink.
private void handleInviteLink() {
try {
String inviteText;
boolean a = SecureRandom.getInstance("SHA1PRNG").nextBoolean();
if (a)
inviteText = getString(R.string.ConversationActivity_lets_switch_to_signal, "https://sgnl.link/1LoIMUl");
else
inviteText = getString(R.string.ConversationActivity_lets_use_this_to_chat, "https://sgnl.link/1MF56H1");
if (isDefaultSms) {
composeText.appendInvite(inviteText);
} else {
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("smsto:" + recipients.getPrimaryRecipient().getNumber()));
intent.putExtra("sms_body", inviteText);
intent.putExtra(Intent.EXTRA_TEXT, inviteText);
startActivity(intent);
}
} catch (NoSuchAlgorithmException e) {
throw new AssertionError(e);
}
}
Aggregations