<commit_before>﻿using UnityEngine;
using System.Collections;
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;

/**
 * Securely save player preferences in Unity3d projects.
 * This uses DES Encryption to encrypt your player prefs.
 *
 * @author <NAME> (<EMAIL>)
 * Copyright © 2014 <NAME>
 */
public class SecurePlayerPrefs {

	/*
   * The key is splited to three parts:
   *     - The prefix, first part.
   *     - Three randomly generated characters for each device.
   *     - The suffix, the end part of the key.
   * These constants are used in storing the random part for each device,
   * And for the prefs it uses the random number instead of PRIVATE_KEY_RAND.
   */
	private static readonly string PRIVATE_KEY_PREFIX = "abcd";
	private static readonly string PRIVATE_KEY_RAND = "efg";
	private static readonly string PRIVATE_KEY_SUFFIX = "h";

	// The private key of DES encryption.
	private static string privateKey;

	// Is the secure prefs initialized.
	private static bool isInit = false;

  // Your private key to store the randomly generated key for each device.
	private static readonly string RAND_KEY = "abcdefgh";

  // Should errors/exceptions be logged.
	private static bool logErrorsEnabled = false;


	/**
	 * Initializes the encryptor. If its the frist time, it will generate
	 * a random 3 digit number and puts it between private key and its appendix.
	 * If this was initialized on this device before, it will load it and create
	 * the private key.
	 *
	 * You can also copy players current preferences if your moving from Unity's
	 * PlayerPrefs to this secure one.
	 */
	public static void Init() {
		if(HasKey(RAND_KEY)) {
			privateKey = PRIVATE_KEY_PREFIX + PRIVATE_KEY_RAND + PRIVATE_KEY_SUFFIX;
			privateKey = GetString(RAND_KEY);
			privateKey = PRIVATE_KEY_PREFIX + privateKey + PRIVATE_KEY_SUFFIX;
		} else {
			int rand = UnityEngine.Random.Range(100, 1000);
			privateKey = PRIVATE_KEY_PREFIX + PRIVATE_KEY_RAND + PRIVATE_KEY_SUFFIX;
			SetInt(RAND_KEY, rand);
			privateKey = PRIVATE_KEY_PREFIX + rand + PRIVATE_KEY_SUFFIX;
			/*
			 * Add copying PlayerPrefs to SecurePlayerPrefs code here.
			 * See the example at GitHub. www.github.com/rawandnf/SecurePlayerPrefs
			 */
		}
		isInit = true;
	}


	/**
	 * Is the decryptor initialized.
	 */
	public static bool isInitialized() {
		return isInit;
	}


