<commit_before>package net.i2p.itoopie.gui;

import java.awt.BorderLayout;

import javax.security.cert.X509Certificate;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

import net.i2p.itoopie.i18n.Transl;
import net.i2p.itoopie.security.CertificateHelper;
import net.i2p.itoopie.security.CertificateManager;

public class CertificateGUI {
	private static boolean isVerifying = false;
	
	public static void main(String[] args){
		System.out.println("Save new cert: " + saveNewCert(null,null));	
		System.out.println("Overwrite cert: " + overwriteCert(null,null));		
	}

	public static boolean saveNewCert(String hostname, X509Certificate cert){
		if (!isVerifying){
			isVerifying = true;
			JFrame frame = new JFrame();
			frame.setLayout(new BorderLayout());
			JButton bt = new JButton();
			bt.setText("text");
			frame.add(bt, BorderLayout.NORTH);
			
			String title = Transl._("New remote host detected");
			String hostString = Transl._("Would you like permanently trust the certificate from the remote host " + hostname + "?");
			
			String certName = "N/A";
			String certAlgo = "N/A";
			String certSerial = "N/A";
			String certThumb = "N/A";
			if (cert != null){
				certName = cert.getSubjectDN().getName();
				String certString = cert.getPublicKey().toString();
				certAlgo = certString.substring(0,certString.indexOf("\n"));
				certSerial = String.valueOf(cert.getPublicKey().serialVersionUID);
				certThumb = CertificateHelper.getThumbPrint(cert);
			}
			String certInfo = "<html>"+Transl._("Certificate info") + "<br><br>" +
					Transl._("Name: ") + certName + "<br>" +
					Transl._("Algorithm: ") + certAlgo + "<br>" +
					Transl._("Serial: ") + certSerial + "<br>" +
					Transl._("SHA-1 ID-hash: ") + certThumb;
			
			String textContent = certInfo + "<br><br>" + hostString;
			 
			int n = JOptionPane.showConfirmDialog(
			    frame,
			    textContent,
			    title,
			    JOptionPane.YES_NO_OPTION,
			    JOptionPane.INFORMATION_MESSAGE);
			
			if (n == JOptionPane.YES_OPTION){
				CertificateManager.forcePutServerCert(hostname, CertificateHelper.convert(cert));
				isVerifying = false;
				return true;
			} else {
				isVerifying = false;
				return false;
			}
		} else {
			return false;
		}
	}
	

	public static boolean overwriteCert(String hostname, X509Certificate cert){
		if (!isVerifying){
			JFrame frame = new JFrame();
			
			String title = Transl._("Warning, new remote host detected");
			String hostString = Transl._("The certificate of " + hostname + " has changed! <br>" +
					"Are you sure you like permanently trust the new certificate from the remote host?");
			
			String certName = "N/A";<commit_msg>Fixed issue with multiple 'Overwrite stored cert' popups.<commit_after>package net.i2p.itoopie.gui;

import java.awt.BorderLayout;

import javax.security.cert.X509Certificate;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

import net.i2p.itoopie.i18n.Transl;
import net.i2p.itoopie.security.CertificateHelper;
import net.i2p.itoopie.security.CertificateManager;

public class CertificateGUI {
	private static boolean isVerifying = false;
	
	public static void main(String[] args){
		System.out.println("Save new cert: " + saveNewCert(null,null));	
		System.out.println("Overwrite cert: " + overwriteCert(null,null));		
	}

	public static boolean saveNewCert(String hostname, X509Certificate cert){
		if (!isVerifying){
			isVerifying = true;
			JFrame frame = new JFrame();
			frame.setLayout(new BorderLayout());
			JButton bt = new JButton();
			bt.setText("text");
			frame.add(bt, BorderLayout.NORTH);
			
			String title = Transl._("New remote host detected");
			String hostString = Transl._("Would you like permanently trust the certificate from the remote host " + hostname + "?");
			
			String certName = "N/A";
			String certAlgo = "N/A";
			String certSerial = "N/A";
			String certThumb = "N/A";
			if (cert != null){
				certName = cert.getSubjectDN().getName();
				String certString = cert.getPublicKey().toString();
				certAlgo = certString.substring(0,certString.indexOf("\n"));
				certSerial = String.valueOf(cert.getPublicKey().serialVersionUID);
				certThumb = CertificateHelper.getThumbPrint(cert);
			}
			String certInfo = "<html>"+Transl._("Certificate info") + "<br><br>" +
					Transl._("Name: ") + certName + "<br>" +
					Transl._("Algorithm: ") + certAlgo + "<br>" +
					Transl._("Serial: ") + certSerial + "<br>" +
					Transl._("SHA-1 ID-hash: ") + certThumb;
			
			String textContent = certInfo + "<br><br>" + hostString;
			 
			int n = JOptionPane.showConfirmDialog(
			    frame,
			    textContent,
			    title,
			    JOptionPane.YES_NO_OPTION,
			    JOptionPane.INFORMATION_MESSAGE);
			
			if (n == JOptionPane.YES_OPTION){
				CertificateManager.forcePutServerCert(hostname, CertificateHelper.convert(cert));
				isVerifying = false;
				return true;
			} else {
				isVerifying = false;
				return false;
			}
		} else {
			return false;
		}
	}
	

	public static boolean overwriteCert(String hostname, X509Certificate cert){
		if (!isVerifying){
			isVerifying = true;
			JFrame frame = new JFrame();
			
			String title = Transl._("Warning, new remote host detected");
			String hostString = Transl._("The certificate of " + hostname + " has changed! <br>" +
					"Are you sure you like permanently trust the new certificate from the remote host?");
			
			String certName = "N/A";
{'fraction_non_alphanumeric': 0.12985371139606286, 'fraction_numerical': 0.006140509301065559, 'mean_word_length': 3.7700258397932815, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5314983', 'n_tokens_mistral': 2038, 'n_tokens_neox': 1701, 'n_words': 414}