<commit_before>package org.coriander.oauth.core.cryptography.keys

import java.io.{IOException, File}
import java.security.interfaces.RSAPrivateKey
import java.security.{KeyFactory, PrivateKey}
import java.security.spec.PKCS8EncodedKeySpec
import scala.io.Source._
import org.apache.commons.codec.binary.Base64._

object RsaPrivateKeyReader {
	def read(file : String): PrivateKey = {
		val allLines = fromFile(new File(file).getCanonicalPath).getLines.toList
		val START_TOKEN = "-----BEGIN PRIVATE KEY-----"
		val END_TOKEN 	= "-----END PRIVATE KEY-----"

		val isPKCS8Format = allLines.count((line : String) => line.trim.matches(START_TOKEN)) == 1

		if (false == isPKCS8Format)
			throw new IOException("The supplied file does not appear to be in PKCS8 format.")

		val justTheKey = linesBetween(file, START_TOKEN, END_TOKEN)

		toPrivateKey(justTheKey.mkString)
	}

	private def linesBetween(file : String, start : String, end : String) = {
		val lines = fromFile(new File(file).getCanonicalPath).getLines.toList

		var done = false
		var started = false

	 	lines.takeWhile((line : String) => {
			if (false == started) {
				started = line.trim.matches(start)
			}

			done = line.trim.matches(end)

			started && !done
		})

		lines.slice(1, lines.size)
	}

	private def toPrivateKey(encodedKey : String) = {
		val privSpec = new PKCS8EncodedKeySpec(decodeBase64(encodedKey))
		val keyFactory = KeyFactory.getInstance("RSA");
		keyFactory.generatePrivate(privSpec).asInstanceOf[RSAPrivateKey]
	}
}<commit_msg>Refactor: simplification<commit_after>package org.coriander.oauth.core.cryptography.keys

import java.io.{IOException, File}
import java.security.interfaces.RSAPrivateKey
import java.security.{KeyFactory, PrivateKey}
import java.security.spec.PKCS8EncodedKeySpec
import scala.io.Source._
import org.apache.commons.codec.binary.Base64._

object RsaPrivateKeyReader {
	def read(file : String): PrivateKey = {
		val allLines = fromFile(new File(file).getCanonicalPath).getLines.toList
		val START_TOKEN = "-----BEGIN PRIVATE KEY-----"
		val END_TOKEN 	= "-----END PRIVATE KEY-----"

		val isPKCS8Format = allLines.count((line : String) => line.trim.matches(START_TOKEN)) == 1

		if (false == isPKCS8Format)
			throw new IOException("The supplied file does not appear to be in PKCS8 format.")

		val justTheKey = linesBetween(file, START_TOKEN, END_TOKEN)

		toPrivateKey(justTheKey.mkString)
	}

	private def linesBetween(file : String, start : String, end : String) = {
		val lines = fromFile(new File(file).getCanonicalPath).getLines.toList

		var done = false
		var started = false

	 	lines.takeWhile((line : String) => {
			if (false == started) {
				started = line.trim.matches(start)
			}

			done = line.trim.matches(end)

			started && !done
		}).slice(1, lines.size)
	}

	private def toPrivateKey(encodedKey : String) = {
		val privSpec = new PKCS8EncodedKeySpec(decodeBase64(encodedKey))
		val keyFactory = KeyFactory.getInstance("RSA");
		keyFactory.generatePrivate(privSpec).asInstanceOf[RSAPrivateKey]
	}
}
{'fraction_non_alphanumeric': 0.11916932907348243, 'fraction_numerical': 0.007028753993610224, 'mean_word_length': 3.743939393939394, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '5219156', 'n_tokens_mistral': 1123, 'n_tokens_neox': 991, 'n_words': 215}