使用 Android 的 SHA-512 哈希
SHA-512 hashing with Android
我想知道我是否可以在 Android Studio 上不使用 salt.
对字符串进行 SHA-512 哈希处理
我看过其他问题,但它们都涉及 salt
,但我想要这样的问题:
TestBox.text = sha512("Hello, world!");
而 TextBox
将读作 c1527cd893c124773d811911970c8fe6e857d6df5dc9226bd8a160614c0cd963a4ddea2b94bb7d36021ef9d865d5cea294a82dd49a0bb269f51f6e7a57f79421
;
您看到的其他问题使用盐,所以不要像这样使用盐:
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);
基于此。
kotlin中计算sha-512的方法
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 32 bit
while (hashtext.length < 32) {
hashtext = "0$hashtext"
}
// return the HashText
return hashtext
}
我猜 Kotlin 中的等价物是:
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()
}
Keds 的例子是正确的,但是 .length 总是大于 32,(127) 所以更正确的答案应该是
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
}
更实用的解决方案:
fun sha512(input: String): String {
return MessageDigest.getInstance("SHA-512")
.digest(input.toByteArray())
.joinToString(separator = "") {
((it.toInt() and 0xff) + 0x100)
.toString(16)
.substring(1)
}
}
我想知道我是否可以在 Android Studio 上不使用 salt.
对字符串进行 SHA-512 哈希处理我看过其他问题,但它们都涉及 salt
,但我想要这样的问题:
TestBox.text = sha512("Hello, world!");
而 TextBox
将读作 c1527cd893c124773d811911970c8fe6e857d6df5dc9226bd8a160614c0cd963a4ddea2b94bb7d36021ef9d865d5cea294a82dd49a0bb269f51f6e7a57f79421
;
您看到的其他问题使用盐,所以不要像这样使用盐:
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);
基于此
kotlin中计算sha-512的方法
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 32 bit
while (hashtext.length < 32) {
hashtext = "0$hashtext"
}
// return the HashText
return hashtext
}
我猜 Kotlin 中的等价物是:
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()
}
Keds 的例子是正确的,但是 .length 总是大于 32,(127) 所以更正确的答案应该是
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
}
更实用的解决方案:
fun sha512(input: String): String {
return MessageDigest.getInstance("SHA-512")
.digest(input.toByteArray())
.joinToString(separator = "") {
((it.toInt() and 0xff) + 0x100)
.toString(16)
.substring(1)
}
}