1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
|
private static String SHA(String text, String type){ String result = null; if (text != null && text.length() > 0){ try { MessageDigest messageDigest = MessageDigest.getInstance(type); messageDigest.update(text.getBytes(StandardCharsets.UTF_8)); byte[] bytes = messageDigest.digest(); StringBuilder stringBuilder = new StringBuilder(); for (int i = 0; i < bytes.length; i++) { String hex = Integer.toHexString(0xff & bytes[i]); if (hex.length() == 1){ stringBuilder.append("0"); } stringBuilder.append(hex); } result = stringBuilder.toString(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } } return result; }
|