The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

132 lines
3.8KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. RSAKey::RSAKey()
  19. {
  20. }
  21. RSAKey::RSAKey (const String& s)
  22. {
  23. if (s.containsChar (','))
  24. {
  25. part1.parseString (s.upToFirstOccurrenceOf (",", false, false), 16);
  26. part2.parseString (s.fromFirstOccurrenceOf (",", false, false), 16);
  27. }
  28. else
  29. {
  30. // the string needs to be two hex numbers, comma-separated..
  31. jassertfalse;
  32. }
  33. }
  34. RSAKey::~RSAKey()
  35. {
  36. }
  37. bool RSAKey::operator== (const RSAKey& other) const noexcept
  38. {
  39. return part1 == other.part1 && part2 == other.part2;
  40. }
  41. bool RSAKey::operator!= (const RSAKey& other) const noexcept
  42. {
  43. return ! operator== (other);
  44. }
  45. String RSAKey::toString() const
  46. {
  47. return part1.toString (16) + "," + part2.toString (16);
  48. }
  49. bool RSAKey::applyToValue (BigInteger& value) const
  50. {
  51. if (part1.isZero() || part2.isZero() || value <= 0)
  52. {
  53. jassertfalse; // using an uninitialised key
  54. value.clear();
  55. return false;
  56. }
  57. BigInteger result;
  58. while (! value.isZero())
  59. {
  60. result *= part2;
  61. BigInteger remainder;
  62. value.divideBy (part2, remainder);
  63. remainder.exponentModulo (part1, part2);
  64. result += remainder;
  65. }
  66. value.swapWith (result);
  67. return true;
  68. }
  69. BigInteger RSAKey::findBestCommonDivisor (const BigInteger& p, const BigInteger& q)
  70. {
  71. // try 3, 5, 9, 17, etc first because these only contain 2 bits and so
  72. // are fast to divide + multiply
  73. for (int i = 2; i <= 65536; i *= 2)
  74. {
  75. const BigInteger e (1 + i);
  76. if (e.findGreatestCommonDivisor (p).isOne() && e.findGreatestCommonDivisor (q).isOne())
  77. return e;
  78. }
  79. BigInteger e (4);
  80. while (! (e.findGreatestCommonDivisor (p).isOne() && e.findGreatestCommonDivisor (q).isOne()))
  81. ++e;
  82. return e;
  83. }
  84. void RSAKey::createKeyPair (RSAKey& publicKey, RSAKey& privateKey,
  85. const int numBits, const int* randomSeeds, const int numRandomSeeds)
  86. {
  87. jassert (numBits > 16); // not much point using less than this..
  88. jassert (numRandomSeeds == 0 || numRandomSeeds >= 2); // you need to provide plenty of seeds here!
  89. BigInteger p (Primes::createProbablePrime (numBits / 2, 30, randomSeeds, numRandomSeeds / 2));
  90. BigInteger q (Primes::createProbablePrime (numBits - numBits / 2, 30, randomSeeds == nullptr ? 0 : (randomSeeds + numRandomSeeds / 2), numRandomSeeds - numRandomSeeds / 2));
  91. const BigInteger n (p * q);
  92. const BigInteger m (--p * --q);
  93. const BigInteger e (findBestCommonDivisor (p, q));
  94. BigInteger d (e);
  95. d.inverseModulo (m);
  96. publicKey.part1 = e;
  97. publicKey.part2 = n;
  98. privateKey.part1 = d;
  99. privateKey.part2 = n;
  100. }