package LoginDb;

import javax.swing.*;
import sun.misc.BASE64Encoder;
//import java.util.Base64;

public class SimpleCrypt {

    public static String doEnrcypt(String sArgument) {
        String encoded = null;

        try {
            String secret = new String(encrypt(sArgument));
            sun.misc.BASE64Encoder encoder = new sun.misc.BASE64Encoder();
          //  encoded = encoder.encode(secret.getBytes());

            //encoded = Base64.getEncoder().encodeToString(secret.getBytes()); //strictly from jdk 9 or later
            System.out.println("the encrypted result : " + encoded);
        } catch (Exception e) {
            // e.printStackTrace();
            System.out.println("Exception occurred during password encryption.");
        } finally {
            System.out.println("Processing done.");
        }
        return encoded;
    }

    public static byte[] encrypt(String x) {
        java.security.MessageDigest d = null;

        try {
            d = java.security.MessageDigest.getInstance("SHA-256");//SHA-1
            d.reset();
            d.update(x.getBytes());
        } catch (Exception e) {
            System.out.println("Error occurred during processing.");
        } finally {
            System.out.println("Processing done.");
        }
        return d.digest();
    }
}
