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.

116 lines
4.5KB

  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. #ifndef __JUCE_RSAKEY_JUCEHEADER__
  19. #define __JUCE_RSAKEY_JUCEHEADER__
  20. //==============================================================================
  21. /**
  22. RSA public/private key-pair encryption class.
  23. An object of this type makes up one half of a public/private RSA key pair. Use the
  24. createKeyPair() method to create a matching pair for encoding/decoding.
  25. */
  26. class JUCE_API RSAKey
  27. {
  28. public:
  29. //==============================================================================
  30. /** Creates a null key object.
  31. Initialise a pair of objects for use with the createKeyPair() method.
  32. */
  33. RSAKey();
  34. /** Loads a key from an encoded string representation.
  35. This reloads a key from a string created by the toString() method.
  36. */
  37. explicit RSAKey (const String& stringRepresentation);
  38. /** Destructor. */
  39. ~RSAKey();
  40. bool operator== (const RSAKey& other) const noexcept;
  41. bool operator!= (const RSAKey& other) const noexcept;
  42. //==============================================================================
  43. /** Turns the key into a string representation.
  44. This can be reloaded using the constructor that takes a string.
  45. */
  46. String toString() const;
  47. //==============================================================================
  48. /** Encodes or decodes a value.
  49. Call this on the public key object to encode some data, then use the matching
  50. private key object to decode it.
  51. Returns false if the operation couldn't be completed, e.g. if this key hasn't been
  52. initialised correctly.
  53. NOTE: This method dumbly applies this key to this data. If you encode some data
  54. and then try to decode it with a key that doesn't match, this method will still
  55. happily do its job and return true, but the result won't be what you were expecting.
  56. It's your responsibility to check that the result is what you wanted.
  57. */
  58. bool applyToValue (BigInteger& value) const;
  59. //==============================================================================
  60. /** Creates a public/private key-pair.
  61. Each key will perform one-way encryption that can only be reversed by
  62. using the other key.
  63. The numBits parameter specifies the size of key, e.g. 128, 256, 512 bit. Bigger
  64. sizes are more secure, but this method will take longer to execute.
  65. The randomSeeds parameter lets you optionally pass it a set of values with
  66. which to seed the random number generation, improving the security of the
  67. keys generated. If you supply these, make sure you provide more than 2 values,
  68. and the more your provide, the better the security.
  69. */
  70. static void createKeyPair (RSAKey& publicKey,
  71. RSAKey& privateKey,
  72. int numBits,
  73. const int* randomSeeds = nullptr,
  74. int numRandomSeeds = 0);
  75. protected:
  76. //==============================================================================
  77. BigInteger part1, part2;
  78. private:
  79. //==============================================================================
  80. static BigInteger findBestCommonDivisor (const BigInteger& p, const BigInteger& q);
  81. JUCE_LEAK_DETECTOR (RSAKey);
  82. };
  83. #endif // __JUCE_RSAKEY_JUCEHEADER__