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.

272 lines
9.5KB

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