Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java中的Sha哈希_Java_Hash_Sha1 - Fatal编程技术网

Java中的Sha哈希

Java中的Sha哈希,java,hash,sha1,Java,Hash,Sha1,因此,我正在进行一个项目,需要在存储用户密码之前对其进行哈希运算(用于登录提示)。实际上散列文本的代码工作得很好,但我正试图从另一个类中使用它。问题是我得到了以下错误,我不知道这意味着什么 1 error found: File: /Users/justin/Desktop/Culminating Java/login.java [line: 10] Error: /Users/justin/Desktop/Culminating Java/login.java:10: unreported

因此,我正在进行一个项目,需要在存储用户密码之前对其进行哈希运算(用于登录提示)。实际上散列文本的代码工作得很好,但我正试图从另一个类中使用它。问题是我得到了以下错误,我不知道这意味着什么

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();
    }
}

如果仔细阅读输出,它会说:

错误:/Users/justin/Desktop/concentering Java/login.Java:10:unreported exception Java.security.nosuchagorithmexception必须捕获或声明要抛出

这意味着它在编译时是一个已检查的异常,您必须在
login.java
中处理它。或者围绕对
myhasher.hasher()的调用包装一个try,catch
并捕获
nosuchagorithmexception

public static void main (String args[]){
    HashTextTest myhasher = new HashTextTest();
    try {
        myhasher.hasher();
    } catch(NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
}
或者向main方法添加一个
抛出nosuchagorithmexception
,如下所示:-


publicstaticvoidmain(stringargs[])在main中抛出nosuchalgorithexception

myhasher.hasher()
可能抛出异常
nosuchalgorithexception
,因此您必须在那里捕获它或在方法声明中使用throws子句。
public static void main (String args[]){
    HashTextTest myhasher = new HashTextTest();
    try {
        myhasher.hasher();
    } catch(NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
}