List of usage examples for java.security MessageDigest getInstance
public static MessageDigest getInstance(String algorithm) throws NoSuchAlgorithmException
From source file:Main.java
/** * Calculation md5 hash of string// www. j a va 2s. c o m * * @param s - string for hash calculation * @return md5 hash of input string */ public static String MD5Hash(String s) { int sHash = s.hashCode(); String result = md5Cache.get(sHash); if (result != null) return result; try { if (MD5 == null) MD5 = MessageDigest.getInstance("MD5"); MessageDigest alg = (MessageDigest) MD5.clone(); alg.update(s.getBytes()); StringBuffer hexString = new StringBuffer(32); for (byte b : alg.digest()) hexString.append(intToHexChars(0xFF & b)); result = hexString.toString(); } catch (Exception e) { } if (result == null) result = String.valueOf(s.hashCode()); md5Cache.put(sHash, result); return result; }
From source file:Main.java
public static String computeMD5(byte[] input) { try {//from ww w . jav a 2s. c o m MessageDigest md = MessageDigest.getInstance("MD5"); md.update(input, TYPE_NOT_CONNECTED, input.length); byte[] md5bytes = md.digest(); StringBuffer hexString = new StringBuffer(); for (int i = TYPE_NOT_CONNECTED; i < md5bytes.length; i += TYPE_WIFI) { String hex = Integer.toHexString(md5bytes[i] & MotionEventCompat.ACTION_MASK); if (hex.length() == TYPE_WIFI) { hexString.append('0'); } hexString.append(hex); } return hexString.toString(); } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e); } }
From source file:MD5.java
public static String create(String content) throws MD5IsNotSupported { String result = ""; try {// w ww. j a v a 2 s .c om byte[] defaultBytes = content.getBytes(); MessageDigest algorithm = MessageDigest.getInstance("MD5"); algorithm.reset(); algorithm.update(defaultBytes); byte messageDigest[] = algorithm.digest(); StringBuffer hexString = new StringBuffer(); for (int i = 0; i < messageDigest.length; i++) { hexString.append(Integer.toHexString(0xFF & messageDigest[i])); } result = hexString.toString(); } catch (NoSuchAlgorithmException ex) { throw new MD5IsNotSupported(ex); } assert !result.isEmpty(); return result; }
From source file:Main.java
public static void printOutMyHashKey(Context context, String packageName) { try {/*from w ww . j ava2 s . c o m*/ PackageInfo info = context.getPackageManager().getPackageInfo(packageName, 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) { e.printStackTrace(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } }
From source file:mx.com.pendulum.carga.util.Md5Converter.java
public static String calculaMD5(byte[] data) { InputStream is = new ByteArrayInputStream(data); try {/* w w w. j a v a2 s . c o m*/ byte[] buffer = new byte[1024]; MessageDigest digest = MessageDigest.getInstance("MD5"); int numRead = 0; while (numRead != -1) { numRead = is.read(buffer); if (numRead > 0) { digest.update(buffer, 0, numRead); } } byte[] md5Bytes = digest.digest(); return convertHashToString(md5Bytes); } catch (Exception e) { return null; } finally { if (is != null) { try { is.close(); } catch (Exception e) { } } } }
From source file:Main.java
public static String getMD5Str(String str) { MessageDigest messageDigest = null; try {//www . j a v a 2s . c o m messageDigest = MessageDigest.getInstance("MD5"); messageDigest.reset(); messageDigest.update(str.getBytes("UTF-8")); } catch (NoSuchAlgorithmException e) { System.out.println("NoSuchAlgorithmException caught!"); } catch (UnsupportedEncodingException e) { } byte[] byteArray = messageDigest.digest(); StringBuffer md5StrBuff = new StringBuffer(); for (int i = 0; i < byteArray.length; i++) { if (Integer.toHexString(0xFF & byteArray[i]).length() == 1) md5StrBuff.append("0").append(Integer.toHexString(0xFF & byteArray[i])); else md5StrBuff.append(Integer.toHexString(0xFF & byteArray[i])); } return md5StrBuff.substring(0, 24).toString().toUpperCase(); }
From source file:controlpac.EncryptHelper.java
public static String Encriptar(String texto) { String base64EncryptedString = ""; try {//w w w.j av a2 s . c o m MessageDigest md = MessageDigest.getInstance("MD5"); //Crea un hash con la clave elegida byte[] digestOfPassword = md.digest(secretKey.getBytes("utf-8")); byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24); SecretKey key = new SecretKeySpec(keyBytes, "DESede"); //Crea la clave Cipher cipher = Cipher.getInstance("DESede"); cipher.init(Cipher.ENCRYPT_MODE, key);//Inicializa el cifrado con la clave byte[] plainTextBytes = texto.getBytes("utf-8"); byte[] buf = cipher.doFinal(plainTextBytes);//Cifra el texto byte[] base64Bytes = Base64.encodeBase64(buf);//Encodea el texto en base64 base64EncryptedString = new String(base64Bytes); } catch (Exception ex) { } return base64EncryptedString; }
From source file:Main.java
public static byte[] computeSHA1(ByteBuffer convertme, int offset, int len) { int oldp = convertme.position(); int oldl = convertme.limit(); try {//from ww w . j av a 2 s . c om MessageDigest md = MessageDigest.getInstance("SHA-1"); convertme.position(offset); convertme.limit(len); md.update(convertme); return md.digest(); } catch (Exception e) { e.printStackTrace(); } finally { convertme.limit(oldl); convertme.position(oldp); } return new byte[20]; }
From source file:Main.java
public static String digest(InputStream in) throws Exception { MessageDigest messageDigest = null; XMLInputFactory inputFactory = XMLInputFactory.newFactory(); messageDigest = MessageDigest.getInstance("MD5"); XMLEventReader eventReader = inputFactory.createXMLEventReader(in); while (eventReader.hasNext()) { XMLEvent event = eventReader.nextEvent(); if (event.isStartElement()) { messageDigest.update(event.asStartElement().getName().toString().getBytes()); } else if (event.isEndElement()) { messageDigest.update(event.asEndElement().getName().toString().getBytes()); }//from w ww . jav a2 s .c o m } StringBuffer result = new StringBuffer(); byte[] digest = messageDigest.digest(); for (byte b : digest) { result.append(Integer.toString((b & 0xff) + 0x100, 16).substring(1)); } return result.toString(); }
From source file:Main.java
public static String getHashKey(Context context) { // Add code to print out the key hash try {/*from w ww . j av a 2 s. c o m*/ PackageInfo info = context.getPackageManager().getPackageInfo(context.getPackageName(), PackageManager.GET_SIGNATURES); for (Signature signature : info.signatures) { MessageDigest md = MessageDigest.getInstance("SHA"); md.update(signature.toByteArray()); return Base64.encodeToString(md.digest(), Base64.DEFAULT); } } catch (NameNotFoundException e) { } catch (NoSuchAlgorithmException e) { } return null; }