home *** CD-ROM | disk | FTP | other *** search
- VERSION 5.00
- Object = "{F9043C88-F6F2-101A-A3C9-08002B2F49FB}#1.2#0"; "comdlg32.ocx"
- Begin VB.Form frmHasher
- BorderStyle = 3 'Fixed Dialog
- Caption = "Hasher"
- ClientHeight = 5715
- ClientLeft = 45
- ClientTop = 330
- ClientWidth = 8580
- LinkTopic = "Form1"
- MaxButton = 0 'False
- MinButton = 0 'False
- ScaleHeight = 5715
- ScaleWidth = 8580
- StartUpPosition = 2 'CenterScreen
- Begin VB.CommandButton cmdOption
- Caption = "Option"
- Height = 315
- Left = 150
- TabIndex = 7
- Top = 5325
- Width = 1140
- End
- Begin VB.Frame fraMessageHashing
- Height = 2265
- Left = 150
- TabIndex = 10
- Top = 900
- Width = 8340
- Begin VB.OptionButton optMessageHashing
- Caption = "Message hashing"
- Height = 240
- Left = 75
- TabIndex = 3
- Top = 0
- Width = 1590
- End
- Begin VB.TextBox txtMessageToHash
- Height = 1635
- Left = 150
- MultiLine = -1 'True
- OLEDropMode = 1 'Manual
- ScrollBars = 2 'Vertical
- TabIndex = 4
- Top = 525
- Width = 8115
- End
- Begin VB.Label lbl
- Caption = "Message to hash"
- Height = 240
- Index = 2
- Left = 150
- TabIndex = 12
- Top = 300
- Width = 1290
- End
- End
- Begin VB.Frame fraFileHashing
- Height = 690
- Left = 150
- TabIndex = 9
- Top = 75
- Width = 8340
- Begin VB.TextBox txtFileToHash
- Height = 285
- Left = 1500
- OLEDropMode = 1 'Manual
- TabIndex = 1
- Top = 300
- Width = 6390
- End
- Begin VB.CommandButton cmdSelectFile
- Caption = "..."
- Height = 285
- Left = 7950
- TabIndex = 2
- Top = 300
- Width = 285
- End
- Begin VB.OptionButton optFileHashing
- Caption = "File hashing"
- Height = 240
- Left = 75
- TabIndex = 0
- Top = 0
- Width = 1140
- End
- Begin VB.Label lbl
- Caption = "File to hash"
- Height = 240
- Index = 0
- Left = 150
- TabIndex = 13
- Top = 300
- Width = 1290
- End
- End
- Begin VB.Frame fraHashValue
- Caption = "Hash value"
- Height = 1440
- Left = 150
- TabIndex = 11
- Top = 3750
- Width = 8340
- Begin VB.TextBox txtHashValue
- Height = 1065
- Left = 150
- MultiLine = -1 'True
- ScrollBars = 2 'Vertical
- TabIndex = 6
- Top = 300
- Width = 8115
- End
- End
- Begin VB.CommandButton cmdQuit
- Cancel = -1 'True
- Caption = "Quit"
- Height = 315
- Left = 7350
- TabIndex = 8
- Top = 5325
- Width = 1140
- End
- Begin VB.CommandButton cmdHash
- Caption = "Hash"
- Height = 315
- Left = 7350
- TabIndex = 5
- Top = 3300
- Width = 1140
- End
- Begin MSComDlg.CommonDialog dlgCommon
- Left = 1875
- Top = 5175
- _ExtentX = 847
- _ExtentY = 847
- _Version = 393216
- End
- Attribute VB_Name = "frmHasher"
- Attribute VB_GlobalNameSpace = False
- Attribute VB_Creatable = False
- Attribute VB_PredeclaredId = True
- Attribute VB_Exposed = False
- ' Xceed Encryption Library - Hasher sample
- ' Copyright (c) 2001 Xceed Software Inc.
- ' [Hasher.frm]
- ' This form module contains the main form's code. It demonstrates how to
- ' Hash a chunk of memory data or a file . It specifically uses:
- ' - The Hash and ReadFile methods.
- ' - The HashSize, HashValue and HashingMethod properties.
- ' This file is part of the Xceed Encryption Library sample applications.
- ' The source code in this file is only intended as a supplement to Xceed
- ' Encryption Library's documentation, and is provided "as is", without
- ' warranty of any kind, either expressed or implied.
- Option Explicit
- 'The values chosen by the user in the Option form
- Private m_nHashSize As Integer
- Private m_eHashingMethod As enuHashingMethod
- '====================================================================================
- ' EVENTS - triggered by the form and its controls
- '====================================================================================
- '------------------------------------------------------------------------------------
- 'Do the Hashing of the message or file
- '------------------------------------------------------------------------------------
- Private Sub cmdHash_Click()
- Dim xHasher As XceedHashing
- Dim xSHA As XceedSHAHashingMethod
- Dim xHaval As XceedHavalHashingMethod
- Dim vaMessageToHash As Variant
- Dim lErrorNumber As Long
- Dim sHashValue As String
- 'Create an instance of the Xceed Hashing
- Set xHasher = New XceedHashing
- lErrorNumber = 0
- On Error Resume Next
- 'Set the hashing method and hash size that will be used to
- 'calculate the hash value.
- Select Case m_eHashingMethod
- Case ehmHaval
- Set xHaval = New XceedHavalHashingMethod
- xHaval.HashSize = m_nHashSize
- If Err.Number = 0 Then
- Set xHasher.HashingMethod = xHaval
- End If
- Set xHaval = Nothing
- Case ehmSHA
- Set xSHA = New XceedSHAHashingMethod
- xSHA.HashSize = m_nHashSize
- If Err.Number = 0 Then
- Set xHasher.HashingMethod = xSHA
- End If
- Set xSHA = Nothing
- End Select
-
- If Err.Number <> 0 Then
- lErrorNumber = Err.Number
- Call MsgBox("Error during HashingMethod initialization." & vbCrLf & Err.Description & " (" & Hex(Err.Number) & ")")
- End If
- On Error GoTo 0
- If lErrorNumber = 0 Then
- If optFileHashing.Value Then
- 'Calculate the hash value of the specified file
- On Error Resume Next
- 'Hash the file using the ReadFile method
- Call xHasher.ReadFile(txtFileToHash.Text, 0, 0, efpHash, True)
- If Err.Number <> 0 Then
- lErrorNumber = Err.Number
- Call MsgBox("Error during ReadFile." & vbCrLf & Err.Description & " (" & Hex(Err.Number) & ")")
- End If
- On Error GoTo 0
- Else
- 'Calculate the hash value of the specified message
-
- 'Convert unicode text to an ASCII string as the message will,
- 'most probably, be sent in ASCII format.
- vaMessageToHash = StrConv(txtMessageToHash.Text, vbFromUnicode)
-
- On Error Resume Next
- 'Hash the ascii string, specifying that (True parameter)
- 'this is the end of data (there will be no more calls to Hash).
- Call xHasher.Hash(vaMessageToHash, True)
-
- If lErrorNumber <> 0 Then
- lErrorNumber = Err.Number
- Call MsgBox("Error during Hash." & vbCrLf & Err.Description & " (" & Hex(Err.Number) & ")")
- End If
- On Error GoTo 0
- End If
- End If
- On Error Resume Next
- 'Convert the resulting signature to an hexadecimal representation that
- 'can be shown in a text box.
- sHashValue = BinaryToHex(xHasher.HashingMethod.HashValue)
- If Err.Number <> 0 Then
- lErrorNumber = Err.Number
- Call MsgBox("Error in hash value." & vbCrLf & Err.Description & " (" & Hex(Err.Number) & ")")
- End If
- On Error GoTo 0
- If lErrorNumber = 0 Then
- If Len(sHashValue) = 0 Then
- 'No output was produced
- txtHashValue.Text = ""
- Else
- 'Display the hash value in the text box
- 'The BinaryToHex method returning a BSTR (Unicode), we
- 'don't have to convert the encrypted string. We can affect
- 'it directly to the textbox
- txtHashValue.Text = sHashValue
- End If
- End If
- 'Deallocate the Hashing object.
- Set xHasher = Nothing
- End Sub
- '------------------------------------------------------------------------------------
- 'Display the options form, saving them if the user click OK
- '------------------------------------------------------------------------------------
- Private Sub cmdOption_Click()
- Dim xFrmOption As frmOption
- Set xFrmOption = New frmOption
- If xFrmOption.ShowForm(m_eHashingMethod, m_nHashSize) Then
- Call SaveOption
- End If
- Set xFrmOption = Nothing
- End Sub
- '------------------------------------------------------------------------------------
- 'Quit the sample application
- '------------------------------------------------------------------------------------
- Private Sub cmdQuit_Click()
- End
- End Sub
- '------------------------------------------------------------------------------------
- 'Select the file name that contain (or will contain) the private key.
- '------------------------------------------------------------------------------------
- Private Sub cmdSelectFile_Click()
- With dlgCommon
- .FileName = ""
- .DialogTitle = "File to hash"
- .Filter = "All type (*.*)|*.*"
- .FilterIndex = 0
- .Flags = cdlOFNExplorer
- On Error Resume Next
- Call .ShowOpen
- If Len(.FileName) > 0 Then
- txtFileToHash.Text = .FileName
- End If
- On Error GoTo 0
- End With
- End Sub
- '------------------------------------------------------------------------------------
- 'Initialize the default and last saved options
- '------------------------------------------------------------------------------------
- Private Sub Form_Load()
- Call LoadOption
- Call EnableControls
- End Sub
- '------------------------------------------------------------------------------------
- 'The user selected file hashing, disable controls associated with message
- 'hashing
- '------------------------------------------------------------------------------------
- Private Sub optFileHashing_Click()
- If Me.Visible Then
- optMessageHashing.Value = False
- Call EnableControls
- Call SaveObjectToHash
- End If
- End Sub
- '------------------------------------------------------------------------------------
- 'The user selected message hashing, disable controls associated with file
- 'hashing
- '------------------------------------------------------------------------------------
- Private Sub optMessageHashing_Click()
- If Me.Visible Then
- optFileHashing.Value = False
- Call EnableControls
- Call SaveObjectToHash
- End If
- End Sub
- '====================================================================================
- ' FUNCTIONS
- '====================================================================================
- '------------------------------------------------------------------------------------
- 'Enable the control associated with the selected object to hash (file or
- 'message). Disable the other controls.
- '------------------------------------------------------------------------------------
- Private Sub EnableControls()
- Dim bFileHashing As Boolean
- bFileHashing = optFileHashing.Value
- txtFileToHash.Enabled = bFileHashing
- cmdSelectFile.Enabled = bFileHashing
- txtMessageToHash.Enabled = Not bFileHashing
- End Sub
- '------------------------------------------------------------------------------------
- 'Load in the member variables the options saved in the registry the last
- 'time this sample file manager was called.
- '------------------------------------------------------------------------------------
- Private Sub LoadOption()
- m_eHashingMethod = Val(GetSetting("XceedHasher", "Hashing", "HashingMethod", "0"))
- m_nHashSize = Val(GetSetting("XceedHasher", "Hashing", "HashSize", "160"))
- optFileHashing.Value = Val(GetSetting("XceedHasher", "Hashing", "FileHashing", CStr(CInt(True))))
- optMessageHashing.Value = Val(GetSetting("XceedHasher", "Hashing", "MessageHashing", CStr(CInt(False))))
- End Sub
- '------------------------------------------------------------------------------------
- 'Save the current selected (and deselected) object to hash (file or message)
- '------------------------------------------------------------------------------------
- Private Sub SaveObjectToHash()
- Call SaveSetting("XceedHasher", "Hashing", "FileHashing", CStr(CInt(optFileHashing.Value)))
- Call SaveSetting("XceedHasher", "Hashing", "MessageHashing", CStr(CInt(optMessageHashing.Value)))
- End Sub
- '------------------------------------------------------------------------------------
- 'Save the current options from the member variables in the registry
- '------------------------------------------------------------------------------------
- Private Sub SaveOption()
- Call SaveSetting("XceedHasher", "Hashing", "HashingMethod", m_eHashingMethod)
- Call SaveSetting("XceedHasher", "Hashing", "HashSize", m_nHashSize)
- End Sub
-