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.

249 lines
8.0KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-12 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #include "../JuceDemoHeader.h"
  19. class RSAComponent : public Component,
  20. private Button::Listener
  21. {
  22. public:
  23. RSAComponent()
  24. {
  25. addAndMakeVisible (rsaGroup);
  26. rsaGroup.setText ("RSA Encryption");
  27. rsaGroup.setColour (GroupComponent::outlineColourId, Colours::darkgrey);
  28. rsaGroup.setColour (GroupComponent::textColourId, Colours::black);
  29. bitSizeLabel.setText ("Num Bits to Use:", dontSendNotification);
  30. bitSizeLabel.attachToComponent (&bitSize, true);
  31. addAndMakeVisible (bitSize);
  32. bitSize.setText (String (256));
  33. addAndMakeVisible (generateRSAButton);
  34. generateRSAButton.setButtonText ("Generate RSA");
  35. generateRSAButton.addListener (this);
  36. addAndMakeVisible (rsaResultBox);
  37. rsaResultBox.setColour (TextEditor::backgroundColourId, Colours::white.withAlpha (0.5f));
  38. rsaResultBox.setReadOnly (true);
  39. rsaResultBox.setMultiLine (true);
  40. }
  41. void resized() override
  42. {
  43. Rectangle<int> area (getLocalBounds());
  44. rsaGroup.setBounds (area);
  45. area.removeFromTop (10);
  46. area.reduce (5, 5);
  47. Rectangle<int> topArea (area.removeFromTop (34));
  48. topArea.removeFromLeft (110);
  49. bitSize.setBounds (topArea.removeFromLeft (topArea.getWidth() / 2).reduced (5));
  50. generateRSAButton.setBounds (topArea.reduced (5));
  51. rsaResultBox.setBounds (area.reduced (5));
  52. }
  53. private:
  54. void createRSAKey()
  55. {
  56. int bits = jlimit (32, 512, bitSize.getText().getIntValue());
  57. bitSize.setText (String (bits), dontSendNotification);
  58. // Create a key-pair...
  59. RSAKey publicKey, privateKey;
  60. RSAKey::createKeyPair (publicKey, privateKey, bits);
  61. // Test the new key on a piece of data...
  62. BigInteger testValue;
  63. testValue.parseString ("1234567890abcdef", 16);
  64. BigInteger encodedValue (testValue);
  65. publicKey.applyToValue (encodedValue);
  66. BigInteger decodedValue (encodedValue);
  67. privateKey.applyToValue (decodedValue);
  68. // ..and show the results..
  69. String message;
  70. message << "Number of bits: " << bits << newLine
  71. << "Public Key: " << publicKey.toString() << newLine
  72. << "Private Key: " << privateKey.toString() << newLine
  73. << newLine
  74. << "Test input: " << testValue.toString (16) << newLine
  75. << "Encoded: " << encodedValue.toString (16) << newLine
  76. << "Decoded: " << decodedValue.toString (16) << newLine;
  77. rsaResultBox.setText (message, false);
  78. }
  79. GroupComponent rsaGroup;
  80. TextButton generateRSAButton;
  81. Label bitSizeLabel;
  82. TextEditor bitSize, rsaResultBox;
  83. void buttonClicked (Button* buttonThatWasClicked) override
  84. {
  85. if (buttonThatWasClicked == &generateRSAButton)
  86. createRSAKey();
  87. }
  88. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (RSAComponent)
  89. };
  90. //==============================================================================
  91. class HashesComponent : public Component,
  92. private TextEditor::Listener
  93. {
  94. public:
  95. HashesComponent()
  96. {
  97. addAndMakeVisible (hashGroup);
  98. hashGroup.setText ("Hashes");
  99. hashGroup.setColour (GroupComponent::outlineColourId, Colours::darkgrey);
  100. hashGroup.setColour (GroupComponent::textColourId, Colours::black);
  101. addAndMakeVisible (hashEntryBox);
  102. hashEntryBox.setMultiLine (true);
  103. hashEntryBox.setColour (TextEditor::backgroundColourId, Colours::white.withAlpha (0.5f));
  104. hashEntryBox.setReturnKeyStartsNewLine (true);
  105. hashEntryBox.setText ("Type some text in this box and the resulting MD5 and SHA hashes will update below");
  106. hashEntryBox.addListener (this);
  107. hashLabel1.setText ("Text to Hash:", dontSendNotification);
  108. hashLabel2.setText ("MD5 Result:", dontSendNotification);
  109. hashLabel3.setText ("SHA Result:", dontSendNotification);
  110. hashLabel1.attachToComponent (&hashEntryBox, true);
  111. hashLabel2.attachToComponent (&md5Result, true);
  112. hashLabel3.attachToComponent (&shaResult, true);
  113. addAndMakeVisible (md5Result);
  114. addAndMakeVisible (shaResult);
  115. updateHashes();
  116. }
  117. void updateHashes()
  118. {
  119. updateMD5Result();
  120. updateSHA256Result();
  121. }
  122. void updateMD5Result()
  123. {
  124. const MD5 md5 (hashEntryBox.getText().toUTF8());
  125. md5Result.setText (md5.toHexString(), dontSendNotification);
  126. }
  127. void updateSHA256Result()
  128. {
  129. const SHA256 sha (hashEntryBox.getText().toUTF8());
  130. shaResult.setText (sha.toHexString(), dontSendNotification);
  131. }
  132. void resized() override
  133. {
  134. Rectangle<int> area (getLocalBounds());
  135. hashGroup.setBounds (area);
  136. area.removeFromLeft (80);
  137. area.removeFromTop (10);
  138. area.reduce (5, 5);
  139. shaResult.setBounds (area.removeFromBottom (34).reduced (5));
  140. md5Result.setBounds (area.removeFromBottom (34).reduced (5));
  141. hashEntryBox.setBounds (area.reduced (5));
  142. }
  143. private:
  144. GroupComponent hashGroup;
  145. TextEditor hashEntryBox;
  146. Label md5Result, shaResult;
  147. Label hashLabel1, hashLabel2, hashLabel3;
  148. void textEditorTextChanged (TextEditor& editor) override
  149. {
  150. if (&editor == &hashEntryBox)
  151. updateHashes();
  152. }
  153. void textEditorReturnKeyPressed (TextEditor& editor) override
  154. {
  155. if (&editor == &hashEntryBox)
  156. updateHashes();
  157. }
  158. void textEditorEscapeKeyPressed (TextEditor& editor) override
  159. {
  160. if (&editor == &hashEntryBox)
  161. updateHashes();
  162. }
  163. void textEditorFocusLost (TextEditor& editor) override
  164. {
  165. if (&editor == &hashEntryBox)
  166. updateHashes();
  167. }
  168. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (HashesComponent)
  169. };
  170. //==============================================================================
  171. class CryptographyDemo : public Component
  172. {
  173. public:
  174. CryptographyDemo()
  175. {
  176. addAndMakeVisible (rsaDemo);
  177. addAndMakeVisible (hashDemo);
  178. }
  179. void paint (Graphics& g) override
  180. {
  181. fillBrushedAluminiumBackground (g);
  182. }
  183. void resized() override
  184. {
  185. Rectangle<int> area (getLocalBounds());
  186. rsaDemo.setBounds (area.removeFromTop (getHeight() / 2).reduced (5));
  187. hashDemo.setBounds (area.reduced (5));
  188. }
  189. private:
  190. RSAComponent rsaDemo;
  191. HashesComponent hashDemo;
  192. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (CryptographyDemo)
  193. };
  194. // This static object will register this demo type in a global list of demos..
  195. static JuceDemoType<CryptographyDemo> demo ("40 Cryptography");