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.

251 lines
9.1KB

  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,
  27. getUIColourIfAvailable (LookAndFeel_V4::ColourScheme::UIColour::outline,
  28. Colours::grey));
  29. rsaGroup.setColour (GroupComponent::textColourId,
  30. getUIColourIfAvailable (LookAndFeel_V4::ColourScheme::UIColour::defaultText,
  31. Colours::white));
  32. rsaResultBox.setColour (TextEditor::backgroundColourId,
  33. getUIColourIfAvailable (LookAndFeel_V4::ColourScheme::UIColour::widgetBackground,
  34. Colours::white.withAlpha (0.5f)));
  35. bitSizeLabel.setText ("Num Bits to Use:", dontSendNotification);
  36. bitSizeLabel.attachToComponent (&bitSize, true);
  37. addAndMakeVisible (bitSize);
  38. bitSize.setText (String (256));
  39. addAndMakeVisible (generateRSAButton);
  40. generateRSAButton.setButtonText ("Generate RSA");
  41. generateRSAButton.addListener (this);
  42. addAndMakeVisible (rsaResultBox);
  43. rsaResultBox.setReadOnly (true);
  44. rsaResultBox.setMultiLine (true);
  45. }
  46. void resized() override
  47. {
  48. Rectangle<int> area (getLocalBounds());
  49. rsaGroup.setBounds (area);
  50. area.removeFromTop (10);
  51. area.reduce (5, 5);
  52. Rectangle<int> topArea (area.removeFromTop (34));
  53. topArea.removeFromLeft (110);
  54. bitSize.setBounds (topArea.removeFromLeft (topArea.getWidth() / 2).reduced (5));
  55. generateRSAButton.setBounds (topArea.reduced (5));
  56. rsaResultBox.setBounds (area.reduced (5));
  57. }
  58. private:
  59. void createRSAKey()
  60. {
  61. int bits = jlimit (32, 1024, bitSize.getText().getIntValue());
  62. bitSize.setText (String (bits), dontSendNotification);
  63. // Create a key-pair...
  64. RSAKey publicKey, privateKey;
  65. RSAKey::createKeyPair (publicKey, privateKey, bits);
  66. // Test the new key on a piece of data...
  67. BigInteger testValue;
  68. testValue.parseString ("1234567890abcdef", 16);
  69. BigInteger encodedValue (testValue);
  70. publicKey.applyToValue (encodedValue);
  71. BigInteger decodedValue (encodedValue);
  72. privateKey.applyToValue (decodedValue);
  73. // ..and show the results..
  74. String message;
  75. message << "Number of bits: " << bits << newLine
  76. << "Public Key: " << publicKey.toString() << newLine
  77. << "Private Key: " << privateKey.toString() << newLine
  78. << newLine
  79. << "Test input: " << testValue.toString (16) << newLine
  80. << "Encoded: " << encodedValue.toString (16) << newLine
  81. << "Decoded: " << decodedValue.toString (16) << newLine;
  82. rsaResultBox.setText (message, false);
  83. }
  84. GroupComponent rsaGroup;
  85. TextButton generateRSAButton;
  86. Label bitSizeLabel;
  87. TextEditor bitSize, rsaResultBox;
  88. void buttonClicked (Button* buttonThatWasClicked) override
  89. {
  90. if (buttonThatWasClicked == &generateRSAButton)
  91. createRSAKey();
  92. }
  93. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (RSAComponent)
  94. };
  95. //==============================================================================
  96. class HashesComponent : public Component,
  97. private TextEditor::Listener
  98. {
  99. public:
  100. HashesComponent()
  101. {
  102. addAndMakeVisible (hashGroup);
  103. hashGroup.setText ("Hashes");
  104. hashGroup.setColour (GroupComponent::outlineColourId,
  105. getUIColourIfAvailable (LookAndFeel_V4::ColourScheme::UIColour::outline,
  106. Colours::grey));
  107. hashGroup.setColour (GroupComponent::textColourId,
  108. getUIColourIfAvailable (LookAndFeel_V4::ColourScheme::UIColour::defaultText,
  109. Colours::white));
  110. hashEntryBox.setColour (TextEditor::backgroundColourId,
  111. getUIColourIfAvailable (LookAndFeel_V4::ColourScheme::UIColour::widgetBackground,
  112. Colours::white.withAlpha (0.5f)));
  113. addAndMakeVisible (hashEntryBox);
  114. hashEntryBox.setMultiLine (true);
  115. hashEntryBox.setReturnKeyStartsNewLine (true);
  116. hashEntryBox.setText ("Type some text in this box and the resulting MD5, SHA and Whirlpool hashes will update below");
  117. hashEntryBox.addListener (this);
  118. hashLabel1.setText ("Text to Hash:", dontSendNotification);
  119. hashLabel2.setText ("MD5 Result:", dontSendNotification);
  120. hashLabel3.setText ("SHA Result:", dontSendNotification);
  121. hashLabel4.setText ("Whirlpool Result:", dontSendNotification);
  122. hashLabel1.attachToComponent (&hashEntryBox, true);
  123. hashLabel2.attachToComponent (&md5Result, true);
  124. hashLabel3.attachToComponent (&shaResult, true);
  125. hashLabel4.attachToComponent (&whirlpoolResult, true);
  126. addAndMakeVisible (md5Result);
  127. addAndMakeVisible (shaResult);
  128. addAndMakeVisible (whirlpoolResult);
  129. updateHashes();
  130. }
  131. void updateHashes()
  132. {
  133. String text = hashEntryBox.getText();
  134. updateMD5Result (text.toUTF8());
  135. updateSHA256Result (text.toUTF8());
  136. updateWhirlpoolResult (text.toUTF8());
  137. }
  138. void updateMD5Result (CharPointer_UTF8 text)
  139. {
  140. md5Result.setText (MD5 (text).toHexString(), dontSendNotification);
  141. }
  142. void updateSHA256Result (CharPointer_UTF8 text)
  143. {
  144. shaResult.setText (SHA256 (text).toHexString(), dontSendNotification);
  145. }
  146. void updateWhirlpoolResult (CharPointer_UTF8 text)
  147. {
  148. whirlpoolResult.setText (Whirlpool (text).toHexString(), dontSendNotification);
  149. }
  150. void resized() override
  151. {
  152. Rectangle<int> area (getLocalBounds());
  153. hashGroup.setBounds (area);
  154. area.removeFromLeft (120);
  155. area.removeFromTop (10);
  156. area.reduce (5, 5);
  157. whirlpoolResult.setBounds (area.removeFromBottom (34));
  158. shaResult.setBounds (area.removeFromBottom (34));
  159. md5Result.setBounds (area.removeFromBottom (34));
  160. hashEntryBox.setBounds (area.reduced (5));
  161. }
  162. private:
  163. GroupComponent hashGroup;
  164. TextEditor hashEntryBox;
  165. Label md5Result, shaResult, whirlpoolResult;
  166. Label hashLabel1, hashLabel2, hashLabel3, hashLabel4;
  167. void textEditorTextChanged (TextEditor&) override { updateHashes(); }
  168. void textEditorReturnKeyPressed (TextEditor&) override { updateHashes(); }
  169. void textEditorEscapeKeyPressed (TextEditor&) override { updateHashes(); }
  170. void textEditorFocusLost (TextEditor&) override { updateHashes(); }
  171. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (HashesComponent)
  172. };
  173. //==============================================================================
  174. class CryptographyDemo : public Component
  175. {
  176. public:
  177. CryptographyDemo()
  178. {
  179. addAndMakeVisible (rsaDemo);
  180. addAndMakeVisible (hashDemo);
  181. }
  182. void paint (Graphics& g) override
  183. {
  184. g.fillAll (getUIColourIfAvailable (LookAndFeel_V4::ColourScheme::UIColour::windowBackground,
  185. Colour::greyLevel (0.4f)));
  186. }
  187. void resized() override
  188. {
  189. Rectangle<int> area (getLocalBounds());
  190. rsaDemo.setBounds (area.removeFromTop (getHeight() / 2).reduced (5));
  191. hashDemo.setBounds (area.reduced (5));
  192. }
  193. private:
  194. RSAComponent rsaDemo;
  195. HashesComponent hashDemo;
  196. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (CryptographyDemo)
  197. };
  198. // This static object will register this demo type in a global list of demos..
  199. static JuceDemoType<CryptographyDemo> demo ("40 Cryptography");