【发布时间】:2015-04-19 21:53:24
【问题描述】:
所以我正在做一个项目,我需要在存储用户密码之前对其进行哈希处理(用于登录提示)。实际上散列文本的代码很好,但我试图在另一个类中使用它。问题是我收到以下错误,我不知道它是什么意思。
1 error found:
File: /Users/justin/Desktop/Culminating Java/login.java [line: 10]
Error: /Users/justin/Desktop/Culminating Java/login.java:10: unreported exception java.security.NoSuchAlgorithmException; must be caught or declared to be thrown
这是散列的代码
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class HashTextTest {
/**
* @param args
* @throws NoSuchAlgorithmException
*/
public static void hasher() throws NoSuchAlgorithmException {
System.out.println(sha1(login.InputPassword));
System.out.println(sha1("password"));
}
static String sha1(String input) throws NoSuchAlgorithmException {
MessageDigest mDigest = MessageDigest.getInstance("SHA1");
byte[] result = mDigest.digest(input.getBytes());
StringBuffer sb = new StringBuffer();
for (int i = 0; i < result.length; i++) {
sb.append(Integer.toString((result[i] & 0xff) + 0x100, 16).substring(1));
}
return sb.toString();
}
}
这就是我尝试使用它的方式。
import javax.swing.*;
import java.io.*;
public class login{
public static String InputPassword = "";
public static void main (String args[]){
HashTextTest myhasher = new HashTextTest();
myhasher.hasher();
}
}
【问题讨论】:
-
在 main 中,
myhasher.hasher()可能会抛出异常NoSuchAlgorithmException,因此您必须在那里捕获它或在方法声明中使用 throws 子句。