List of usage examples for java.math BigInteger BigInteger
private BigInteger(byte[] magnitude, int signum)
From source file:Main.java
private static String digest(final String value) { byte[] digested; try {//from w w w . java 2 s.c om digested = MessageDigest.getInstance(HASH_ALGORITHM).digest(value.getBytes(CHARSET)); } catch (NoSuchAlgorithmException e) { return null; } catch (UnsupportedEncodingException e) { return null; } String hashed = new BigInteger(1, digested).toString(16); int padding = HASH_LENGTH - hashed.length(); if (padding == 0) return hashed; char[] zeros = new char[padding]; Arrays.fill(zeros, '0'); return new StringBuilder(HASH_LENGTH).append(zeros).append(hashed).toString(); }
From source file:br.com.gerenciapessoal.service.CadastroUsuarioService.java
@SuppressWarnings("null") public static String md5(String input) { String md5 = null;// w w w . j a va 2s. co m if (!StringUtils.isNotBlank(input)) { return null; } try { //Create MessageDigest object for MD5 MessageDigest digest = MessageDigest.getInstance("MD5"); //Update input string in message digest digest.update(input.getBytes(), 0, input.length()); //Converts message digest value in base 16 (hex) md5 = new BigInteger(1, digest.digest()).toString(16); } catch (NoSuchAlgorithmException e) { } return md5.trim(); }
From source file:com.algodefu.yeti.md5.MD5HashGenerator.java
public static String generateKeyByObject(Object object) { MessageDigest m = null;//from www . j a v a 2 s . c o m try { m = MessageDigest.getInstance("MD5"); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } m.reset(); //m.update(objectToByteArray(object)); m.update(SerializationUtils.serialize((Serializable) object)); byte[] digest = m.digest(); BigInteger bigInt = new BigInteger(1, digest); String hashtext = bigInt.toString(16); while (hashtext.length() < 32) { hashtext = "0" + hashtext; } return hashtext; }
From source file:Main.java
public static String getMD5EncryptedString(String encTarget) { MessageDigest mdEnc = null;/*from w ww . jav a2 s. c om*/ try { mdEnc = MessageDigest.getInstance("MD5"); } catch (NoSuchAlgorithmException e) { System.out.println("Exception while encrypting to md5"); e.printStackTrace(); } // Encryption algorithm mdEnc.update(encTarget.getBytes(), 0, encTarget.length()); String md5 = new BigInteger(1, mdEnc.digest()).toString(16); while (md5.length() < 32) { md5 = "0" + md5; } return md5; }
From source file:net.ftb.util.CryptoUtils.java
/** * Newer implementation available if possible use {@link #decrypt(String str, byte[] key)} * @param str string to decrypt/* w w w .j ava2s. co m*/ * @param key decryption key * @return decrypted string or "" if fails */ @Deprecated public static String decryptLegacy(String str, byte[] key) { BigInteger in = new BigInteger(str, 16).xor(new BigInteger(1, key)); try { return new String(in.toByteArray(), "utf8"); } catch (UnsupportedEncodingException e) { return ""; } catch (NumberFormatException e) { Logger.logError("Error occurred during legacy decryption"); return ""; } }
From source file:Main.java
public static RSAPublicKeySpec getRecipientsPublicKey(String contactNum, Context context) { Log.w(TAG, "retrieving public key for contact " + contactNum); SharedPreferences prefs = context.getSharedPreferences(contactNum, Context.MODE_PRIVATE); String pubMod = prefs.getString(PREF_PUBLIC_MOD, DEFAULT_PREF); String pubExp = prefs.getString(PREF_PUBLIC_EXP, DEFAULT_PREF); Log.w(TAG, "the public modulus is " + pubMod + " and exponent is " + pubExp + " for " + contactNum); // String recipient = prefs.getString(PREF_RECIPIENT_NUM, DEFAULT_PREF); if (!pubMod.isEmpty() && !pubExp.isEmpty()) { Log.i(TAG, "great! public key found for " + contactNum + " with modulus " + pubMod + " and exponent " + pubExp);/*from w ww . jav a2 s . c o m*/ byte[] pubModBA = Base64.decode(pubMod, Base64.DEFAULT); byte[] pubExpBA = Base64.decode(pubExp, Base64.DEFAULT); BigInteger pubModBI = new BigInteger(1, pubModBA); // 1 is added as // an attempt to // deal with // com.android.org.bouncycastle.crypto.DataLengthException: // input too // large for RSA // cipher BigInteger pubExpBI = new BigInteger(1, pubExpBA); Log.i(TAG, "public modulus is " + pubModBI + " and public exponent is " + pubExpBI + " in base 256 " + pubModBA + " " + pubExpBA); // do I need to catch any exception for the following? RSAPublicKeySpec pubKeySpec = new RSAPublicKeySpec(pubModBI, pubExpBI); // X509EncodedKeySpec publicKeySpec = new // X509EncodedKeySpec(encodedKey); return pubKeySpec; } Log.w(TAG, "recipient's public key not found"); return null; }
From source file:Main.java
public static String toHex(byte[] bytes) { BigInteger bi = new BigInteger(1, bytes); return String.format("%0" + (bytes.length << 1) + "X", bi); }
From source file:Main.java
public static String getMD5(String str) { if (str != null) { try {//from w ww . j a v a2 s .c o m final BigInteger bigInt; if (sMD5Digest == null) { sMD5Digest = MessageDigest.getInstance("MD5"); } synchronized (sMD5Digest) { sMD5Digest.reset(); bigInt = new BigInteger(1, sMD5Digest.digest(str.getBytes("UTF-8"))); } String hash = bigInt.toString(16); while (hash.length() < 32) { hash = "0" + hash; } return hash; } catch (Exception e) { e.printStackTrace(); return null; } } return null; }
From source file:com.codemage.sql.util.API_keyGenarator.java
public String nextAPIId() { return new BigInteger(130, random).toString(32); }
From source file:MD5.java
public static String calculateMD5(File updateFile) { MessageDigest digest;/*from ww w . j a va2 s . c o m*/ try { digest = MessageDigest.getInstance("MD5"); } catch (NoSuchAlgorithmException e) { return null; } InputStream is; try { is = new FileInputStream(updateFile); } catch (FileNotFoundException e) { return null; } byte[] buffer = new byte[8192]; int read; try { while ((read = is.read(buffer)) > 0) { digest.update(buffer, 0, read); } byte[] md5sum = digest.digest(); BigInteger bigInt = new BigInteger(1, md5sum); String output = bigInt.toString(16); // Fill to 32 chars output = String.format("%32s", output).replace(' ', '0'); return output; } catch (IOException e) { throw new RuntimeException("Unable to process file for MD5", e); } finally { try { is.close(); } catch (IOException e) { System.out.println(e); } } }