zoloft/jsencrypt-core-libs — pull request #1 by phillipp (2014-10-23 15:44:02+00:00)
Title: Ensure that the generated RSA key has the desired length

Currently it is possible that RSAGenerate generates RSA keys with a public key of length `B-1` instead of `B` (for example 1023 bit instead of 1024 bit). For use cases that require specific bit lengths (for example Certificate Authorities), that will not work. The patch adds a check to the bit length of the public key (n). If the length is less than B (desired bit length), p and q are chosen again.

There was another option, where the two most significant bits of both p and q would be tested (independently) and re-generated when both were not significant.

I profiled both methods and found out that the method in the commit has less overhead. I profiled all three methods with 100 keys each.

```
Original 22s
Method from patch 30s (+36%)
Significant-bit-method 36s (+63%)
```

Still, this commit increases the time to generate keys in some cases significantly. In ~33% of the key generations the run-time nearly doubled.
**But** it is important to notice the original library didn't generate correct keys in that cases. So actually, this fixes a serious bug in the original library that led to the generation of faulty keys in 33% of the cases but was faster.