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.

248 lines
7.8KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. #include "../JuceDemoHeader.h"
  18. class RSAComponent : public Component,
  19. private Button::Listener
  20. {
  21. public:
  22. RSAComponent()
  23. {
  24. addAndMakeVisible (rsaGroup);
  25. rsaGroup.setText ("RSA Encryption");
  26. rsaGroup.setColour (GroupComponent::outlineColourId, Colours::darkgrey);
  27. rsaGroup.setColour (GroupComponent::textColourId, Colours::black);
  28. bitSizeLabel.setText ("Num Bits to Use:", dontSendNotification);
  29. bitSizeLabel.attachToComponent (&bitSize, true);
  30. addAndMakeVisible (bitSize);
  31. bitSize.setText (String (256));
  32. addAndMakeVisible (generateRSAButton);
  33. generateRSAButton.setButtonText ("Generate RSA");
  34. generateRSAButton.addListener (this);
  35. addAndMakeVisible (rsaResultBox);
  36. rsaResultBox.setColour (TextEditor::backgroundColourId, Colours::white.withAlpha (0.5f));
  37. rsaResultBox.setReadOnly (true);
  38. rsaResultBox.setMultiLine (true);
  39. }
  40. void resized() override
  41. {
  42. Rectangle<int> area (getLocalBounds());
  43. rsaGroup.setBounds (area);
  44. area.removeFromTop (10);
  45. area.reduce (5, 5);
  46. Rectangle<int> topArea (area.removeFromTop (34));
  47. topArea.removeFromLeft (110);
  48. bitSize.setBounds (topArea.removeFromLeft (topArea.getWidth() / 2).reduced (5));
  49. generateRSAButton.setBounds (topArea.reduced (5));
  50. rsaResultBox.setBounds (area.reduced (5));
  51. }
  52. private:
  53. void createRSAKey()
  54. {
  55. int bits = jlimit (32, 512, bitSize.getText().getIntValue());
  56. bitSize.setText (String (bits), dontSendNotification);
  57. // Create a key-pair...
  58. RSAKey publicKey, privateKey;
  59. RSAKey::createKeyPair (publicKey, privateKey, bits);
  60. // Test the new key on a piece of data...
  61. BigInteger testValue;
  62. testValue.parseString ("1234567890abcdef", 16);
  63. BigInteger encodedValue (testValue);
  64. publicKey.applyToValue (encodedValue);
  65. BigInteger decodedValue (encodedValue);
  66. privateKey.applyToValue (decodedValue);
  67. // ..and show the results..
  68. String message;
  69. message << "Number of bits: " << bits << newLine
  70. << "Public Key: " << publicKey.toString() << newLine
  71. << "Private Key: " << privateKey.toString() << newLine
  72. << newLine
  73. << "Test input: " << testValue.toString (16) << newLine
  74. << "Encoded: " << encodedValue.toString (16) << newLine
  75. << "Decoded: " << decodedValue.toString (16) << newLine;
  76. rsaResultBox.setText (message, false);
  77. }
  78. GroupComponent rsaGroup;
  79. TextButton generateRSAButton;
  80. Label bitSizeLabel;
  81. TextEditor bitSize, rsaResultBox;
  82. void buttonClicked (Button* buttonThatWasClicked) override
  83. {
  84. if (buttonThatWasClicked == &generateRSAButton)
  85. createRSAKey();
  86. }
  87. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (RSAComponent)
  88. };
  89. //==============================================================================
  90. class HashesComponent : public Component,
  91. private TextEditor::Listener
  92. {
  93. public:
  94. HashesComponent()
  95. {
  96. addAndMakeVisible (hashGroup);
  97. hashGroup.setText ("Hashes");
  98. hashGroup.setColour (GroupComponent::outlineColourId, Colours::darkgrey);
  99. hashGroup.setColour (GroupComponent::textColourId, Colours::black);
  100. addAndMakeVisible (hashEntryBox);
  101. hashEntryBox.setMultiLine (true);
  102. hashEntryBox.setColour (TextEditor::backgroundColourId, Colours::white.withAlpha (0.5f));
  103. hashEntryBox.setReturnKeyStartsNewLine (true);
  104. hashEntryBox.setText ("Type some text in this box and the resulting MD5 and SHA hashes will update below");
  105. hashEntryBox.addListener (this);
  106. hashLabel1.setText ("Text to Hash:", dontSendNotification);
  107. hashLabel2.setText ("MD5 Result:", dontSendNotification);
  108. hashLabel3.setText ("SHA Result:", dontSendNotification);
  109. hashLabel1.attachToComponent (&hashEntryBox, true);
  110. hashLabel2.attachToComponent (&md5Result, true);
  111. hashLabel3.attachToComponent (&shaResult, true);
  112. addAndMakeVisible (md5Result);
  113. addAndMakeVisible (shaResult);
  114. updateHashes();
  115. }
  116. void updateHashes()
  117. {
  118. updateMD5Result();
  119. updateSHA256Result();
  120. }
  121. void updateMD5Result()
  122. {
  123. const MD5 md5 (hashEntryBox.getText().toUTF8());
  124. md5Result.setText (md5.toHexString(), dontSendNotification);
  125. }
  126. void updateSHA256Result()
  127. {
  128. const SHA256 sha (hashEntryBox.getText().toUTF8());
  129. shaResult.setText (sha.toHexString(), dontSendNotification);
  130. }
  131. void resized() override
  132. {
  133. Rectangle<int> area (getLocalBounds());
  134. hashGroup.setBounds (area);
  135. area.removeFromLeft (80);
  136. area.removeFromTop (10);
  137. area.reduce (5, 5);
  138. shaResult.setBounds (area.removeFromBottom (34).reduced (5));
  139. md5Result.setBounds (area.removeFromBottom (34).reduced (5));
  140. hashEntryBox.setBounds (area.reduced (5));
  141. }
  142. private:
  143. GroupComponent hashGroup;
  144. TextEditor hashEntryBox;
  145. Label md5Result, shaResult;
  146. Label hashLabel1, hashLabel2, hashLabel3;
  147. void textEditorTextChanged (TextEditor& editor) override
  148. {
  149. if (&editor == &hashEntryBox)
  150. updateHashes();
  151. }
  152. void textEditorReturnKeyPressed (TextEditor& editor) override
  153. {
  154. if (&editor == &hashEntryBox)
  155. updateHashes();
  156. }
  157. void textEditorEscapeKeyPressed (TextEditor& editor) override
  158. {
  159. if (&editor == &hashEntryBox)
  160. updateHashes();
  161. }
  162. void textEditorFocusLost (TextEditor& editor) override
  163. {
  164. if (&editor == &hashEntryBox)
  165. updateHashes();
  166. }
  167. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (HashesComponent)
  168. };
  169. //==============================================================================
  170. class CryptographyDemo : public Component
  171. {
  172. public:
  173. CryptographyDemo()
  174. {
  175. addAndMakeVisible (rsaDemo);
  176. addAndMakeVisible (hashDemo);
  177. }
  178. void paint (Graphics& g) override
  179. {
  180. g.fillAll (Colour::greyLevel (0.4f));
  181. }
  182. void resized() override
  183. {
  184. Rectangle<int> area (getLocalBounds());
  185. rsaDemo.setBounds (area.removeFromTop (getHeight() / 2).reduced (5));
  186. hashDemo.setBounds (area.reduced (5));
  187. }
  188. private:
  189. RSAComponent rsaDemo;
  190. HashesComponent hashDemo;
  191. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (CryptographyDemo)
  192. };
  193. // This static object will register this demo type in a global list of demos..
  194. static JuceDemoType<CryptographyDemo> demo ("40 Cryptography");