<commit_before>#NewHope_X25519_XSalsa20_Poly1305 Implementation by e
from newhope import newhope
import libnacl.utils
import libnacl
import hashlib
import random
import string
import os
import sys


class NewHopeXSPolyBox():

	def __init__(self):
		self._state = 0

	def setState(self):
		self._state += state

	def clearState(self):
		self._state = 0

	def initCode(self):
		return b'\x0b'

	def genPrekeyChunk(self, size = 32):
		self.prekey_chunk_1 = os.urandom(size)

	def log(self, msg):
		print('[%s]: %s' % (self.name, msg))

	def createSealedBox(self):
		self.pk, self.sk = libnacl.crypto_box_keypair()

	def setSenderPubKey(self, pk):
		self.sender_nacl_pubkey = pk

	def encryptPrekeyChunk(self):
		self.enc_prekey_chunk_1 = libnacl.crypto_box_seal(self.prekey_chunk_1, self.sender_nacl_pubkey)
		assert self.enc_prekey_chunk_1 != self.prekey_chunk_1

	def openSealedBox(self):
		self.prekey_chunk_1 = libnacl.crypto_box_seal_open(self.enc_prekey_chunk_1, self.pk, self.sk)

	def initNewHope(self):
		self.message = newhope.keygen(True)

	def createNewHopeSharedKey(self):
		self.shared_newhope_key = newhope.b_key

	def combine_prekey_chunks(self):
		combined = []
		for x in self.shared_newhope_key:
			combined.append(x)
		for x in self.prekey_chunk_1:
			combined.append(x)
		self.prekey = combined

	def genSeed(self):
		"""Generates a cryptographically secure random string of numbers
		   which for our purposes acts as a private seed.
		   Then we use shake_128 to generate a secure hash of the seed
		   Both are returned
		"""
		seed_length = int(''.join(random.SystemRandom().choice(string.digits) for _ in range(0, 3)))
		seed = os.urandom(seed_length)
		hashing_algorithm = hashlib.shake_128()
		hashing_algorithm.update(seed)
		# 2200 bytes from SHAKE-128 function is enough data to get 1024 coefficients
		# smaller than 5q, from Alkim, Ducas, Pöppelmann, Schwabe section 7:
		seed_hash = hashing_algorithm.digest(100)
		self.seed = seed
		self.seed_hash = seed_hash

	def verifySeed(self):
		hashing_algorithm = hashlib.shake_128()
		hashing_algorithm.update(self.recv_seed)
		# 2200 bytes from SHAKE-128 function is enough data to get 1024 coefficients
		# smaller than 5q, from Alkim, Ducas, Pöppelmann, Schwabe section 7:
		seed_hash = hashing_algorithm.digest(100)	
		assert seed_hash == self.recv_hash
		return True<commit_msg>One liner to combine prekey chunks<commit_after>#NewHope_X25519_XSalsa20_Poly1305 Implementation by e
from newhope import newhope
import libnacl.utils
import libnacl
import hashlib
import random
import string
import os
import sys


class NewHopeXSPolyBox():

	def __init__(self):
		self._state = 0

	def setState(self):
		self._state += state

	def clearState(self):
		self._state = 0

	def initCode(self):
		return b'\x0b'

	def genPrekeyChunk(self, size = 32):
		self.prekey_chunk_1 = os.urandom(size)

	def log(self, msg):
		print('[%s]: %s' % (self.name, msg))

	def createSealedBox(self):
		self.pk, self.sk = libnacl.crypto_box_keypair()

	def setSenderPubKey(self, pk):
		self.sender_nacl_pubkey = pk

	def encryptPrekeyChunk(self):
		self.enc_prekey_chunk_1 = libnacl.crypto_box_seal(self.prekey_chunk_1, self.sender_nacl_pubkey)
		assert self.enc_prekey_chunk_1 != self.prekey_chunk_1

	def openSealedBox(self):
		self.prekey_chunk_1 = libnacl.crypto_box_seal_open(self.enc_prekey_chunk_1, self.pk, self.sk)

	def initNewHope(self):
		self.message = newhope.keygen(True)

	def createNewHopeSharedKey(self):
		self.shared_newhope_key = newhope.b_key

	def combine_prekey_chunks(self):
		self.prekey = self.shared_newhope_key + [x for x in self.prekey_chunk_1]

	def genSeed(self):
		"""Generates a cryptographically secure random string of numbers
		   which for our purposes acts as a private seed.
		   Then we use shake_128 to generate a secure hash of the seed
		   Both are returned
		"""
		seed_length = int(''.join(random.SystemRandom().choice(string.digits) for _ in range(0, 3)))
		self.seed = os.urandom(seed_length)
		hashing_algorithm = hashlib.shake_128()
		hashing_algorithm.update(self.seed)
		# 2200 bytes from SHAKE-128 function is enough data to get 1024 coefficients
		# smaller than 5q, from Alkim, Ducas, Pöppelmann, Schwabe section 7:
		self.seed_hash = hashing_algorithm.digest(100)

	def verifySeed(self):
		hashing_algorithm = hashlib.shake_128()
		hashing_algorithm.update(self.recv_seed)
		# 2200 bytes from SHAKE-128 function is enough data to get 1024 coefficients
		# smaller than 5q, from Alkim, Ducas, Pöppelmann, Schwabe section 7:
		seed_hash = hashing_algorithm.digest(100)	
		assert seed_hash == self.recv_hash
		return True
{'fraction_non_alphanumeric': 0.09100734976221357, 'fraction_numerical': 0.028966709900562042, 'mean_word_length': 4.798245614035087, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '1259216', 'n_tokens_mistral': 1853, 'n_tokens_neox': 1612, 'n_words': 408}