{***************************************************************************** The DEC team (see file NOTICE.txt) licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. A copy of this licence is found in the root directory of this project in the file LICENCE.txt or alternatively at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *****************************************************************************} unit MainForm; interface uses System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants, FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.StdCtrls, FMX.Controls.Presentation, FMX.ScrollBox, FMX.Memo, FMX.Layouts, FMX.ListBox, FMX.Edit; type TFormMain = class(TForm) VertScrollBox1: TVertScrollBox; LayoutBottom: TLayout; Label3: TLabel; Label4: TLabel; ButtonCalc: TButton; EditInput: TEdit; EditOutput: TEdit; Label2: TLabel; Label5: TLabel; ComboBoxHashFunction: TComboBox; Label6: TLabel; ComboBoxInputFormatting: TComboBox; ComboBoxOutputFormatting: TComboBox; LayoutTop: TLayout; CheckBoxLiveCalc: TCheckBox; LabelVersion: TLabel; CheckBoxIsPasswordHash: TCheckBox; procedure FormCreate(Sender: TObject); procedure FormResize(Sender: TObject); procedure ButtonCalcClick(Sender: TObject); procedure ComboBoxHashFunctionChange(Sender: TObject); procedure EditInputChangeTracking(Sender: TObject); procedure EditInputKeyUp(Sender: TObject; var Key: Word; var KeyChar: Char; Shift: TShiftState); private /// /// Lists all available hash classes in the hash classes combo box /// procedure InitHashCombo; /// /// Lists all available formatting classes in the formatting classes /// combo boxes /// procedure InitFormatCombos; /// /// Displays a given error message text in a non modal message box /// /// /// Text to show as error message /// procedure ShowErrorMessage(ErrorMsg: string); /// /// Returns the full class name of the selected hash class. /// /// /// Full class name instead of the displayed algorithm name. It does not /// guard agains having nothing selected. /// function GetSelectedHashClassName: string; public end; var FormMain: TFormMain; implementation uses DECBaseClass, DECHashBase, DECHash, DECHashAUthentication, DECFormatBase, DECFormat, DECUtil, Generics.Collections, FMX.Platform {$IFDEF Android} , Androidapi.JNI.GraphicsContentViewText, Androidapi.Helpers, Androidapi.JNI.App {$ENDIF}; {$R *.fmx} procedure TFormMain.ButtonCalcClick(Sender: TObject); var Hash : TDECHash; InputFormatting : TDECFormatClass; OutputFormatting : TDECFormatClass; InputBuffer : TBytes; OutputBuffer : TBytes; begin if ComboBoxInputFormatting.ItemIndex >= 0 then begin // Find the class type of the selected formatting class and create an instance of it InputFormatting := TDECFormat.ClassByName( ComboBoxInputFormatting.Items[ComboBoxInputFormatting.ItemIndex]); end else begin ShowErrorMessage('No input format selected'); exit; end; if ComboBoxOutputFormatting.ItemIndex >= 0 then begin // Find the class type of the selected formatting class and create an instance of it OutputFormatting := TDECFormat.ClassByName( ComboBoxOutputFormatting.Items[ComboBoxOutputFormatting.ItemIndex]); end else begin ShowErrorMessage('No input format selected'); exit; end; if ComboBoxHashFunction.ItemIndex >= 0 then begin // Find the class type of the selected hash class and create an instance of it Hash := TDECHash.ClassByName(GetSelectedHashClassName).Create; try InputBuffer := System.SysUtils.BytesOf(EditInput.Text); if InputFormatting.IsValid(InputBuffer) then begin OutputBuffer := Hash.CalcBytes(InputFormatting.Decode(InputBuffer)); EditOutput.Text := string(DECUtil.BytesToRawString(OutputFormatting.Encode(OutputBuffer))); end else ShowErrorMessage('Input has wrong format'); finally Hash.Free; end; end; end; procedure TFormMain.ShowErrorMessage(ErrorMsg: string); {$IF RTLVersion > 30} var AsyncDlg : IFMXDialogServiceASync; {$ENDIF} begin {$IF RTLVersion > 30} if TPlatformServices.Current.SupportsPlatformService(IFMXDialogServiceAsync, IInterface(AsyncDlg)) then AsyncDlg.MessageDialogAsync(Translate(ErrorMsg), TMsgDlgType.mtError, [TMsgDlgBtn.mbOk], TMsgDlgBtn.mbOk, 0, procedure (const AResult: TModalResult) begin end); {$ELSE} MessageDlg(Translate(ErrorMsg), TMsgDlgType.mtError, [TMsgDlgBtn.mbOk], 0); {$ENDIF} end; procedure TFormMain.ComboBoxHashFunctionChange(Sender: TObject); begin CheckBoxIsPasswordHash.IsChecked := TDECHash.ClassByName(GetSelectedHashClassName).IsPasswordHash; end; procedure TFormMain.EditInputChangeTracking(Sender: TObject); begin if CheckBoxLiveCalc.IsChecked then ButtonCalcClick(self); end; procedure TFormMain.EditInputKeyUp(Sender: TObject; var Key: Word; var KeyChar: Char; Shift: TShiftState); begin if (Key = vkReturn) then ButtonCalcClick(self); end; procedure TFormMain.FormCreate(Sender: TObject); var AppService : IFMXApplicationService; begin if TPlatformServices.Current.SupportsPlatformService(IFMXApplicationService, IInterface(AppService)) then LabelVersion.Text := format(LabelVersion.Text, [AppService.AppVersion]) else LabelVersion.Text := format(LabelVersion.Text, ['']); InitHashCombo; InitFormatCombos; end; procedure TFormMain.InitFormatCombos; var MyClass : TPair; Formats : TStringList; CopyIdx : Integer; begin Formats := TStringList.Create; try for MyClass in TDECFormat.ClassList do Formats.Add(MyClass.Value.GetShortClassName); Formats.Sort; ComboBoxInputFormatting.Items.AddStrings(Formats); ComboBoxOutputFormatting.Items.AddStrings(Formats); if Formats.Count > 0 then begin if Formats.Find('TFormat_Copy', CopyIdx) then begin ComboBoxInputFormatting.ItemIndex := CopyIdx; ComboBoxOutputFormatting.ItemIndex := CopyIdx; end else begin ComboBoxInputFormatting.ItemIndex := 0; ComboBoxOutputFormatting.ItemIndex := 0; end; end; finally Formats.Free; end; end; procedure TFormMain.InitHashCombo; var MyClass : TPair; Hashes : TStringList; begin Hashes := TStringList.Create; try for MyClass in TDECHash.ClassList do Hashes.Add(MyClass.Value.GetShortClassName); Hashes.Sort; ComboBoxHashFunction.Items.AddStrings(Hashes); if Hashes.Count > 0 then ComboBoxHashFunction.ItemIndex := 0; finally Hashes.Free; end; end; procedure TFormMain.FormResize(Sender: TObject); begin LayoutTop.Width := VertScrollBox1.Width; LayoutBottom.Width := VertScrollBox1.Width; end; function TFormMain.GetSelectedHashClassName: string; begin Result := 'THash_' + ComboBoxHashFunction.Items[ComboBoxHashFunction.ItemIndex]; end; end.