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.

135 lines
3.5KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 6 technical preview.
  4. Copyright (c) 2020 - Raw Material Software Limited
  5. You may use this code under the terms of the GPL v3
  6. (see www.gnu.org/licenses).
  7. For this technical preview, this file is not subject to commercial licensing.
  8. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  9. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  10. DISCLAIMED.
  11. ==============================================================================
  12. */
  13. namespace juce
  14. {
  15. RSAKey::RSAKey()
  16. {
  17. }
  18. RSAKey::RSAKey (const String& s)
  19. {
  20. if (s.containsChar (','))
  21. {
  22. part1.parseString (s.upToFirstOccurrenceOf (",", false, false), 16);
  23. part2.parseString (s.fromFirstOccurrenceOf (",", false, false), 16);
  24. }
  25. else
  26. {
  27. // the string needs to be two hex numbers, comma-separated..
  28. jassertfalse;
  29. }
  30. }
  31. RSAKey::~RSAKey()
  32. {
  33. }
  34. bool RSAKey::operator== (const RSAKey& other) const noexcept
  35. {
  36. return part1 == other.part1 && part2 == other.part2;
  37. }
  38. bool RSAKey::operator!= (const RSAKey& other) const noexcept
  39. {
  40. return ! operator== (other);
  41. }
  42. bool RSAKey::isValid() const noexcept
  43. {
  44. return operator!= (RSAKey());
  45. }
  46. String RSAKey::toString() const
  47. {
  48. return part1.toString (16) + "," + part2.toString (16);
  49. }
  50. bool RSAKey::applyToValue (BigInteger& value) const
  51. {
  52. if (part1.isZero() || part2.isZero() || value <= 0)
  53. {
  54. jassertfalse; // using an uninitialised key
  55. value.clear();
  56. return false;
  57. }
  58. BigInteger result;
  59. while (! value.isZero())
  60. {
  61. result *= part2;
  62. BigInteger remainder;
  63. value.divideBy (part2, remainder);
  64. remainder.exponentModulo (part1, part2);
  65. result += remainder;
  66. }
  67. value.swapWith (result);
  68. return true;
  69. }
  70. BigInteger RSAKey::findBestCommonDivisor (const BigInteger& p, const BigInteger& q)
  71. {
  72. // try 3, 5, 9, 17, etc first because these only contain 2 bits and so
  73. // are fast to divide + multiply
  74. for (int i = 2; i <= 65536; i *= 2)
  75. {
  76. const BigInteger e (1 + i);
  77. if (e.findGreatestCommonDivisor (p).isOne() && e.findGreatestCommonDivisor (q).isOne())
  78. return e;
  79. }
  80. BigInteger e (4);
  81. while (! (e.findGreatestCommonDivisor (p).isOne() && e.findGreatestCommonDivisor (q).isOne()))
  82. ++e;
  83. return e;
  84. }
  85. void RSAKey::createKeyPair (RSAKey& publicKey, RSAKey& privateKey,
  86. const int numBits, const int* randomSeeds, const int numRandomSeeds)
  87. {
  88. jassert (numBits > 16); // not much point using less than this..
  89. jassert (numRandomSeeds == 0 || numRandomSeeds >= 2); // you need to provide plenty of seeds here!
  90. BigInteger p (Primes::createProbablePrime (numBits / 2, 30, randomSeeds, numRandomSeeds / 2));
  91. BigInteger q (Primes::createProbablePrime (numBits - numBits / 2, 30, randomSeeds == nullptr ? nullptr : (randomSeeds + numRandomSeeds / 2), numRandomSeeds - numRandomSeeds / 2));
  92. const BigInteger n (p * q);
  93. const BigInteger m (--p * --q);
  94. const BigInteger e (findBestCommonDivisor (p, q));
  95. BigInteger d (e);
  96. d.inverseModulo (m);
  97. publicKey.part1 = e;
  98. publicKey.part2 = n;
  99. privateKey.part1 = d;
  100. privateKey.part2 = n;
  101. }
  102. } // namespace juce