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.

270 lines
9.4KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE examples.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. The code included in this file is provided under the terms of the ISC license
  6. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  7. To use, copy, modify, and/or distribute this software for any purpose with or
  8. without fee is hereby granted provided that the above copyright notice and
  9. this permission notice appear in all copies.
  10. THE SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES,
  11. WHETHER EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR
  12. PURPOSE, ARE DISCLAIMED.
  13. ==============================================================================
  14. */
  15. /*******************************************************************************
  16. The block below describes the properties of this PIP. A PIP is a short snippet
  17. of code that can be read by the Projucer and used to generate a JUCE project.
  18. BEGIN_JUCE_PIP_METADATA
  19. name: CryptographyDemo
  20. version: 1.0.0
  21. vendor: JUCE
  22. website: http://juce.com
  23. description: Encrypts and decrypts data.
  24. dependencies: juce_core, juce_cryptography, juce_data_structures, juce_events,
  25. juce_graphics, juce_gui_basics
  26. exporters: xcode_mac, vs2017, linux_make, androidstudio, xcode_iphone
  27. type: Component
  28. mainClass: CryptographyDemo
  29. useLocalCopy: 1
  30. END_JUCE_PIP_METADATA
  31. *******************************************************************************/
  32. #pragma once
  33. #include "../Assets/DemoUtilities.h"
  34. //==============================================================================
  35. class RSAComponent : public Component
  36. {
  37. public:
  38. RSAComponent()
  39. {
  40. addAndMakeVisible (rsaGroup);
  41. addAndMakeVisible (bitSize);
  42. bitSize.setText (String (256));
  43. bitSizeLabel.attachToComponent (&bitSize, true);
  44. addAndMakeVisible (generateRSAButton);
  45. generateRSAButton.onClick = [this] { createRSAKey(); };
  46. addAndMakeVisible (rsaResultBox);
  47. rsaResultBox.setReadOnly (true);
  48. rsaResultBox.setMultiLine (true);
  49. }
  50. void resized() override
  51. {
  52. auto area = getLocalBounds();
  53. rsaGroup.setBounds (area);
  54. area.removeFromTop (10);
  55. area.reduce (5, 5);
  56. auto topArea = area.removeFromTop (34);
  57. topArea.removeFromLeft (110);
  58. bitSize.setBounds (topArea.removeFromLeft (topArea.getWidth() / 2).reduced (5));
  59. generateRSAButton.setBounds (topArea.reduced (5));
  60. rsaResultBox.setBounds (area.reduced (5));
  61. }
  62. private:
  63. void createRSAKey()
  64. {
  65. auto bits = jlimit (32, 1024, bitSize.getText().getIntValue());
  66. bitSize.setText (String (bits), dontSendNotification);
  67. // Create a key-pair...
  68. RSAKey publicKey, privateKey;
  69. RSAKey::createKeyPair (publicKey, privateKey, bits);
  70. // Test the new key on a piece of data...
  71. BigInteger testValue;
  72. testValue.parseString ("1234567890abcdef", 16);
  73. auto encodedValue = testValue;
  74. publicKey.applyToValue (encodedValue);
  75. auto decodedValue = encodedValue;
  76. privateKey.applyToValue (decodedValue);
  77. // ..and show the results..
  78. String message;
  79. message << "Number of bits: " << bits << newLine
  80. << "Public Key: " << publicKey .toString() << newLine
  81. << "Private Key: " << privateKey.toString() << newLine
  82. << newLine
  83. << "Test input: " << testValue.toString (16) << newLine
  84. << "Encoded: " << encodedValue.toString (16) << newLine
  85. << "Decoded: " << decodedValue.toString (16) << newLine;
  86. rsaResultBox.setText (message, false);
  87. }
  88. GroupComponent rsaGroup { {}, "RSA Encryption" };
  89. TextButton generateRSAButton { "Generate RSA" };
  90. Label bitSizeLabel { {}, "Num Bits to Use:" };
  91. TextEditor bitSize, rsaResultBox;
  92. void lookAndFeelChanged() override
  93. {
  94. rsaGroup.setColour (GroupComponent::outlineColourId,
  95. getUIColourIfAvailable (LookAndFeel_V4::ColourScheme::UIColour::outline,
  96. Colours::grey));
  97. rsaGroup.setColour (GroupComponent::textColourId,
  98. getUIColourIfAvailable (LookAndFeel_V4::ColourScheme::UIColour::defaultText,
  99. Colours::white));
  100. rsaResultBox.setColour (TextEditor::backgroundColourId,
  101. getUIColourIfAvailable (LookAndFeel_V4::ColourScheme::UIColour::widgetBackground,
  102. Colours::white.withAlpha (0.5f)));
  103. bitSize.applyFontToAllText (bitSize.getFont());
  104. rsaResultBox.applyFontToAllText (rsaResultBox.getFont());
  105. }
  106. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (RSAComponent)
  107. };
  108. //==============================================================================
  109. class HashesComponent : public Component
  110. {
  111. public:
  112. HashesComponent()
  113. {
  114. addAndMakeVisible (hashGroup);
  115. addAndMakeVisible (hashEntryBox);
  116. hashEntryBox.setMultiLine (true);
  117. hashEntryBox.setReturnKeyStartsNewLine (true);
  118. hashEntryBox.setText ("Type some text in this box and the resulting MD5, SHA and Whirlpool hashes will update below");
  119. auto updateHashes = [this]
  120. {
  121. auto text = hashEntryBox.getText();
  122. updateMD5Result (text.toUTF8());
  123. updateSHA256Result (text.toUTF8());
  124. updateWhirlpoolResult (text.toUTF8());
  125. };
  126. hashEntryBox.onTextChange = updateHashes;
  127. hashEntryBox.onReturnKey = updateHashes;
  128. hashLabel1.attachToComponent (&hashEntryBox, true);
  129. hashLabel2.attachToComponent (&md5Result, true);
  130. hashLabel3.attachToComponent (&shaResult, true);
  131. hashLabel4.attachToComponent (&whirlpoolResult, true);
  132. addAndMakeVisible (md5Result);
  133. addAndMakeVisible (shaResult);
  134. addAndMakeVisible (whirlpoolResult);
  135. updateHashes();
  136. }
  137. void updateMD5Result (CharPointer_UTF8 text)
  138. {
  139. md5Result.setText (MD5 (text).toHexString(), dontSendNotification);
  140. }
  141. void updateSHA256Result (CharPointer_UTF8 text)
  142. {
  143. shaResult.setText (SHA256 (text).toHexString(), dontSendNotification);
  144. }
  145. void updateWhirlpoolResult (CharPointer_UTF8 text)
  146. {
  147. whirlpoolResult.setText (Whirlpool (text).toHexString(), dontSendNotification);
  148. }
  149. void resized() override
  150. {
  151. auto area = getLocalBounds();
  152. hashGroup.setBounds (area);
  153. area.removeFromLeft (120);
  154. area.removeFromTop (10);
  155. area.reduce (5, 5);
  156. whirlpoolResult.setBounds (area.removeFromBottom (34));
  157. shaResult .setBounds (area.removeFromBottom (34));
  158. md5Result .setBounds (area.removeFromBottom (34));
  159. hashEntryBox .setBounds (area.reduced (5));
  160. }
  161. private:
  162. GroupComponent hashGroup { {}, "Hashes" };
  163. TextEditor hashEntryBox;
  164. Label md5Result, shaResult, whirlpoolResult;
  165. Label hashLabel1 { {}, "Text to Hash:" };
  166. Label hashLabel2 { {}, "MD5 Result:" };
  167. Label hashLabel3 { {}, "SHA Result:" };
  168. Label hashLabel4 { {}, "Whirlpool Result:" };
  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. setSize (750, 750);
  193. }
  194. void paint (Graphics& g) override
  195. {
  196. g.fillAll (getUIColourIfAvailable (LookAndFeel_V4::ColourScheme::UIColour::windowBackground,
  197. Colour::greyLevel (0.4f)));
  198. }
  199. void resized() override
  200. {
  201. auto area = getLocalBounds();
  202. rsaDemo .setBounds (area.removeFromTop (getHeight() / 2).reduced (5));
  203. hashDemo.setBounds (area.reduced (5));
  204. }
  205. private:
  206. RSAComponent rsaDemo;
  207. HashesComponent hashDemo;
  208. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (CryptographyDemo)
  209. };