lazexe
04/06/2018, 7:47 AMJava
public class Ut {
public static String sha256(String input) throws NoSuchAlgorithmException {
MessageDigest mDigest = MessageDigest.getInstance("SHA-256");
byte[] result = mDigest.digest(input.getBytes());
StringBuilder sb = new StringBuilder();
for (byte aResult : result) {
sb.append(Integer.toString((aResult & 0xff) + 0x100, 16).substring(1));
}
return sb.toString();
}
}
Kotlin auto converter
@Throws(NoSuchAlgorithmException::class)
fun String.sha256(): String {
val mDigest = MessageDigest.getInstance("SHA-256")
val result = mDigest.digest(this.toByteArray())
val sb = StringBuilder()
for (aResult in result) {
sb.append(Integer.toString((aResult and 0xff) + 0x100, 16).substring(1))
}
return sb.toString()
}
And I got error: http://prntscr.com/j1lw9b
Autofix fixed it: http://prntscr.com/j1lwkr
And test do not pass well 😞 http://prntscr.com/j1lx30
Please suggest me how to fix this line sb.append(Integer.toString((aResult and 0xff) + 0x100, 16).substring(1))