{"size": 820, "ext": "js", "max_stars_repo_path": "scripts/rsaEncryption.js", "max_stars_repo_name": "mateusmaaia/rsa-encryption", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "scripts/rsaEncryption.js", "max_issues_repo_name": "mateusmaaia/rsa-encryption", "max_issues_repo_issues_event_min_datetime": "2022-01-14T05:30:21.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-22T22:08:14.000Z", "max_forks_repo_path": "scripts/rsaEncryption.js", "max_forks_repo_name": "mateusmaaia/rsa-encryption", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": 2, "max_forks_count": null, "avg_line_length": 28.275862069, "max_line_length": 76, "alphanum_fraction": 0.6707317073}

const getPrimeValue = require('./primeValues')
const getTotientValue = require('./totienValue')
const getCoprimeNumbers = require('./coprimeNumbers')
const getEncryptedKey = require('./encryptedKey')
const getDecryptedKey = require('./decryptedKey')

const rsaEncryption = async (string, bits) => {
  console.log('A string informada foi: ' + string)

  let [p, q] = await Promise.all([getPrimeValue(bits), getPrimeValue(bits)])
  let n = p * q
  let totientN = getTotientValue(p, q)

  for (let i = 2; i < totientN; i++) {
    let comprimeTest = getCoprimeNumbers(totientN, i)
    if (comprimeTest === 1) {
      let e = i
      let d = Math.pow(e, -1) % totientN

      let encryptedKey = getEncryptedKey(e, n, string)
      getDecryptedKey(d, n, encryptedKey)
      return
    }
  }
}

module.exports = rsaEncryption
