<commit_before>package selectvoting.tests;

import junit.framework.TestCase;

import org.junit.Test;

import selectvoting.lib.crypto.CryptoLib;
import selectvoting.utils.Utilities;
import static selectvoting.utils.MessageTools.equal;
import static selectvoting.utils.Utilities.hexStringToByteArray;


public class TestCryptoLib extends TestCase {

	private static byte[] key = hexStringToByteArray("<KEY>");
	private static byte[] encrypted_message = hexStringToByteArray("1693e24f26f49c096e7cf5e73800d5ded1aac5bbfc3ee9dc586a1244a2ce5a");
	private static byte[] encrypted_plaintext = hexStringToByteArray("3f2267");

	private static byte[] encryption_key =  hexStringToByteArray("30819F300D06092A864886F70D010101050003818D0030818902818100B21E1FA56085DFEF9DA015A731CA2243FFF2A6354CD6C3AC5210C9D047702908A876F4E822A35A097BF0D8E0397A1B9C3F7BB4A055239E3F67500A707A3B5659FBCA35A1CEFFC251D72BE04F313A4B11451845E01F3A30B18546A521B268772051BC2ADC22EBDA6B9ECE530460A6DFE8818B1F53363E5C91BB7BA450C21AFCE90203010001");
	private static byte[] decryption_key =  hexStringToByteArray("<KEY>");
	private static byte[] rsa_encrypted = hexStringToByteArray("<KEY>");
	private static byte[] pk_encrypted = hexStringToByteArray("000000806efc22a799ddfec610205139ffabbc5cfa9896fa689db251f5319dd376cbea4f4eded6013e2a10ae09dc2b879e8391cfeb6e59461344959cb3d0391ea2c7686a38532a05cec42304452f166993af4fe98ccf0c5129ded7bb0d672293f20cdffbd75d6b5a7f0004da53862c2ccf44e531dc64682ef7b0363d59e85a0d53161443347a2588b03e39b5c2206d04305b1e06fb1f835c68a971088a39e47344d7");

	
	private void log(byte[] b) {
		System.out.println(Utilities.byteArrayToHexString(b));
	}

	@Test
	public void testSymEnc() {
		byte[] plaintext = {0x3f,0x22};

		byte[] c = CryptoLib.symkey_encrypt(key, plaintext);	
		byte[] m = CryptoLib.symkey_decrypt(key, c);
		// log(m);
		assertTrue( equal(plaintext,m) );

		byte[] m1 = CryptoLib.symkey_decrypt(key, encrypted_message);
		assertTrue( equal(m1, encrypted_plaintext));
		// log(m1);
	}

	@Test
	public void testRawPKE() {
		byte[] plaintext = {0x3f,0x33};

		byte[] c = CryptoLib.just_pke_encrypt(plaintext, encryption_key);
		// log(c);
		byte[] m = CryptoLib.just_pke_decrypt(c, decryption_key);
		// log(m);
		assertTrue( equal(plaintext,m) );

		byte[] m1 = CryptoLib.just_pke_decrypt(rsa_encrypted, decryption_key);
		// log(m1);
		assertTrue( equal(plaintext,m1) );
	}

	@Test
	public void testPKE() {
		byte[] plaintext = {0x3f,0x37};

		byte[] c = CryptoLib.pke_encrypt(plaintext, encryption_key);
		log(c);
		byte[] m = CryptoLib.pke_decrypt(c, decryption_key);
		log(m);
		assertTrue( equal(plaintext,m) );
		
		byte[] d = CryptoLib.pke_decrypt(pk_encrypted, decryption_key);
		assertTrue( equal(plaintext,d) );
	}
}<commit_msg>[java] add simple tests for digital signatures<commit_after>package selectvoting.tests;

import junit.framework.TestCase;

import org.junit.Test;

import selectvoting.lib.crypto.CryptoLib;
import selectvoting.lib.crypto.KeyPair;
import selectvoting.utils.Utilities;
import static selectvoting.utils.MessageTools.equal;
import static selectvoting.utils.Utilities.hexStringToByteArray;


public class TestCryptoLib extends TestCase {

