java:
public static String md5(String str){
String MD5 = "";
try{
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(str.getBytes("UTF-8"));
byte byteData[] = md.digest();
StringBuffer sb = new StringBuffer();
for(int i = 0 ; i < byteData.length ; i++)
sb.append(Integer.toString((byteData[i]&0xff) + 0x100, 16).substring(1));
MD5 = sb.toString();
}
catch(NoSuchAlgorithmException e) { e.printStackTrace(); MD5 = null; }
catch (UnsupportedEncodingException e) { e.printStackTrace(); MD5 = null; }
return MD5;
}
kotlin:
private fun md5(input:String): String {
val md = MessageDigest.getInstance("MD5")
return BigInteger(1, md.digest(input.toByteArray())).toString(16).padStart(32, '0')
}
출처
https://stackoverflow.com/questions/64171624/how-to-generate-an-md5-hash-in-kotlin
public static String sha256(String str) {
String SHA = "";
try{
MessageDigest sh = MessageDigest.getInstance("SHA-256");
sh.update(str.getBytes());
byte byteData[] = sh.digest();
StringBuffer sb = new StringBuffer();
for(int i = 0 ; i < byteData.length ; i++)
sb.append(Integer.toString((byteData[i]&0xff) + 0x100, 16).substring(1));
SHA = sb.toString();
}catch(NoSuchAlgorithmException e) { e.printStackTrace(); SHA = null; }
return SHA;
}