我是Java编程的新手。
我想获取某些给定字符串的sha-256哈希。
我得到:
㔴晥㜶晥㜶㔴扡㔴晥扡㌲㌲㌲摣㔴㜶㌲扡扡㌲㌲㜶㜶晥㔴
但是期望像这样:
074cfbbad12bade66c92d01142b83c8f2b24030959980151f83972f4575a43e1
这是我的代码:
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class HelloWorld{
private static final char[] DIGITS;
static {
char[] arrc;
char[] arrc2 = arrc = new char[16];
arrc2[0] = 12592;
arrc2[1] = 13106;
arrc2[2] = 13620;
arrc2[3] = 14134;
arrc2[4] = 14648;
arrc2[5] = 25185;
arrc2[6] = 25699;
arrc2[7] = 26213;
DIGITS = arrc;
}
public static void main(String []args){
StringBuilder localStringBuilder = new StringBuilder();
localStringBuilder.append(55);
localStringBuilder.append("market-gap");
localStringBuilder.append("marjet-gre");
localStringBuilder.append("1");
localStringBuilder.append("05/19/1989");
localStringBuilder.append("6.3.0");
localStringBuilder.append("0");
localStringBuilder.append("");
localStringBuilder.append("en");
localStringBuilder.append("");
localStringBuilder.append("00e0533117bb8707");
String paramString1 = localStringBuilder.toString();
String strrrr = sha256(paramString1);
System.out.println(strrrr);
}
public static String encodeHex(byte[] arrby) {
StringBuilder stringBuilder = new StringBuilder(arrby.length * 2);
for (int i = 0; i < arrby.length; ++i) {
stringBuilder.append(DIGITS[(arrby[i] & 240) >>> 4]);
stringBuilder.append(DIGITS[arrby[i] & 15]);
}
return stringBuilder.toString();
}
public static String sha256(String paramString)
{
try
{
MessageDigest localMessageDigest = MessageDigest.getInstance("SHA-256");
return encodeHex(localMessageDigest.digest(paramString.getBytes()));
}
catch (NoSuchAlgorithmException e)
{
//ThrowableExtension.printStackTrace(e);
}
return null;
}
}
我在这里做错了什么?
致谢
答案 0 :(得分:0)
您的十六进制编码器看起来非常可疑。它应该如何工作?
您可以尝试JDK中已经存在的转换器:
public static String sha256(String paramString) {
try {
MessageDigest localMessageDigest = MessageDigest.getInstance("SHA-256");
byte[] binaryDigest = localMessageDigest.digest(paramString.getBytes());
return DatatypeConverter.printHexBinary(binaryDigest);
} catch (NoSuchAlgorithmException e) {
// ThrowableExtension.printStackTrace(e);
}
return null;
}
编辑:
好的,知道了。
您的DIGITS
值错误。这是一个工作版本:
public class HelloWorld {
private static final char[] DIGITS = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
public static void main(String[] args) {
StringBuilder localStringBuilder = new StringBuilder();
localStringBuilder.append(55);
localStringBuilder.append("market-gap");
localStringBuilder.append("marjet-gre");
localStringBuilder.append("1");
localStringBuilder.append("05/19/1989");
localStringBuilder.append("6.3.0");
localStringBuilder.append("0");
localStringBuilder.append("");
localStringBuilder.append("en");
localStringBuilder.append("");
localStringBuilder.append("00e0533117bb8707");
String paramString1 = localStringBuilder.toString();
String strrrr = sha256(paramString1);
System.out.println(strrrr);
}
public static String encodeHex(byte[] arrby) {
StringBuilder stringBuilder = new StringBuilder(arrby.length * 2);
for (int i = 0; i < arrby.length; ++i) {
stringBuilder.append(DIGITS[(arrby[i] & 0xF0) >>> 4]);
stringBuilder.append(DIGITS[arrby[i] & 0x0F]);
}
return stringBuilder.toString();
}
public static String sha256(String paramString) {
try {
MessageDigest localMessageDigest = MessageDigest.getInstance("SHA-256");
return encodeHex(localMessageDigest.digest(paramString.getBytes()));
} catch (NoSuchAlgorithmException e) {
// ThrowableExtension.printStackTrace(e);
}
return null;
}
}
此外,出于可读性考虑,我将整数240
和15
更改为0xF0
和0x0F
。