	private static byte[] key = hexStringToByteArray("<KEY>");
	private static byte[] encrypted_message = hexStringToByteArray("1693e24f26f49c096e7cf5e73800d5ded1aac5bbfc3ee9dc586a1244a2ce5a");
	private static byte[] encrypted_plaintext = hexStringToByteArray("3f2267");

	private static byte[] public_key =  hexStringToByteArray("30819F300D06092A864886F70D010101050003818D0030818902818100B21E1FA56085DFEF9DA015A731CA2243FFF2A6354CD6C3AC5210C9D047702908A876F4E822A35A097BF0D8E0397A1B9C3F7BB4A055239E3F67500A707A3B5659FBCA35A1CEFFC251D72BE04F313A4B11451845E01F3A30B18546A521B268772051BC2ADC22EBDA6B9ECE530460A6DFE8818B1F53363E5C91BB7BA450C21AFCE90203010001");
	private static byte[] private_key =  hexStringToByteArray("<KEY>");
	private static byte[] rsa_encrypted = hexStringToByteArray("<KEY>");
	private static byte[] pk_encrypted = hexStringToByteArray("000000806efc22a799ddfec610205139ffabbc5cfa9896fa689db251f5319dd376cbea4f4eded6013e2a10ae09dc2b879e8391cfeb6e59461344959cb3d0391ea2c7686a38532a05cec42304452f166993af4fe98ccf0c5129ded7bb0d672293f20cdffbd75d6b5a7f0004da53862c2ccf44e531dc64682ef7b0363d59e85a0d53161443347a2588b03e39b5c2206d04305b1e06fb1f835c68a971088a39e47344d7");

	
	private void log(byte[] b) {
		System.out.println(Utilities.byteArrayToHexString(b));
	}

	@Test
	public void testSymEnc() {
		byte[] plaintext = {0x3f,0x22};

		byte[] c = CryptoLib.symkey_encrypt(key, plaintext);	
		byte[] m = CryptoLib.symkey_decrypt(key, c);
		// log(m);
		assertTrue( equal(plaintext,m) );

		byte[] m1 = CryptoLib.symkey_decrypt(key, encrypted_message);
		assertTrue( equal(m1, encrypted_plaintext));
		// log(m1);
	}

	@Test
	public void testRawPKE() {
		byte[] plaintext = {0x3f,0x33};

		byte[] c = CryptoLib.just_pke_encrypt(plaintext, public_key);
		// log(c);
		byte[] m = CryptoLib.just_pke_decrypt(c, private_key);
		// log(m);
		assertTrue( equal(plaintext,m) );

		byte[] m1 = CryptoLib.just_pke_decrypt(rsa_encrypted, private_key);
		// log(m1);
		assertTrue( equal(plaintext,m1) );
	}

	@Test
	public void testPKE() {
		byte[] plaintext = {0x3f,0x37};

		byte[] c = CryptoLib.pke_encrypt(plaintext, public_key);
		// log(c);
		byte[] m = CryptoLib.pke_decrypt(c, private_key);
		// log(m);
		assertTrue( equal(plaintext,m) );
		
		byte[] d = CryptoLib.pke_decrypt(pk_encrypted, private_key);
		assertTrue( equal(plaintext,d) );
	}

	@Test
	public void testSignatures() {
		KeyPair k = CryptoLib.generateSignatureKeyPair();
		byte[] message = {0x3f,0x22, 0x1f};
		byte[] message1 = {0x3f,0x22, 0x1e};
		byte[] signature = CryptoLib.sign(message, k.privateKey);
		boolean verified = CryptoLib.verify(message, signature, k.publicKey);
		assertTrue( verified );
		verified = CryptoLib.verify(message1, signature, k.publicKey);
		assertFalse( verified );
		
		signature = CryptoLib.sign(message, private_key);
		log(signature);		
		verified = CryptoLib.verify(message, signature, public_key);
		assertTrue( verified );
		verified = CryptoLib.verify(message1, signature, public_key);
		assertFalse( verified );
		
	}
	
}
{'fraction_non_alphanumeric': 0.10541586073500966, 'fraction_numerical': 0.16038039974210186, 'mean_word_length': 7.28437917222964, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '7138282', 'n_tokens_mistral': 3084, 'n_tokens_neox': 2514, 'n_words': 332}