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.

236 lines
8.0KB

  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::grey);
  27. rsaGroup.setColour (GroupComponent::textColourId, Colours::white);
  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, 1024, 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::grey);
  99. hashGroup.setColour (GroupComponent::textColourId, Colours::white);
  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, SHA and Whirlpool 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. hashLabel4.setText ("Whirlpool Result:", dontSendNotification);
  110. hashLabel1.attachToComponent (&hashEntryBox, true);
  111. hashLabel2.attachToComponent (&md5Result, true);
  112. hashLabel3.attachToComponent (&shaResult, true);
  113. hashLabel4.attachToComponent (&whirlpoolResult, true);
  114. addAndMakeVisible (md5Result);
  115. addAndMakeVisible (shaResult);
  116. addAndMakeVisible (whirlpoolResult);
  117. updateHashes();
  118. }
  119. void updateHashes()
  120. {
  121. String text = hashEntryBox.getText();
  122. updateMD5Result (text.toUTF8());
  123. updateSHA256Result (text.toUTF8());
  124. updateWhirlpoolResult (text.toUTF8());
  125. }
  126. void updateMD5Result (CharPointer_UTF8 text)
  127. {
  128. md5Result.setText (MD5 (text).toHexString(), dontSendNotification);
  129. }
  130. void updateSHA256Result (CharPointer_UTF8 text)
  131. {
  132. shaResult.setText (SHA256 (text).toHexString(), dontSendNotification);
  133. }
  134. void updateWhirlpoolResult (CharPointer_UTF8 text)
  135. {
  136. whirlpoolResult.setText (Whirlpool (text).toHexString(), dontSendNotification);
  137. }
  138. void resized() override
  139. {
  140. Rectangle<int> area (getLocalBounds());
  141. hashGroup.setBounds (area);
  142. area.removeFromLeft (120);
  143. area.removeFromTop (10);
  144. area.reduce (5, 5);
  145. whirlpoolResult.setBounds (area.removeFromBottom (34));
  146. shaResult.setBounds (area.removeFromBottom (34));
  147. md5Result.setBounds (area.removeFromBottom (34));
  148. hashEntryBox.setBounds (area.reduced (5));
  149. }
  150. private:
  151. GroupComponent hashGroup;
  152. TextEditor hashEntryBox;
  153. Label md5Result, shaResult, whirlpoolResult;
  154. Label hashLabel1, hashLabel2, hashLabel3, hashLabel4;
  155. void textEditorTextChanged (TextEditor&) override { updateHashes(); }
  156. void textEditorReturnKeyPressed (TextEditor&) override { updateHashes(); }
  157. void textEditorEscapeKeyPressed (TextEditor&) override { updateHashes(); }
  158. void textEditorFocusLost (TextEditor&) override { updateHashes(); }
  159. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (HashesComponent)
  160. };
  161. //==============================================================================
  162. class CryptographyDemo : public Component
  163. {
  164. public:
  165. CryptographyDemo()
  166. {
  167. addAndMakeVisible (rsaDemo);
  168. addAndMakeVisible (hashDemo);
  169. }
  170. void paint (Graphics& g) override
  171. {
  172. g.fillAll (Colour::greyLevel (0.4f));
  173. }
  174. void resized() override
  175. {
  176. Rectangle<int> area (getLocalBounds());
  177. rsaDemo.setBounds (area.removeFromTop (getHeight() / 2).reduced (5));
  178. hashDemo.setBounds (area.reduced (5));
  179. }
  180. private:
  181. RSAComponent rsaDemo;
  182. HashesComponent hashDemo;
  183. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (CryptographyDemo)
  184. };
  185. // This static object will register this demo type in a global list of demos..
  186. static JuceDemoType<CryptographyDemo> demo ("40 Cryptography");