【发布时间】:2020-06-29 05:01:20
【问题描述】:
我在 Hackerrank.com 上解决了挑战,遇到了有关 java SHA-256 加密哈希函数的挑战。 here
我编写了以下代码作为解决方案。但是我的解决方案有些测试用例失败了。希望知道我的代码出了什么问题。
public class Solution {
public static String toHexString(byte[] hash)
{
BigInteger number = new BigInteger(1, hash);
StringBuilder hexString = new StringBuilder(number.toString(16));
while (hexString.length() < 32)
{
hexString.insert(0, '0');
}
return hexString.toString();
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String input = sc.next();
try
{
MessageDigest md = MessageDigest.getInstance("SHA-256");
System.out.println(toHexString(md.digest(input.getBytes(StandardCharsets.UTF_8))));
}
// For specifying wrong message digest algorithms
catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
}
}
【问题讨论】:
-
有助于了解预期的答案/结果应该是什么......
-
在图片中可以看到程序的预期输出
标签: java java-7 sha256 cryptographic-hash-function