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.

264 lines
9.4KB

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