问题描述
我有一个问题产生正确的哈希由Android工作室给定的字符串。我也读了不少解决方案,而无需了解如何转换的正确方法。
我需要正确的哈希值,因为我想提出一个与它的HTTP请求。
下面是我的code在JAVA:
公共字符串getHash(最后弦乐appSecret,最后弦乐sessionToken)抛出抛出:NoSuchAlgorithmException,UnsupportedEncodingException { 字符串输入= sessionToken + appSecret;
消息摘要消化= MessageDigest.getInstance(SHA-256);
digest.reset(); 字节[] = byteData digest.digest(input.getBytes(UTF-8));
StringBuffer的SB =新的StringBuffer(); 的for(int i = 0; I< byteData.length;我++){
sb.append(的String.format(%02X,为0xFF&安培; byteData [I]));
}
返回sb.toString(); }
有关等的输入:
<$p$p><$c$c>1130_11825_253402300799_1_1bcb4a27d42524de11325ec627b63878770a8651c0a0d8ddfc8fc06b92aea281634ff11f7d874c03851932304601439e我所需要的精确的输出:
01a9d698f0587a25ad8ef56b0994ec0022364aff91d668a4b3a4b97c40167672
但我得到一个错误的输出:
a60f61b5e9f832b153a91e8d2b1ffa28b9611b2d60c3669663cfe050ac8e28cc
我想我的问题是如何读/打印字符串,但我无法弄清楚如何纠正它。
我知道,一个在线散列运算返回正确的哈希值。
谢谢你。
我修改 getHash
采取AA单字符串
,我去掉了调用复位()
和你完成消化()
。我也preFER的。就像,
公共静态字符串getHash(最终弦乐味精){
StringBuilder的SB =新的StringBuilder();
尝试{
消息摘要消化= MessageDigest.getInstance(SHA-256);
digest.update(msg.getBytes());
字节[] = byteData digest.digest();
对于(字节x:byteData){
字符串str = Integer.toHexString(Byte.toUnsignedInt(X));
如果(str.length()2){
sb.append('0');
}
sb.append(STR);
}
}赶上(例外五){
e.printStackTrace();
}
返回sb.toString();
}
和调用它像
公共静态无效的主要(字串[] args){
串出= getHash(\"1130_11825_253402300799_1_1bcb4a27d42524de11325ec627b63878770a8651c0a0d8ddfc8fc06b92aea281634ff11f7d874c03851932304601439e\");
预计字符串=01a9d698f0587a25ad8ef56b0994ec0022364aff91d668a4b3a4b97c40167672;
的System.out.println(out.equals(预期));
}
我得到
真
I have a problem generating a correct hash for a given string by android studio. I have read a lot of solutions without understanding how to convert it the right way.I need the correct hash since i am making an HTTP request with it.
Here is my code in JAVA:
public String getHash(final String appSecret , final String sessionToken)throws NoSuchAlgorithmException ,UnsupportedEncodingException{
String input = sessionToken + appSecret;
MessageDigest digest = MessageDigest.getInstance("SHA-256");
digest.reset();
byte[] byteData = digest.digest(input.getBytes("UTF-8"));
StringBuffer sb = new StringBuffer();
for (int i = 0; i < byteData.length; i++){
sb.append(String.format("%02x", 0xFF & byteData[i]));
}
return sb.toString();
}
For an input like:
1130_11825_253402300799_1_1bcb4a27d42524de11325ec627b63878770a8651c0a0d8ddfc8fc06b92aea281634ff11f7d874c03851932304601439e
I need the exact output:
01a9d698f0587a25ad8ef56b0994ec0022364aff91d668a4b3a4b97c40167672
but i got a wrong output:
a60f61b5e9f832b153a91e8d2b1ffa28b9611b2d60c3669663cfe050ac8e28cc
I think my problem is how to read/print the string but i can't figure out how to correct it.I know that an online hash calculator return a correct hash.Thanks.
I modified getHash
to take a a single String
, I removed the call to reset()
and you to finish digest()
. I also prefer the for each
loop. Like,
public static String getHash(final String msg) {
StringBuilder sb = new StringBuilder();
try {
MessageDigest digest = MessageDigest.getInstance("SHA-256");
digest.update(msg.getBytes());
byte[] byteData = digest.digest();
for (byte x : byteData) {
String str = Integer.toHexString(Byte.toUnsignedInt(x));
if (str.length() < 2) {
sb.append('0');
}
sb.append(str);
}
} catch (Exception e) {
e.printStackTrace();
}
return sb.toString();
}
And call it like
public static void main(String[] args) {
String out = getHash("1130_11825_253402300799_1_1bcb4a27d42524de11325ec627b63878770a8651c0a0d8ddfc8fc06b92aea281634ff11f7d874c03851932304601439e");
String expected = "01a9d698f0587a25ad8ef56b0994ec0022364aff91d668a4b3a4b97c40167672";
System.out.println(out.equals(expected));
}
I get
true
这篇关于在Android的SHA256散列的错误calculatin的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!