SHA-512 hashing with Java
Asked Answered
A

5

10

I was wondering if I can SHA-512 hash a string on Android Studio without a salt.

I've seen other questions, but they all involve the salt, but I want something like this:

TestBox.text = sha512("Hello, world!");

And TextBox will read c1527cd893c124773d811911970c8fe6e857d6df5dc9226bd8a160614c0cd963a4ddea2b94bb7d36021ef9d865d5cea294a82dd49a0bb269f51f6e7a57f79421;

Allsun answered 1/10, 2017 at 7:53 Comment(1)
This is not an Android-specific question, and can be tackled using standard Java libraries.Classmate
P
18

The other questions you saw use salt so just don't use salt like so:

MessageDigest md = MessageDigest.getInstance("SHA-512");
byte[] digest = md.digest("Hello, world!".getBytes());
StringBuilder sb = new StringBuilder();
for (int i = 0; i < digest.length; i++) {
    sb.append(Integer.toString((digest[i] & 0xff) + 0x100, 16).substring(1));
}
System.out.println(sb);

Based on this answer.

Partlow answered 1/10, 2017 at 8:7 Comment(0)
E
6

Keds example is right, but .length will always be biger than 32, (127) so more correct answer should be

private fun getSHA512(input:String):String{
    val md: MessageDigest = MessageDigest.getInstance("SHA-512")
    val messageDigest = md.digest(input.toByteArray())

    // Convert byte array into signum representation
    val no = BigInteger(1, messageDigest)

    // Convert message digest into hex value
    var hashtext: String = no.toString(16)

    // Add preceding 0s to make it 128 chars long
    while (hashtext.length < 128) {
        hashtext = "0$hashtext"
    }



    // return the HashText
    return hashtext
}
Euthanasia answered 21/3, 2021 at 14:50 Comment(0)
P
2

method for calculating sha-512 in kotlin

 fun getSHA256(input:String):String{
    return MessageDigest
        .getInstance("SHA-256")
        .digest(input.toByteArray())
        .fold("") { str, it -> str + "%02x".format(it) }
}
Paly answered 17/3, 2020 at 10:29 Comment(1)
Here the length of has txt should be 128 so if less than that is found then needs to insert the '0' at the starting of the string Please Edit this. // Add preceding 0s to make it 128 chars long while (hashtext.length < 128) { hashtext = "0$hashtext" }Caddie
S
0

A more functional solution:

fun sha512(input: String): String {
        return MessageDigest.getInstance("SHA-512")
            .digest(input.toByteArray())
            .joinToString(separator = "") {
                ((it.toInt() and 0xff) + 0x100)
                    .toString(16)
                    .substring(1) 
            }
    }
Sitology answered 13/9, 2021 at 12:2 Comment(0)
P
-2

I guess the equivalent in Kotlin is:

fun encriptar(cadena: String): String {

    var md: MessageDigest = MessageDigest.getInstance("SHA-512")
    var digest = md.digest(cadena.toByteArray())
    var sb: StringBuilder = StringBuilder()

    var i = 0
    while (i < digest.count()) {
        sb.append(((digest[i] and 0xff.toByte()) + 0x100).toString(16).substring(0, 1))
        i++
    }

    return sb.toString()
}
Pamalapamela answered 15/7, 2020 at 11:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.