Sunday, June 12, 2022
HomeWordPress DevelopmentAuthentication with API Key in Java

Authentication with API Key in Java


import static java.nio.charset.StandardCharsets.UTF_8;

  

import java.safety.MessageDigest;

import java.safety.NoSuchAlgorithmException;

import java.sql.*;

import java.time.LocalDate;

import java.time.format.DateTimeFormatter;

public class CreateAndUpdateAPIKey {

    Connection conn;

    

    public String generateMD5Hashvalue(String userName)

    {

        LocalDate dateObj = LocalDate.now();

        DateTimeFormatter formatter

            = DateTimeFormatter.ofPattern("yyyyMMdd");

        String date = dateObj.format(formatter);

  

        MessageDigest md;

        attempt {

            md = MessageDigest.getInstance("MD5");

        }

        catch (NoSuchAlgorithmException e) {

            throw new IllegalArgumentException(e);

        }

        String secretPhase

            = "geeks";

        System.out.println("Present Date : " + date);

        System.out.println("Login Id : " + userName);

        System.out.println("Secret Section : " + secretPhase);

  

        

        

        byte[] hashResult

            = md.digest((date + userName + secretPhase)

                            .getBytes(UTF_8));

        

        String password = bytesToHex(hashResult);

        System.out.println("Generated password.."

                           + password);

  

        return password;

    }

    non-public String bytesToHex(byte[] bytes)

    {

        StringBuilder sb = new StringBuilder();

        for (byte b : bytes) {

            sb.append(String.format("%02x", b));

        }

        return sb.toString();

    }

  

    public static void primary(String[] args)

    {

        

        

        

        new CreateAndUpdateAPIKey("customers");

    }

  

    public CreateAndUpdateAPIKey(String tableName)

    {

        attempt {

            

            Class.forName("com.mysql.jdbc.Driver")

                .newInstance();

            String url

            conn = DriverManager.getConnection(url, "root",

                                               "admin");

            doSelectAndUpdate(tableName);

            conn.shut();

        }

        catch (ClassNotFoundException ex) {

            System.err.println(ex.getMessage());

        }

        catch (IllegalAccessException ex) {

            System.err.println(ex.getMessage());

        }

        catch (InstantiationException ex) {

            System.err.println(ex.getMessage());

        }

        catch (SQLException ex) {

            System.err.println(ex.getMessage());

        }

    }

  

    non-public void doSelectAndUpdate(String tableName)

    {

        doSelect(tableName);

    }

  

    non-public void doSelect(String tableName)

    {

        String question = null, userName = null;

        

        

        

        question = "SELECT * FROM customers";

  

        attempt {

            Assertion st = conn.createStatement();

            ResultSet rs = st.executeQuery(question);

            whereas (rs.subsequent()) {

                

                

                userName = rs.getString("loginId");

                

                String password

                    = generateMD5Hashvalue(userName);

                

                doUpdate(password, userName, tableName);

                System.out.println(userName + ":"

                                   + password);

            }

        }

        catch (SQLException ex) {

            System.err.println(ex.getMessage());

        }

    }

  

    non-public void doUpdate(String apiKey, String userName,

                          String tableName)

    {

        System.out.print("n[Performing UPDATE] ... ");

        attempt {

            Assertion st = conn.createStatement();

            

            

            String sqlUpdate = null;

            sqlUpdate = "UPDATE customers "

                        + "SET apikey = ? "

                        + "WHERE loginId = ?";

  

            PreparedStatement pstmt

                = conn.prepareStatement(sqlUpdate);

  

            pstmt.setString(1, apiKey);

            pstmt.setString(2, userName);

  

            int rowAffected = pstmt.executeUpdate();

            System.out.println(String.format(

                "Row affected %d", rowAffected));

        }

        catch (SQLException ex) {

            System.err.println(ex.getMessage());

        }

    }

}

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments