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.

142 lines
3.7KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 6 End-User License
  8. Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
  9. End User License Agreement: www.juce.com/juce-6-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. namespace juce
  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. bool RSAKey::isValid() const noexcept
  48. {
  49. return operator!= (RSAKey());
  50. }
  51. String RSAKey::toString() const
  52. {
  53. return part1.toString (16) + "," + part2.toString (16);
  54. }
  55. bool RSAKey::applyToValue (BigInteger& value) const
  56. {
  57. if (part1.isZero() || part2.isZero() || value <= 0)
  58. {
  59. jassertfalse; // using an uninitialised key
  60. value.clear();
  61. return false;
  62. }
  63. BigInteger result;
  64. while (! value.isZero())
  65. {
  66. result *= part2;
  67. BigInteger remainder;
  68. value.divideBy (part2, remainder);
  69. remainder.exponentModulo (part1, part2);
  70. result += remainder;
  71. }
  72. value.swapWith (result);
  73. return true;
  74. }
  75. BigInteger RSAKey::findBestCommonDivisor (const BigInteger& p, const BigInteger& q)
  76. {
  77. // try 3, 5, 9, 17, etc first because these only contain 2 bits and so
  78. // are fast to divide + multiply
  79. for (int i = 2; i <= 65536; i *= 2)
  80. {
  81. const BigInteger e (1 + i);
  82. if (e.findGreatestCommonDivisor (p).isOne() && e.findGreatestCommonDivisor (q).isOne())
  83. return e;
  84. }
  85. BigInteger e (4);
  86. while (! (e.findGreatestCommonDivisor (p).isOne() && e.findGreatestCommonDivisor (q).isOne()))
  87. ++e;
  88. return e;
  89. }
  90. void RSAKey::createKeyPair (RSAKey& publicKey, RSAKey& privateKey,
  91. const int numBits, const int* randomSeeds, const int numRandomSeeds)
  92. {
  93. jassert (numBits > 16); // not much point using less than this..
  94. jassert (numRandomSeeds == 0 || numRandomSeeds >= 2); // you need to provide plenty of seeds here!
  95. BigInteger p (Primes::createProbablePrime (numBits / 2, 30, randomSeeds, numRandomSeeds / 2));
  96. BigInteger q (Primes::createProbablePrime (numBits - numBits / 2, 30, randomSeeds == nullptr ? nullptr : (randomSeeds + numRandomSeeds / 2), numRandomSeeds - numRandomSeeds / 2));
  97. const BigInteger n (p * q);
  98. const BigInteger m (--p * --q);
  99. const BigInteger e (findBestCommonDivisor (p, q));
  100. BigInteger d (e);
  101. d.inverseModulo (m);
  102. publicKey.part1 = e;
  103. publicKey.part2 = n;
  104. privateKey.part1 = d;
  105. privateKey.part2 = n;
  106. }
  107. } // namespace juce