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
8.9KB

  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. auto 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. {
  99. public:
  100. HashesComponent()
  101. {
  102. addAndMakeVisible (hashGroup);
  103. hashGroup.setText ("Hashes");
  104. addAndMakeVisible (hashEntryBox);
  105. hashEntryBox.setMultiLine (true);
  106. hashEntryBox.setReturnKeyStartsNewLine (true);
  107. hashEntryBox.setText ("Type some text in this box and the resulting MD5, SHA and Whirlpool hashes will update below");
  108. auto updateHashes = [this]
  109. {
  110. auto text = hashEntryBox.getText();
  111. updateMD5Result (text.toUTF8());
  112. updateSHA256Result (text.toUTF8());
  113. updateWhirlpoolResult (text.toUTF8());
  114. };
  115. hashEntryBox.onTextChange = updateHashes;
  116. hashEntryBox.onReturnKey = updateHashes;
  117. hashLabel1.setText ("Text to Hash:", dontSendNotification);
  118. hashLabel2.setText ("MD5 Result:", dontSendNotification);
  119. hashLabel3.setText ("SHA Result:", dontSendNotification);
  120. hashLabel4.setText ("Whirlpool Result:", dontSendNotification);
  121. hashLabel1.attachToComponent (&hashEntryBox, true);
  122. hashLabel2.attachToComponent (&md5Result, true);
  123. hashLabel3.attachToComponent (&shaResult, true);
  124. hashLabel4.attachToComponent (&whirlpoolResult, true);
  125. addAndMakeVisible (md5Result);
  126. addAndMakeVisible (shaResult);
  127. addAndMakeVisible (whirlpoolResult);
  128. updateHashes();
  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. auto 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 lookAndFeelChanged() override
  160. {
  161. hashGroup.setColour (GroupComponent::outlineColourId,
  162. getUIColourIfAvailable (LookAndFeel_V4::ColourScheme::UIColour::outline,
  163. Colours::grey));
  164. hashGroup.setColour (GroupComponent::textColourId,
  165. getUIColourIfAvailable (LookAndFeel_V4::ColourScheme::UIColour::defaultText,
  166. Colours::white));
  167. hashEntryBox.setColour (TextEditor::backgroundColourId,
  168. getUIColourIfAvailable (LookAndFeel_V4::ColourScheme::UIColour::widgetBackground,
  169. Colours::white.withAlpha (0.5f)));
  170. hashEntryBox.applyFontToAllText (hashEntryBox.getFont());
  171. }
  172. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (HashesComponent)
  173. };
  174. //==============================================================================
  175. class CryptographyDemo : public Component
  176. {
  177. public:
  178. CryptographyDemo()
  179. {
  180. addAndMakeVisible (rsaDemo);
  181. addAndMakeVisible (hashDemo);
  182. }
  183. void paint (Graphics& g) override
  184. {
  185. g.fillAll (getUIColourIfAvailable (LookAndFeel_V4::ColourScheme::UIColour::windowBackground,
  186. Colour::greyLevel (0.4f)));
  187. }
  188. void resized() override
  189. {
  190. Rectangle<int> area (getLocalBounds());
  191. rsaDemo.setBounds (area.removeFromTop (getHeight() / 2).reduced (5));
  192. hashDemo.setBounds (area.reduced (5));
  193. }
  194. private:
  195. RSAComponent rsaDemo;
  196. HashesComponent hashDemo;
  197. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (CryptographyDemo)
  198. };
  199. // This static object will register this demo type in a global list of demos..
  200. static JuceDemoType<CryptographyDemo> demo ("40 Cryptography");