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.

253 lines
9.1KB

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