List of usage examples for java.security MessageDigest getInstance
public static MessageDigest getInstance(String algorithm) throws NoSuchAlgorithmException
From source file:com.smallfe.clerk.util.DigestUtil.java
public static String getSHA256(String message) { try {// www . j a v a 2 s . c o m MessageDigest md = MessageDigest.getInstance("SHA-256"); md.update(message.getBytes()); byte byteData[] = md.digest(); //convert the byte to hex format method 1 StringBuffer sb = new StringBuffer(); for (int i = 0; i < byteData.length; i++) { sb.append(Integer.toString((byteData[i] & 0xff) + 0x100, 16).substring(1)); } return sb.toString(); } catch (NoSuchAlgorithmException ex) { Logger.getLogger(DigestUtil.class.getName()).log(Level.SEVERE, null, ex); return null; } }
From source file:Main.java
public static String getFileMD5String(File file) { if (!file.exists() || file.isFile()) { return null; }/*from w w w. j a va 2 s. c o m*/ MessageDigest messagedigest; FileInputStream fis = null; try { fis = new FileInputStream(file); messagedigest = MessageDigest.getInstance("MD5"); int len; byte[] buf = new byte[1024]; while ((len = fis.read(buf)) != -1) { messagedigest.update(buf, 0, len); } byte md5Bytes[] = messagedigest.digest(); StringBuffer stringbuffer = new StringBuffer(); for (int i = 0; i < md5Bytes.length; i++) { stringbuffer.append(HEX_DIGITS[(md5Bytes[i] & 0xf0) >>> 4]); stringbuffer.append(HEX_DIGITS[md5Bytes[i] & 0x0f]); } return stringbuffer.toString(); } catch (IOException e) { e.printStackTrace(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } finally { if (fis != null) { try { fis.close(); } catch (IOException e) { e.printStackTrace(); } } } return null; }
From source file:Main.java
public static byte[] encryptMD5File(File file) { FileInputStream in = null;/*from w ww. jav a 2 s . c om*/ try { in = new FileInputStream(file); FileChannel channel = in.getChannel(); MappedByteBuffer buffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, file.length()); MessageDigest md = MessageDigest.getInstance("MD5"); md.update(buffer); return md.digest(); } catch (NoSuchAlgorithmException | IOException e) { e.printStackTrace(); } finally { if (in != null) { try { in.close(); } catch (IOException ignored) { } } } return null; }
From source file:BD.Encriptador.java
public static String Encriptar(String texto) { String secretKey = "qualityinfosolutions"; //llave para encriptar datos String base64EncryptedString = ""; try {/* w w w .j ava2 s.c om*/ MessageDigest md = MessageDigest.getInstance("MD5"); byte[] digestOfPassword = md.digest(secretKey.getBytes("utf-8")); byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24); SecretKey key = new SecretKeySpec(keyBytes, "DESede"); Cipher cipher = Cipher.getInstance("DESede"); cipher.init(Cipher.ENCRYPT_MODE, key); byte[] plainTextBytes = texto.getBytes("utf-8"); byte[] buf = cipher.doFinal(plainTextBytes); byte[] base64Bytes = Base64.encodeBase64(buf); base64EncryptedString = new String(base64Bytes); } catch (Exception ex) { } return base64EncryptedString; }
From source file:Main.java
public static String keyHash(Context context) { String key = ""; try {/* w w w .j av a 2s . com*/ PackageInfo info = context.getPackageManager().getPackageInfo("org.tathva.triloaded.anubhava", PackageManager.GET_SIGNATURES); for (Signature signature : info.signatures) { MessageDigest md = MessageDigest.getInstance("SHA"); md.update(signature.toByteArray()); key = Base64.encodeToString(md.digest(), Base64.DEFAULT); Log.d("anas", key); } } catch (NameNotFoundException e) { } catch (NoSuchAlgorithmException e) { } return key; }
From source file:Main.java
static String getSHA1CertFingerprint(Context ctx) { try {//www . ja v a 2 s.co m Signature[] sigs = ctx.getPackageManager().getPackageInfo(ctx.getPackageName(), PackageManager.GET_SIGNATURES).signatures; if (sigs.length == 0) { return "ERROR: NO SIGNATURE."; } else if (sigs.length > 1) { return "ERROR: MULTIPLE SIGNATURES"; } byte[] digest = MessageDigest.getInstance("SHA1").digest(sigs[0].toByteArray()); StringBuilder hexString = new StringBuilder(); for (int i = 0; i < digest.length; ++i) { if (i > 0) { hexString.append(":"); } byteToString(hexString, digest[i]); } return hexString.toString(); } catch (PackageManager.NameNotFoundException ex) { ex.printStackTrace(); return "(ERROR: package not found)"; } catch (NoSuchAlgorithmException ex) { ex.printStackTrace(); return "(ERROR: SHA1 algorithm not found)"; } }
From source file:Main.java
private static String hashWithAlgorithm(String algorithm, byte[] bytes) { MessageDigest hash;// w w w . j ava 2 s . co m try { hash = MessageDigest.getInstance(algorithm); } catch (NoSuchAlgorithmException e) { return null; } return hashBytes(hash, bytes); }
From source file:Main.java
public static byte[] calculateMd5(String filePath) throws IOException { try {//w w w.j a v a 2 s . c o m MessageDigest digest = MessageDigest.getInstance("MD5"); byte[] buffer = new byte[4 * 1024]; InputStream is = new FileInputStream(new File(filePath)); int lent; while ((lent = is.read(buffer)) != -1) { digest.update(buffer, 0, lent); } is.close(); return digest.digest(); } catch (NoSuchAlgorithmException e) { throw new RuntimeException("MD5 algorithm not found."); } }
From source file:eds.component.encryption.EncryptionUtility.java
public static String getHash(String transactionId, EncryptionType type) { String secureHash = transactionId; MessageDigest md;// w w w .jav a 2 s . co m String hashedID = ""; byte[] hash; try { //md = MessageDigest.getInstance("SHA-256"); md = MessageDigest.getInstance(type.toString()); hash = md.digest(secureHash.getBytes("UTF-8")); hashedID = new String(Hex.encodeHex(hash));//String.format("%032x", new BigInteger(hash));;//String.format("%032x", Arrays.toString(hash)); } catch (NoSuchAlgorithmException | UnsupportedEncodingException ex) { throw new RuntimeException("No such algorithm or encoding method: " + ex.getMessage()); } return hashedID; }
From source file:lib.clases_cripto.java
public static String Encriptar(String texto) { String secretKey = "qualityinfosolutions"; //llave para encriptar datos String base64EncryptedString = ""; try {/* w w w . ja v a2s.c o m*/ MessageDigest md = MessageDigest.getInstance("MD5"); byte[] digestOfPassword = md.digest(texto.getBytes("utf-8")); byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24); Md5Crypt.md5Crypt(keyBytes); SecretKey key = new SecretKeySpec(keyBytes, "DESede"); Cipher cipher = Cipher.getInstance("DESede"); cipher.init(Cipher.ENCRYPT_MODE, key); byte[] plainTextBytes = texto.getBytes("utf-8"); byte[] buf = cipher.doFinal(plainTextBytes); byte[] base64Bytes = Base64.encodeBase64(buf); base64EncryptedString = new String(base64Bytes); } catch (Exception ex) { } return base64EncryptedString; }