	/**
	 * Converts unsecure player prefs to secure player prefs.
	 *
	 * Basically translates the prefs to secure ones.
	 * @param keynames Is the list of keys with old and new name, [old name][new name][type].
	 */
	private static void securePlayerPrefs(string[,] keynames, bool deleteOldKeys) {
		for (int i = 0; i < keynames.GetLength(0); i++) {
			if (keynames[i, 2].Equals("int")) {
				// Getting the integer and saving it encrypted.
				int temp = PlayerPrefs.GetInt(keynames[i, 0]);
				if(deleteOldKeys) {
					PlayerPrefs.DeleteKey(keynames[i, 0]);
				}
				SetInt(keynames[i, 1], temp);
			} else if (keynames[i, 2].Equals("float")) {
				// Getting the float and saving it encrypted.
				float temp = PlayerPrefs.GetFloat(keynames[i, 0]);
				if(deleteOldKeys) {
					PlayerPrefs.DeleteKey(keynames[i, 0]);
				}
				SetFloat(keynames[i, 1], temp);
			} else {
				// If not a float and int, then we take it as a string and save it encrypted.
				string temp = PlayerPrefs.GetString(keynames[i, 0]);
				if(deleteOldKeys) {
					PlayerPrefs.DeleteKey(keynames[i, 0]);
				}
				SetString(keynames[i, 1], temp);<commit_msg>Update SecurePlayerPrefs.cs<commit_after>﻿using UnityEngine;
using System.Collections;
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;

/**
 * Securely save player preferences in Unity3d projects.
 * This uses DES Encryption to encrypt your player prefs.
 *
 * @author <NAME> (<EMAIL>)
 * Copyright © 2014 <NAME>
 */
public class SecurePlayerPrefs {

	/*
   * The key is splited to three parts:
   *     - The prefix, first part.
   *     - Three randomly generated characters for each device.
   *     - The suffix, the end part of the key.
   * These constants are used in storing the random part for each device,
   * And for the prefs it uses the random number instead of PRIVATE_KEY_RAND.
   */
	private static readonly string PRIVATE_KEY_PREFIX = "abcd";
	private static readonly string PRIVATE_KEY_RAND = "efg";
	private static readonly string PRIVATE_KEY_SUFFIX = "h";

	// The private key of DES encryption.
	private static string privateKey;

	// Is the secure prefs initialized.
	private static bool isInit = false;

  // Your private key to store the randomly generated key for each device.
	private static readonly string RAND_KEY = "abcdefgh";

  // Should errors/exceptions be logged.
	private static bool logErrorsEnabled = false;


	/**
	 * Initializes the encryptor. If its the frist time, it will generate
	 * a random 3 digit number and puts it between private key and its appendix.
	 * If this was initialized on this device before, it will load it and create
	 * the private key.
	 *
	 * You can also copy players current preferences if your moving from Unity's
	 * PlayerPrefs to this secure one.
	 */
	public static void Init() {
		if(HasKey(RAND_KEY)) {
			privateKey = PRIVATE_KEY_PREFIX + PRIVATE_KEY_RAND + PRIVATE_KEY_SUFFIX;
			privateKey = GetString(RAND_KEY);
			privateKey = PRIVATE_KEY_PREFIX + privateKey + PRIVATE_KEY_SUFFIX;
		} else {
			int rand = UnityEngine.Random.Range(100, 1000);
			privateKey = PRIVATE_KEY_PREFIX + PRIVATE_KEY_RAND + PRIVATE_KEY_SUFFIX;
			SetInt(RAND_KEY, rand);
			privateKey = PRIVATE_KEY_PREFIX + rand + PRIVATE_KEY_SUFFIX;
			/*
			 * Add copying PlayerPrefs to SecurePlayerPrefs code here.
			 * See the example at GitHub. www.github.com/rawandnf/SecurePlayerPrefs
			 */
		}
		isInit = true;
	}


	/**
	 * Is the decryptor initialized.
	 */
	public static bool isInitialized() {
		return isInit;
	}


	/**
	 * Converts unsecure player prefs to secure player prefs.
	 *
	 * Basically translates the prefs to secure ones.
	 * @param keynames Is the list of keys with old and new name, [old name][new name][type].
	 * @param deleteOldKeys Should it delete the old keys.
	 */
	private static void securePlayerPrefs(string[,] keynames, bool deleteOldKeys) {
		for (int i = 0; i < keynames.GetLength(0); i++) {
			if (keynames[i, 2].Equals("int")) {
				// Getting the integer and saving it encrypted.
				int temp = PlayerPrefs.GetInt(keynames[i, 0]);
				if(deleteOldKeys) {
					PlayerPrefs.DeleteKey(keynames[i, 0]);
				}
				SetInt(keynames[i, 1], temp);
			} else if (keynames[i, 2].Equals("float")) {
				// Getting the float and saving it encrypted.
				float temp = PlayerPrefs.GetFloat(keynames[i, 0]);
				if(deleteOldKeys) {
					PlayerPrefs.DeleteKey(keynames[i, 0]);
				}
				SetFloat(keynames[i, 1], temp);
			} else {
				// If not a float and int, then we take it as a string and save it encrypted.
				string temp = PlayerPrefs.GetString(keynames[i, 0]);
				if(deleteOldKeys) {
					PlayerPrefs.DeleteKey(keynames[i, 0]);
				}
				SetString(keynames[i, 1], temp);
{'fraction_non_alphanumeric': 0.09431306306306306, 'fraction_numerical': 0.00731981981981982, 'mean_word_length': 3.499683343888537, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '7618040', 'n_tokens_mistral': 2529, 'n_tokens_neox': 2180, 'n_words': 768}