<commit_before>from sympy import integer_nthroot
from fractions import gcd

# Computes an RSA private key given a public exponent
# e and a list of the primes factors of the modulus
def compute_priv_rsa_exp(e, prime_factors_list):
	totient = totient_from_primes(prime_factors_list)
	d = long(mod_inv(e, totient))
	return d

# Computes the euler's phi function of the number that is the
# product of the provided list of primes
def totient_from_primes(prime_factors_list):
	totient = 1
	for factor in prime_factors_list:
		totient = totient * (factor - 1)
	return totient

# Returns True if n1 and n2 are coprime and False otherwise
def coprime(n1, n2):
	return gcd(n1, n2) == 1

# Returns True if the provided value s can be decoded using
# the specified codec
def is_printable(s, codec='utf8'):
	try: s.decode(codec)
	except UnicodeDecodeError: 
		return False
	else: 
		return True

def int_nthroot(n, r): # returns (rounded root, whether root is int) 
	(root, exact) = integer_nthroot(n,r)
	if exact:
		return root
	else:
		return None

def crt(ds, rs):
	'''
	Chinese Remainder Theorem
	ds: array of dividers
	rs: array of remainders
	Return the number s such that s mod ds[i] = rs[i]
	'''
	length = len(ds)
	if not length == len(rs):
		print "The lengths of the two must be the same"
		return None

	p = i = prod = 1 
	s = 0
	for i in range(length): 
		prod *= ds[i]
	for i in range(length):
		p = prod / ds[i]
		s += rs[i] * mod_inv(p, ds[i]) * p
	return s % prod


def mod_inv(a, m):
	'''
	Return r such that a*r mod m = 1
	'''
	g, x, y = egcd(a, m)
	if g != 1:
		print("no inverse")
		return None
	else:
		return x % m


def egcd(a, b):
	'''
	Extended Euclidean gcd. Return g,x,y such that ax+by=g=gcd(a,b)
	'''
	if a == 0: 
		return (b, 0, 1)
	else:
		g, y, x = egcd(b%a, a)
		return (g, x-(b//a)*y, y)<commit_msg>pem extraction fn added in util<commit_after>from sympy import integer_nthroot
from fractions import gcd
from base64 import b64decode
from Crypto.Util.asn1 import DerSequence
from Crypto.PublicKey import RSA
from binascii import a2b_base64


# Computes an RSA private key given a public exponent
# e and a list of the primes factors of the modulus
def compute_priv_rsa_exp(e, prime_factors_list):
	totient = totient_from_primes(prime_factors_list)
	d = long(mod_inv(e, totient))
	return d

# Computes the euler's phi function of the number that is the
# product of the provided list of primes
def totient_from_primes(prime_factors_list):
	totient = 1
	for factor in prime_factors_list:
		totient = totient * (factor - 1)
	return totient

# Returns True if n1 and n2 are coprime and False otherwise
def coprime(n1, n2):
	return gcd(n1, n2) == 1

# Returns True if the provided value s can be decoded using
# the specified codec
def is_printable(s, codec='utf8'):
	try: s.decode(codec)
	except UnicodeDecodeError: 
		return False
	else: 
		return True

def int_nthroot(n, r): # returns (rounded root, whether root is int) 
	(root, exact) = integer_nthroot(n,r)
	if exact:
		return root
	else:
		return None

def crt(ds, rs):
	'''
	Chinese Remainder Theorem
	ds: array of dividers
	rs: array of remainders
	Return the number s such that s mod ds[i] = rs[i]
	'''
	length = len(ds)
	if not length == len(rs):
		print "The lengths of the two must be the same"
		return None

	p = i = prod = 1 
	s = 0
	for i in range(length): 
		prod *= ds[i]
	for i in range(length):
		p = prod / ds[i]
		s += rs[i] * mod_inv(p, ds[i]) * p
	return s % prod


def mod_inv(a, m):
	'''
	Return r such that a*r mod m = 1
	'''
	g, x, y = egcd(a, m)
	if g != 1:
		print("no inverse")
		return None
	else:
		return x % m


def egcd(a, b):
	'''
	Extended Euclidean gcd. Return g,x,y such that ax+by=g=gcd(a,b)
	'''
	if a == 0: 
		return (b, 0, 1)
	else:
		g, y, x = egcd(b%a, a)
		return (g, x-(b//a)*y, y)
		
		
def extractKeyFromPEM(pemfile):
	try:
		f = open(pemfile,'r')
	except IOError:
		print "Error: PEM file not found"
	else:
		pem = f.read()
		
		lines = pem.replace(" ",'').split()
		der = a2b_base64(''.join(lines[1:-1]))

		# Extract subjectPublicKeyInfo field from X.509 certificate (see RFC3280)
		cert = DerSequence()
		cert.decode(der)
		tbsCertificate = DerSequence()	
		tbsCertificate.decode(cert[0])
		subjectPublicKeyInfo = tbsCertificate[6]

		# Initialize RSA key
		rsa_key = RSA.importKey(subjectPublicKeyInfo)
		
		f.close()
		return rsa_key 		
{'fraction_non_alphanumeric': 0.10937858289383169, 'fraction_numerical': 0.012841091492776886, 'mean_word_length': 3.710583153347732, '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': '2313735', 'n_tokens_mistral': 1787, 'n_tokens_neox': 1631, 'n_words': 581}