<commit_before>package org.csstudio.platform.security;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;

import org.csstudio.platform.securestore.SecureStore;
import org.eclipse.core.runtime.Platform;
import org.eclipse.equinox.security.storage.ISecurePreferences;
import org.eclipse.equinox.security.storage.SecurePreferencesFactory;

/**Secure Storage is used to save sensitive data such as user name and password in 
 * encrypted style. The secure storage file with a name of <code>secure_storage</code>
 * is under the directory where the program is installed.
 * <p> 
 * To encrypt the sensitive data, a master password provider must be provided which could be
 * configured under <code>General->Security->Secure Storage</code>. For more information, 
 * please see {@link ISecurePreferences}
 * </p>
 * <p>
 * Note: SecureStorage is different from {@link SecureStore} whose data is encrypted by 
 * the login user's password. SecureStorage is used to store sensitive data per installation
 * whereas {@link SecureStore} store sensitive data for per user.
 * </p>
 * This class includes the helpful functions for secure storage. 
 * 
 * @author <NAME>
 * @author <NAME>
 * @author <NAME>
 */
@SuppressWarnings("nls")
public class SecureStorage
{
    /** @return URL of the file that stores the secure settings */
	private static URL getStorageURL() throws MalformedURLException
    {
        return new URL(Platform.getInstallLocation().getURL()+"secure_storage");
    }

    /**Retrieve value of the key specified from the secure storage file  
	 * @param qualifier path to the root node
	 * @param key key with this the value is associated
	 * @return value associated the key. 
	 * If value was stored in an encrypted form, it will be decrypted. 
	 * null if the secure storage file or the key does not exist.
	 */
    public static String retrieveSecureStorage(final String qualifier, final String key)
	{
    	try
    	{
            final URL url = getStorageURL();
            // Check if the file exists.
            // This code went through several iterations because of issues
            // with URL, URI and File, see also
            // http://weblogs.java.net/blog/kohsuke/archive/2007/04/how_to_convert.html
            File file;
            try
            {
                // In principle, the API is clear: URL -> URI, then use the
                // matching File(URI) constructor.
                // The URL may contain spaces as '%20', and this converts
                // them to plain spaces.
                // But it fails with URLs like
                // "file:///c:/Some Path/Some File"
                file = new File(url.toURI());
            }<commit_msg>o.c.platform SecureStorage: ITER prefers config. over instance location<commit_after>package org.csstudio.platform.security;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;

import org.csstudio.platform.securestore.SecureStore;
import org.eclipse.core.runtime.Platform;
import org.eclipse.equinox.security.storage.ISecurePreferences;
import org.eclipse.equinox.security.storage.SecurePreferencesFactory;

/**Secure Storage is used to save sensitive data such as user name and password in 
 * encrypted style. The secure storage file with a name of <code>secure_storage</code>
 * is under the program's "configuration" area.
 * At runtime, this location can be checked via the "osgi.configuration.area" property,
 * which defaults to the "configuration" subdirectory of the "installation" area
 * (see Help/About ..., Installation Details, Configuration).
 * <p> 
 * To encrypt the sensitive data, a master password provider must be provided which could be
 * configured under <code>General->Security->Secure Storage</code>. For more information, 
 * please see {@link ISecurePreferences}
 * </p>
 * <p>
 * Note: SecureStorage is different from {@link SecureStore} whose data is encrypted by 
 * the login user's password. SecureStorage is used to store sensitive data per installation
 * whereas {@link SecureStore} store sensitive data for per user.
 * </p>
 * This class includes the helpful functions for secure storage. 
 * 
 * @author <NAME>
 * @author <NAME>
 * @author <NAME>
 * @author <NAME> (config. instead of install area)
 */
@SuppressWarnings("nls")
public class SecureStorage
{
    /** @return URL of the file that stores the secure settings */
	private static URL getStorageURL() throws MalformedURLException
    {
        return new URL(Platform.getConfigurationLocation().getURL()+"secure_storage");
    }

    /**Retrieve value of the key specified from the secure storage file  
	 * @param qualifier path to the root node
	 * @param key key with this the value is associated
	 * @return value associated the key. 
	 * If value was stored in an encrypted form, it will be decrypted. 
	 * null if the secure storage file or the key does not exist.
	 */
    public static String retrieveSecureStorage(final String qualifier, final String key)
	{
    	try
    	{
            final URL url = getStorageURL();
            // Check if the file exists.
            // This code went through several iterations because of issues
            // with URL, URI and File, see also
            // http://weblogs.java.net/blog/kohsuke/archive/2007/04/how_to_convert.html
            File file;
            try
            {
                // In principle, the API is clear: URL -> URI, then use the
                // matching File(URI) constructor.
                // The URL may contain spaces as '%20', and this converts
                // them to plain spaces.
                // But it fails with URLs like
                // "file:///c:/Some Path/Some File"
                file = new File(url.toURI());
            }
{'fraction_non_alphanumeric': 0.08667462890291759, 'fraction_numerical': 0.002729909571745436, 'mean_word_length': 2.8515111695137976, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 32, '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': '1120737', 'n_tokens_mistral': 1567, 'n_tokens_neox': 1481, 'n_words': 617}