<commit_before>/*******************************************************************************
 * Copyright (C) 2009 The University of Manchester
 * 
 * Modifications to the initial code base are copyright of their respective
 * authors, or their employers as appropriate.
 * 
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307
 ******************************************************************************/
package net.sf.taverna.t2.ui.perspectives.myexperiment.model;

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.net.InetAddress;
import java.net.URL;
import java.net.UnknownHostException;
import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.TreeSet;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.PBEParameterSpec;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.SwingConstants;
import javax.imageio.ImageIO;

import net.sf.taverna.raven.appconfig.ApplicationRuntime;
import net.sf.taverna.t2.ui.perspectives.myexperiment.JClickableLabel;
import net.sf.taverna.t2.ui.perspectives.myexperiment.MyExperimentPerspective;
import net.sf.taverna.t2.ui.perspectives.myexperiment.model.SearchEngine.QuerySearchInstance;

import org.jdom.Document;
import org.jdom.Element;

/**
 * @author <NAME>
 */
public class Util {
  // ******** DATA ENCRYPTION ********

  private static final String PBE_PASSWORD = System.getProperty("user.home");
  private static final String PBE_SALT;

  static {
	String host_name = "";
	try {
	  host_name = InetAddress.getLocalHost().toString();
	} catch (UnknownHostException e) {
	  host_name = "unknown_localhost";
	}
	PBE_SALT = host_name;
  }

  /**
   * The following section (encrypt(), decrypt() and doEncryption() methods) is
   * used to store user passwords in an encrypted way within the settings file.
   */
  public static byte[] encrypt(String str) {
	return (doEncryption(str, Cipher.ENCRYPT_MODE));
  }

  public static byte[] decrypt(String str) {
	return (doEncryption(str, Cipher.DECRYPT_MODE));
  }

  private static byte[] doEncryption(String str, int mode) {
	// password-based encryption uses 2 parameters for processing:
	// a *password*, which is then hashed with a *salt* to generate
	// a strong key - these 2 are defined as class constants
	try {
	  SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
	  PBEKeySpec keySpec = new PBEKeySpec(Util.PBE_PASSWORD.toCharArray());
	  SecretKey key = keyFactory.generateSecret(keySpec);
	  MessageDigest md = MessageDigest.getInstance("MD5");
	  md.update(Util.PBE_SALT.getBytes());
	  byte[] digest = md.digest();
	  byte[] salt = new byte[8];
	  for (int i = 0; i < 8; ++i)
		salt[i] = digest[i];
	  PBEParameterSpec paramSpec = new PBEParameterSpec(salt, 20);

	  Cipher cipher = Cipher.getInstance("PBEWithMD5AndDES");
	  cipher.init(mode, key, paramSpec);

	  byte[] encrypted = cipher.doFinal(str.getBytes());
	  return (encrypted);
	} catch (Exception e) {
	  System.err.println(e);
	  e.printStackTrace();
	  return (new byte[1]);
	}

  }
<commit_msg>Changed printStackTrace to go to a logger as per T2-578<commit_after>/*******************************************************************************
 * Copyright (C) 2009 The University of Manchester
 * 
 * Modifications to the initial code base are copyright of their respective
 * authors, or their employers as appropriate.
 * 
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307
 ******************************************************************************/
package net.sf.taverna.t2.ui.perspectives.myexperiment.model;

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.net.InetAddress;
import java.net.URL;
import java.net.UnknownHostException;
import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.TreeSet;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.PBEParameterSpec;
import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.SwingConstants;

import net.sf.taverna.raven.appconfig.ApplicationRuntime;
import net.sf.taverna.t2.ui.perspectives.myexperiment.JClickableLabel;
import net.sf.taverna.t2.ui.perspectives.myexperiment.MyExperimentPerspective;
import net.sf.taverna.t2.ui.perspectives.myexperiment.model.SearchEngine.QuerySearchInstance;

import org.apache.log4j.Logger;
import org.jdom.Document;
import org.jdom.Element;

/**
 * @author <NAME>
 */
public class Util {
	private static Logger logger = Logger
	.getLogger(Util.class);

	// ******** DATA ENCRYPTION ********

  private static final String PBE_PASSWORD = System.getProperty("user.home");
  private static final String PBE_SALT;

  static {
	String host_name = "";
	try {
	  host_name = InetAddress.getLocalHost().toString();
	} catch (UnknownHostException e) {
	  host_name = "unknown_localhost";
	}
	PBE_SALT = host_name;
  }

  /**
   * The following section (encrypt(), decrypt() and doEncryption() methods) is
   * used to store user passwords in an encrypted way within the settings file.
   */
  public static byte[] encrypt(String str) {
	return (doEncryption(str, Cipher.ENCRYPT_MODE));
  }

  public static byte[] decrypt(String str) {
	return (doEncryption(str, Cipher.DECRYPT_MODE));
  }

  private static byte[] doEncryption(String str, int mode) {
	// password-based encryption uses 2 parameters for processing:
	// a *password*, which is then hashed with a *salt* to generate
	// a strong key - these 2 are defined as class constants
	try {
	  SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
	  PBEKeySpec keySpec = new PBEKeySpec(Util.PBE_PASSWORD.toCharArray());
	  SecretKey key = keyFactory.generateSecret(keySpec);
	  MessageDigest md = MessageDigest.getInstance("MD5");
	  md.update(Util.PBE_SALT.getBytes());
	  byte[] digest = md.digest();
	  byte[] salt = new byte[8];
	  for (int i = 0; i < 8; ++i)
		salt[i] = digest[i];
	  PBEParameterSpec paramSpec = new PBEParameterSpec(salt, 20);

	  Cipher cipher = Cipher.getInstance("PBEWithMD5AndDES");
	  cipher.init(mode, key, paramSpec);

	  byte[] encrypted = cipher.doFinal(str.getBytes());
	  return (encrypted);
	} catch (Exception e) {
		logger.error("Could not encrypt and store password");
	  return (new byte[1]);
	}

  }

{'fraction_non_alphanumeric': 0.13740009402914904, 'fraction_numerical': 0.009050305594734367, 'mean_word_length': 4.768813559322034, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, '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': '1799141', 'n_tokens_mistral': 2626, 'n_tokens_neox': 2466, 'n_words': 761}