如何在 Android 中计算字符串的 SHA-256 哈希值?
我正在尝试获取 Android 中字符串的 SHA256。
这是我想要匹配的 PHP 代码:
echo bin2hex(mhash(MHASH_SHA256,"asdf"));
//outputs "f0e4c2f76c58916ec258f246851bea091d14d4247a2fc3e18694461b1816e13b"
现在,在 Java 中,我尝试执行以下操作:
String password="asdf"
MessageDigest digest=null;
try {
digest = MessageDigest.getInstance("SHA-256");
} catch (NoSuchAlgorithmException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
digest.reset();
try {
Log.i("Eamorr",digest.digest(password.getBytes("UTF-8")).toString());
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
但这打印出:“a42yzk3axdv3k4yh98g8”
我在这里做错了什么?
感谢埃里克森的解决方案:
Log.i("Eamorr",bin2hex(getHash("asdf")));
public byte[] getHash(String password) {
MessageDigest digest=null;
try {
digest = MessageDigest.getInstance("SHA-256");
} catch (NoSuchAlgorithmException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
digest.reset();
return digest.digest(password.getBytes());
}
static String bin2hex(byte[] data) {
return String.format("%0" + (data.length*2) + "X", new BigInteger(1, data));
}
I'm trying to get the SHA256 of a string in Android.
Here is the PHP code that I want to match:
echo bin2hex(mhash(MHASH_SHA256,"asdf"));
//outputs "f0e4c2f76c58916ec258f246851bea091d14d4247a2fc3e18694461b1816e13b"
Now, in Java, I'm trying to do the following:
String password="asdf"
MessageDigest digest=null;
try {
digest = MessageDigest.getInstance("SHA-256");
} catch (NoSuchAlgorithmException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
digest.reset();
try {
Log.i("Eamorr",digest.digest(password.getBytes("UTF-8")).toString());
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
But this prints out: "a42yzk3axdv3k4yh98g8"
What did I do wrong here?
Solution thanks to erickson:
Log.i("Eamorr",bin2hex(getHash("asdf")));
public byte[] getHash(String password) {
MessageDigest digest=null;
try {
digest = MessageDigest.getInstance("SHA-256");
} catch (NoSuchAlgorithmException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
digest.reset();
return digest.digest(password.getBytes());
}
static String bin2hex(byte[] data) {
return String.format("%0" + (data.length*2) + "X", new BigInteger(1, data));
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我知道这个问题已经得到解答,但我在 android arsenal 找到了一个哈希库,它非常简单,简单,只有一个行代码。可以散列 MD5、SHA-1、SHA-256、SHA-384 或 SHA-512。
首先将其添加到您的 gradle 并同步
实现 'com.github.1AboveAll:Hasher:1.2'
开始拥有
Hasher.Companion.hash("Hello",HashType.SHA_1);
i know this has been answered but i found a Hashing Library at android arsenal and its very easy,simple and just one line of code. can hash MD5, SHA-1, SHA-256, SHA-384, or SHA-512.
first add this to your gradle and sync
implementation 'com.github.1AboveAll:Hasher:1.2'
start hasing
Hasher.Companion.hash("Hello",HashType.SHA_1);
PHP 函数
bin2hex
意味着它接受一个字节字符串并将其编码为十六进制数。在 Java 代码中,您尝试获取一堆随机字节并使用平台的默认字符编码将它们解码为字符串。这是行不通的,而且即使行得通,也不会产生相同的结果。
这是 Java 的快速而肮脏的二进制到十六进制转换:
这编写很快,但执行起来不一定很快。如果你做了很多这样的事情,你应该用更快的实现来重写函数。
The PHP function
bin2hex
means that it takes a string of bytes and encodes it as a hexadecimal number.In the Java code, you are trying to take a bunch of random bytes and decode them as a string using your platform's default character encoding. That isn't going to work, and if it did, it wouldn't produce the same results.
Here's a quick-and-dirty binary-to-hex conversion for Java:
This is quick to write, not necessarily quick to execute. If you are doing a lot of these, you should rewrite the function with a faster implementation.
您的思路是正确的,但转换字节有点复杂。这适用于我的设备:
来源:bytesToHexString 函数来自 IOSched 项目。
You are along the right lines, but converting the bytes is a little more complicated. This works on my device:
Source: bytesToHexString function is from the IOSched project.
通过使用示例完成答案,感谢埃里克森。
使用示例:
Complete answer with use example, thanks to erickson.
Example of use: