我试图得到一个字符串的Android SHA256。
下面是我想匹配的PHP code:
回声BIN2HEX(mhash(MHASH_SHA256,ASDF));
//输出f0e4c2f76c58916ec258f246851bea091d14d4247a2fc3e18694461b1816e13b
现在,在Java中,我试图做到以下几点:
字符串密码=ASDF
消息摘要消化= NULL;
尝试 {
摘要= MessageDigest.getInstance(SHA-256);
}赶上(抛出:NoSuchAlgorithmException E1){
// TODO自动生成的catch块
e1.printStackTrace();
}
digest.reset();
尝试 {
Log.i(Eamorr,digest.digest(password.getBytes(UTF-8))的toString());
}赶上(UnsupportedEncodingException E){
// TODO自动生成的catch块
e.printStackTrace();
}
但这种打印出:a42yzk3axdv3k4yh98g8
我做了什么错在这里?
解决方案感谢埃里克森:
Log.i(Eamorr,BIN2HEX(getHash(ASDF)));
公共字节[] getHash(字符串密码){
消息摘要消化= NULL;
尝试 {
摘要= MessageDigest.getInstance(SHA-256);
}赶上(抛出:NoSuchAlgorithmException E1){
// TODO自动生成的catch块
e1.printStackTrace();
}
digest.reset();
返回digest.digest(password.getBytes());
}
静态字符串BIN2HEX(byte []的数据){
返回的String.Format(%0+(data.length * 2)+X的,新的BigInteger(1,数据));
}
解决方案
PHP函数 BIN2HEX
意味着需要字节串和连接codeS作为一个十六进制数。
在Java的code,你正在试图采取了一堆随机字节和DE code他们为使用平台的默认字符编码的字符串。这是不会工作,并且如果它没有,它不会产生相同的结果。
下面是一个快速和肮脏的二进制到十六进制转换为Java的:
静态字符串BIN2HEX(byte []的数据){
返回的String.Format(%0+(data.length * 2)+'X',新的BigInteger(1,数据));
}
这是很快的写的,不一定快的执行。如果你是做了很多的这些,你应该重写功能,以更快地实现。
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));
}
解决方案
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:
static String bin2hex(byte[] data) {
return String.format("%0" + (data.length * 2) + 'x', new BigInteger(1, data));
}
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.
上一篇:致命异常:GLThread 84:FileNotFoundException异常的Android logcat的错误异常、错误、GLThread、FileNotFoundException
下一篇:获得NullPointerException异常,而试图从EditText上提取文本异常、文本、NullPointerException、EditText