List of usage examples for java.security MessageDigest getInstance
public static MessageDigest getInstance(String algorithm) throws NoSuchAlgorithmException
From source file:data.repository.pragma.utils.MD5Utils.java
public static String getMD5(String file_url) { URL url;/* w w w . j a va 2 s . c om*/ try { url = new URL(file_url); InputStream is = url.openStream(); MessageDigest md = MessageDigest.getInstance("MD5"); String digest = getDigest(is, md, 2048); return digest; } catch (MalformedURLException e) { // TODO Auto-generated catch block return null; } catch (IOException e) { // TODO Auto-generated catch block return null; } catch (NoSuchAlgorithmException e) { // TODO Auto-generated catch block return null; } }
From source file:brainleg.app.util.EncryptionUtil.java
private static byte[] generateDigest(String algorithm, byte[] data) { if (data == null) { return null; }//from w ww . ja v a 2 s . c o m try { MessageDigest messageDigest = MessageDigest.getInstance(algorithm); byte[] digest = messageDigest.digest(data); return digest; } catch (NoSuchAlgorithmException e) { throw new IllegalArgumentException(e); } }
From source file:Main.java
/** * Digests the given message with the given algorithm * * @param originalMessage the original message * @param algorithm the algorithm// w w w. ja v a 2 s. c om * @return the byte [ ] * @throws NoSuchAlgorithmException if the given algorithm does not exist in JCA */ public static byte[] digestMessage(byte[] originalMessage, String algorithm) throws NoSuchAlgorithmException { MessageDigest messageDigest; messageDigest = MessageDigest.getInstance(algorithm); messageDigest.update(originalMessage, 0, originalMessage.length); return messageDigest.digest(); }
From source file:StringUtils.java
/** * Encode a string using algorithm specified in web.xml and return the * resulting encrypted password. If exception, the plain credentials * string is returned/*www .ja va2 s . co m*/ * * @param password Password or other credentials to use in authenticating * this username * @param algorithm Algorithm used to do the digest * * @return encypted password based on the algorithm. */ public static String encodePassword(String password, String algorithm) { byte[] unencodedPassword = password.getBytes(); MessageDigest md = null; try { // first create an instance, given the provider md = MessageDigest.getInstance(algorithm); } catch (Exception e) { System.out.println(e); return password; } md.reset(); // call the update method one or more times // (useful when you don't know the size of your data, eg. stream) md.update(unencodedPassword); // now calculate the hash byte[] encodedPassword = md.digest(); StringBuffer buf = new StringBuffer(); for (int i = 0; i < encodedPassword.length; i++) { if ((encodedPassword[i] & 0xff) < 0x10) { buf.append("0"); } buf.append(Long.toString(encodedPassword[i] & 0xff, 16)); } return buf.toString(); }
From source file:com.netflix.exhibitor.core.s3.S3Utils.java
public static byte[] md5(byte[] buffer, int length) { try {// www . j a v a 2 s .c o m MessageDigest mdigest = MessageDigest.getInstance("MD5"); mdigest.update(buffer, 0, length); return mdigest.digest(); } catch (Exception e) { throw new RuntimeException(e); } }
From source file:Main.java
/** * Compute the sha-256 digest of data./*from w w w. j av a 2s . co m*/ * @param data The input byte buffer. This does not change the position. * @return The digest. */ public static byte[] digestSha256(ByteBuffer data) { MessageDigest sha256; try { sha256 = MessageDigest.getInstance("SHA-256"); } catch (NoSuchAlgorithmException exception) { // Don't expect this to happen. throw new Error("MessageDigest: SHA-256 is not supported: " + exception.getMessage()); } int savePosition = data.position(); sha256.update(data); data.position(savePosition); return sha256.digest(); }
From source file:Main.java
public static void printKeyHash(Activity pActivity) { // Add code to print out the key hash try {/* ww w .j av a 2 s .com*/ PackageInfo info = pActivity.getPackageManager().getPackageInfo(pActivity.getPackageName(), PackageManager.GET_SIGNATURES); for (Signature signature : info.signatures) { MessageDigest md = MessageDigest.getInstance("SHA"); md.update(signature.toByteArray()); Log.d("KeyHash:", Base64.encodeToString(md.digest(), Base64.DEFAULT)); } } catch (PackageManager.NameNotFoundException e) { Log.d("KeyHash:", e.toString()); } catch (NoSuchAlgorithmException e) { Log.d("KeyHash:", e.toString()); } }
From source file:com.aast.encrypt.EncryptManager.java
private static byte[] getKeyFromString(String keyStr) throws UnsupportedEncodingException, NoSuchAlgorithmException { byte[] key = keyStr.getBytes("UTF-8"); MessageDigest sha = MessageDigest.getInstance("SHA-1"); key = sha.digest(key);/*from w ww.java 2 s. c o m*/ key = Arrays.copyOf(key, 16); // use only first 128 bit return key; }
From source file:Main.java
private static String getMD5(byte[] source) { try {//ww w . j a v a2 s. c o m MessageDigest md5 = MessageDigest.getInstance("MD5"); StringBuffer result = new StringBuffer(); for (byte b : md5.digest(source)) { result.append(Integer.toHexString((b & 0xf0) >>> 4)); result.append(Integer.toHexString(b & 0x0f)); } return result.toString(); } catch (Exception e) { throw new RuntimeException(e.getMessage(), e); } }
From source file:Main.java
public static String getMD5(String str) { if (str != null) { try {/*w w w . java 2 s . co 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; }