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.

257 lines
9.2KB

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