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.

138 lines
4.0KB

  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. BEGIN_JUCE_NAMESPACE
  19. //==============================================================================
  20. RSAKey::RSAKey()
  21. {
  22. }
  23. RSAKey::RSAKey (const String& s)
  24. {
  25. if (s.containsChar (','))
  26. {
  27. part1.parseString (s.upToFirstOccurrenceOf (",", false, false), 16);
  28. part2.parseString (s.fromFirstOccurrenceOf (",", false, false), 16);
  29. }
  30. else
  31. {
  32. // the string needs to be two hex numbers, comma-separated..
  33. jassertfalse;
  34. }
  35. }
  36. RSAKey::~RSAKey()
  37. {
  38. }
  39. bool RSAKey::operator== (const RSAKey& other) const noexcept
  40. {
  41. return part1 == other.part1 && part2 == other.part2;
  42. }
  43. bool RSAKey::operator!= (const RSAKey& other) const noexcept
  44. {
  45. return ! operator== (other);
  46. }
  47. String RSAKey::toString() const
  48. {
  49. return part1.toString (16) + "," + part2.toString (16);
  50. }
  51. bool RSAKey::applyToValue (BigInteger& value) const
  52. {
  53. if (part1.isZero() || part2.isZero() || value <= 0)
  54. {
  55. jassertfalse; // using an uninitialised key
  56. value.clear();
  57. return false;
  58. }
  59. BigInteger result;
  60. while (! value.isZero())
  61. {
  62. result *= part2;
  63. BigInteger remainder;
  64. value.divideBy (part2, remainder);
  65. remainder.exponentModulo (part1, part2);
  66. result += remainder;
  67. }
  68. value.swapWith (result);
  69. return true;
  70. }
  71. BigInteger RSAKey::findBestCommonDivisor (const BigInteger& p, const BigInteger& q)
  72. {
  73. // try 3, 5, 9, 17, etc first because these only contain 2 bits and so
  74. // are fast to divide + multiply
  75. for (int i = 2; i <= 65536; i *= 2)
  76. {
  77. const BigInteger e (1 + i);
  78. if (e.findGreatestCommonDivisor (p).isOne() && e.findGreatestCommonDivisor (q).isOne())
  79. return e;
  80. }
  81. BigInteger e (4);
  82. while (! (e.findGreatestCommonDivisor (p).isOne() && e.findGreatestCommonDivisor (q).isOne()))
  83. ++e;
  84. return e;
  85. }
  86. void RSAKey::createKeyPair (RSAKey& publicKey, RSAKey& privateKey,
  87. const int numBits, const int* randomSeeds, const int numRandomSeeds)
  88. {
  89. jassert (numBits > 16); // not much point using less than this..
  90. jassert (numRandomSeeds == 0 || numRandomSeeds >= 2); // you need to provide plenty of seeds here!
  91. BigInteger p (Primes::createProbablePrime (numBits / 2, 30, randomSeeds, numRandomSeeds / 2));
  92. BigInteger q (Primes::createProbablePrime (numBits - numBits / 2, 30, randomSeeds == nullptr ? 0 : (randomSeeds + numRandomSeeds / 2), numRandomSeeds - numRandomSeeds / 2));
  93. const BigInteger n (p * q);
  94. const BigInteger m (--p * --q);
  95. const BigInteger e (findBestCommonDivisor (p, q));
  96. BigInteger d (e);
  97. d.inverseModulo (m);
  98. publicKey.part1 = e;
  99. publicKey.part2 = n;
  100. privateKey.part1 = d;
  101. privateKey.part2 = n;
  102. }
  103. END_JUCE_NAMESPACE