1

I've written a code for a login form that compares the generated hash from the password input with a saved hash. The problem is that when i try to compile it, it gives me the following error due to exceptions:

Exception in thread "AWT-EventQueue-0" java.lang.Error: Unresolved compilation problem: Unhandled exception type NoSuchAlgorithmException.

I am currently using Eclipse for Java EE 4.7.3a with Java SE - 1.8 .

This is the source code:

import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import java.awt.Font;
import java.awt.Color;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.security.MessageDigest;

public class Login {

private JFrame frame;
private JPasswordField password;
private JTextField username;

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                Login window = new Login();
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the application.
 */
public Login() {
    initialize();
}

/**
 * Initialize the contents of the frame.
 */
private void initialize() {
    frame = new JFrame();
    frame.setBounds(100, 100, 450, 338);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);

    JLabel lblUsername = new JLabel("Username");
    lblUsername.setFont(new Font("Tahoma", Font.PLAIN, 20));
    lblUsername.setBounds(52, 78, 105, 36);
    frame.getContentPane().add(lblUsername);

    password = new JPasswordField();
    password.setBounds(208, 158, 144, 26);
    frame.getContentPane().add(password);

    username = new JTextField();
    username.setBounds(206, 83, 146, 26);
    frame.getContentPane().add(username);
    username.setColumns(10);

    JLabel lblPassword = new JLabel("Password");
    lblPassword.setFont(new Font("Tahoma", Font.PLAIN, 20));
    lblPassword.setBounds(52, 149, 105, 41);
    frame.getContentPane().add(lblPassword);

    JLabel lblLogin = new JLabel("LOGIN");
    lblLogin.setFont(new Font("Yu Gothic Medium", Font.PLAIN, 35));
    lblLogin.setForeground(Color.RED);
    lblLogin.setBounds(147, -15, 117, 112);
    frame.getContentPane().add(lblLogin);

    JButton btnLogin = new JButton("Login");
    btnLogin.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            String uname = username.getText();
            String pwd = password.getText();
            String sh = "SAVEDSHA256HASH";
            MessageDigest md = MessageDigest.getInstance("SHA-256");
            md.update(pwd.getBytes());
            byte byteData[] = md.digest();
            StringBuffer sb = new StringBuffer();
            for(int i = 0; i < byteData.length; i++) {
                sb.append(Integer.toString((byteData[i] & 0xff) + 0x100, 16).substring(1));
            }
            String ch = sb.toString();
            if(uname.equals("username") && ch.equals(sh)) {
                JOptionPane.showMessageDialog(frame, "Access Granted");
            } else {
                JOptionPane.showMessageDialog(frame, "Invalid username or password");
            }
            }
        });
    btnLogin.setBounds(147, 221, 117, 29);
    frame.getContentPane().add(btnLogin);
    }
`}

Contact me if you need further informations

Thanks

1 Answer 1

2
MessageDigest.getInstance("SHA-256")

Method throws NoSuchAlgorithmException. You need to add try catch block around that statement.

                try {
                    md = MessageDigest.getInstance("SHA-256");
                } catch (NoSuchAlgorithmException e) {
                    e.printStackTrace();
                }
Sign up to request clarification or add additional context in comments.

2 Comments

Hi, i have tried your solution and it doesn't work in fact it gives me another error: Exception in thread "AWT-EventQueue-0" java.lang.Error: Unresolved compilation problems: NoSuchAlgorithmException cannot be resolved to a type md cannot be resolved md cannot be resolved
did u add import import java.security.NoSuchAlgorithmException;?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.