home *** CD-ROM | disk | FTP | other *** search
/ Chip 2002 September / Chip_2002-09_cd1.bin / zkuste / vbasic / Data / Utils / XZipComp.exe / XceedEncryption.Cab / F112951_Hasher.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  2001-07-04  |  14.2 KB  |  356 lines

  1. VERSION 5.00
  2. Object = "{F9043C88-F6F2-101A-A3C9-08002B2F49FB}#1.2#0"; "comdlg32.ocx"
  3. Begin VB.Form frmHasher 
  4.    BorderStyle     =   3  'Fixed Dialog
  5.    Caption         =   "Hasher"
  6.    ClientHeight    =   5715
  7.    ClientLeft      =   45
  8.    ClientTop       =   330
  9.    ClientWidth     =   8580
  10.    LinkTopic       =   "Form1"
  11.    MaxButton       =   0   'False
  12.    MinButton       =   0   'False
  13.    ScaleHeight     =   5715
  14.    ScaleWidth      =   8580
  15.    StartUpPosition =   2  'CenterScreen
  16.    Begin VB.CommandButton cmdOption 
  17.       Caption         =   "Option"
  18.       Height          =   315
  19.       Left            =   150
  20.       TabIndex        =   7
  21.       Top             =   5325
  22.       Width           =   1140
  23.    End
  24.    Begin VB.Frame fraMessageHashing 
  25.       Height          =   2265
  26.       Left            =   150
  27.       TabIndex        =   10
  28.       Top             =   900
  29.       Width           =   8340
  30.       Begin VB.OptionButton optMessageHashing 
  31.          Caption         =   "Message hashing"
  32.          Height          =   240
  33.          Left            =   75
  34.          TabIndex        =   3
  35.          Top             =   0
  36.          Width           =   1590
  37.       End
  38.       Begin VB.TextBox txtMessageToHash 
  39.          Height          =   1635
  40.          Left            =   150
  41.          MultiLine       =   -1  'True
  42.          OLEDropMode     =   1  'Manual
  43.          ScrollBars      =   2  'Vertical
  44.          TabIndex        =   4
  45.          Top             =   525
  46.          Width           =   8115
  47.       End
  48.       Begin VB.Label lbl 
  49.          Caption         =   "Message to hash"
  50.          Height          =   240
  51.          Index           =   2
  52.          Left            =   150
  53.          TabIndex        =   12
  54.          Top             =   300
  55.          Width           =   1290
  56.       End
  57.    End
  58.    Begin VB.Frame fraFileHashing 
  59.       Height          =   690
  60.       Left            =   150
  61.       TabIndex        =   9
  62.       Top             =   75
  63.       Width           =   8340
  64.       Begin VB.TextBox txtFileToHash 
  65.          Height          =   285
  66.          Left            =   1500
  67.          OLEDropMode     =   1  'Manual
  68.          TabIndex        =   1
  69.          Top             =   300
  70.          Width           =   6390
  71.       End
  72.       Begin VB.CommandButton cmdSelectFile 
  73.          Caption         =   "..."
  74.          Height          =   285
  75.          Left            =   7950
  76.          TabIndex        =   2
  77.          Top             =   300
  78.          Width           =   285
  79.       End
  80.       Begin VB.OptionButton optFileHashing 
  81.          Caption         =   "File hashing"
  82.          Height          =   240
  83.          Left            =   75
  84.          TabIndex        =   0
  85.          Top             =   0
  86.          Width           =   1140
  87.       End
  88.       Begin VB.Label lbl 
  89.          Caption         =   "File to hash"
  90.          Height          =   240
  91.          Index           =   0
  92.          Left            =   150
  93.          TabIndex        =   13
  94.          Top             =   300
  95.          Width           =   1290
  96.       End
  97.    End
  98.    Begin VB.Frame fraHashValue 
  99.       Caption         =   "Hash value"
  100.       Height          =   1440
  101.       Left            =   150
  102.       TabIndex        =   11
  103.       Top             =   3750
  104.       Width           =   8340
  105.       Begin VB.TextBox txtHashValue 
  106.          Height          =   1065
  107.          Left            =   150
  108.          MultiLine       =   -1  'True
  109.          ScrollBars      =   2  'Vertical
  110.          TabIndex        =   6
  111.          Top             =   300
  112.          Width           =   8115
  113.       End
  114.    End
  115.    Begin VB.CommandButton cmdQuit 
  116.       Cancel          =   -1  'True
  117.       Caption         =   "Quit"
  118.       Height          =   315
  119.       Left            =   7350
  120.       TabIndex        =   8
  121.       Top             =   5325
  122.       Width           =   1140
  123.    End
  124.    Begin VB.CommandButton cmdHash 
  125.       Caption         =   "Hash"
  126.       Height          =   315
  127.       Left            =   7350
  128.       TabIndex        =   5
  129.       Top             =   3300
  130.       Width           =   1140
  131.    End
  132.    Begin MSComDlg.CommonDialog dlgCommon 
  133.       Left            =   1875
  134.       Top             =   5175
  135.       _ExtentX        =   847
  136.       _ExtentY        =   847
  137.       _Version        =   393216
  138.    End
  139. Attribute VB_Name = "frmHasher"
  140. Attribute VB_GlobalNameSpace = False
  141. Attribute VB_Creatable = False
  142. Attribute VB_PredeclaredId = True
  143. Attribute VB_Exposed = False
  144. ' Xceed Encryption Library - Hasher sample
  145. ' Copyright (c) 2001 Xceed Software Inc.
  146. ' [Hasher.frm]
  147. ' This form module contains the main form's code. It demonstrates how to
  148. ' Hash a chunk of memory data or a file . It specifically uses:
  149. '  - The Hash and ReadFile methods.
  150. '  - The HashSize, HashValue and HashingMethod properties.
  151. ' This file is part of the Xceed Encryption Library sample applications.
  152. ' The source code in this file is only intended as a supplement to Xceed
  153. ' Encryption Library's documentation, and is provided "as is", without
  154. ' warranty of any kind, either expressed or implied.
  155. Option Explicit
  156. 'The values chosen by the user in the Option form
  157. Private m_nHashSize As Integer
  158. Private m_eHashingMethod As enuHashingMethod
  159. '====================================================================================
  160. ' EVENTS - triggered by the form and its controls
  161. '====================================================================================
  162. '------------------------------------------------------------------------------------
  163. 'Do the Hashing of the message or file
  164. '------------------------------------------------------------------------------------
  165. Private Sub cmdHash_Click()
  166.     Dim xHasher As XceedHashing
  167.     Dim xSHA As XceedSHAHashingMethod
  168.     Dim xHaval As XceedHavalHashingMethod
  169.     Dim vaMessageToHash As Variant
  170.     Dim lErrorNumber As Long
  171.     Dim sHashValue As String
  172.     'Create an instance of the Xceed Hashing
  173.     Set xHasher = New XceedHashing
  174.     lErrorNumber = 0
  175.     On Error Resume Next
  176.         'Set the hashing method and hash size that will be used to
  177.         'calculate the hash value.
  178.         Select Case m_eHashingMethod
  179.             Case ehmHaval
  180.                 Set xHaval = New XceedHavalHashingMethod
  181.                 xHaval.HashSize = m_nHashSize
  182.                 If Err.Number = 0 Then
  183.                     Set xHasher.HashingMethod = xHaval
  184.                 End If
  185.                 Set xHaval = Nothing
  186.             Case ehmSHA
  187.                 Set xSHA = New XceedSHAHashingMethod
  188.                 xSHA.HashSize = m_nHashSize
  189.                 If Err.Number = 0 Then
  190.                     Set xHasher.HashingMethod = xSHA
  191.                 End If
  192.                 Set xSHA = Nothing
  193.         End Select
  194.         
  195.         If Err.Number <> 0 Then
  196.             lErrorNumber = Err.Number
  197.             Call MsgBox("Error during HashingMethod initialization." & vbCrLf & Err.Description & " (" & Hex(Err.Number) & ")")
  198.         End If
  199.     On Error GoTo 0
  200.     If lErrorNumber = 0 Then
  201.         If optFileHashing.Value Then
  202.             'Calculate the hash value of the specified file
  203.             On Error Resume Next
  204.                 'Hash the file using the ReadFile method
  205.                 Call xHasher.ReadFile(txtFileToHash.Text, 0, 0, efpHash, True)
  206.                 If Err.Number <> 0 Then
  207.                     lErrorNumber = Err.Number
  208.                     Call MsgBox("Error during ReadFile." & vbCrLf & Err.Description & " (" & Hex(Err.Number) & ")")
  209.                 End If
  210.             On Error GoTo 0
  211.         Else
  212.             'Calculate the hash value of the specified message
  213.             
  214.             'Convert unicode text to an ASCII string as the message will,
  215.             'most probably, be sent in ASCII format.
  216.             vaMessageToHash = StrConv(txtMessageToHash.Text, vbFromUnicode)
  217.             
  218.             On Error Resume Next
  219.                 'Hash the ascii string, specifying that (True parameter)
  220.                 'this is the end of data (there will be no more calls to Hash).
  221.                 Call xHasher.Hash(vaMessageToHash, True)
  222.                 
  223.                 If lErrorNumber <> 0 Then
  224.                     lErrorNumber = Err.Number
  225.                     Call MsgBox("Error during Hash." & vbCrLf & Err.Description & " (" & Hex(Err.Number) & ")")
  226.                 End If
  227.             On Error GoTo 0
  228.         End If
  229.     End If
  230.     On Error Resume Next
  231.         'Convert the resulting signature to an hexadecimal representation that
  232.         'can be shown in a text box.
  233.         sHashValue = BinaryToHex(xHasher.HashingMethod.HashValue)
  234.         If Err.Number <> 0 Then
  235.             lErrorNumber = Err.Number
  236.             Call MsgBox("Error in hash value." & vbCrLf & Err.Description & " (" & Hex(Err.Number) & ")")
  237.         End If
  238.     On Error GoTo 0
  239.     If lErrorNumber = 0 Then
  240.         If Len(sHashValue) = 0 Then
  241.             'No output was produced
  242.             txtHashValue.Text = ""
  243.         Else
  244.             'Display the hash value in the text box
  245.             'The BinaryToHex method returning a BSTR (Unicode), we
  246.             'don't have to convert the encrypted string. We can affect
  247.             'it directly to the textbox
  248.             txtHashValue.Text = sHashValue
  249.         End If
  250.     End If
  251.     'Deallocate the Hashing object.
  252.     Set xHasher = Nothing
  253. End Sub
  254. '------------------------------------------------------------------------------------
  255. 'Display the options form, saving them if the user click OK
  256. '------------------------------------------------------------------------------------
  257. Private Sub cmdOption_Click()
  258.     Dim xFrmOption As frmOption
  259.     Set xFrmOption = New frmOption
  260.     If xFrmOption.ShowForm(m_eHashingMethod, m_nHashSize) Then
  261.         Call SaveOption
  262.     End If
  263.     Set xFrmOption = Nothing
  264. End Sub
  265. '------------------------------------------------------------------------------------
  266. 'Quit the sample application
  267. '------------------------------------------------------------------------------------
  268. Private Sub cmdQuit_Click()
  269.     End
  270. End Sub
  271. '------------------------------------------------------------------------------------
  272. 'Select the file name that contain (or will contain) the private key.
  273. '------------------------------------------------------------------------------------
  274. Private Sub cmdSelectFile_Click()
  275.     With dlgCommon
  276.         .FileName = ""
  277.         .DialogTitle = "File to hash"
  278.         .Filter = "All type (*.*)|*.*"
  279.         .FilterIndex = 0
  280.         .Flags = cdlOFNExplorer
  281.         On Error Resume Next
  282.             Call .ShowOpen
  283.             If Len(.FileName) > 0 Then
  284.                 txtFileToHash.Text = .FileName
  285.             End If
  286.         On Error GoTo 0
  287.     End With
  288. End Sub
  289. '------------------------------------------------------------------------------------
  290. 'Initialize the default and last saved options
  291. '------------------------------------------------------------------------------------
  292. Private Sub Form_Load()
  293.     Call LoadOption
  294.     Call EnableControls
  295. End Sub
  296. '------------------------------------------------------------------------------------
  297. 'The user selected file hashing, disable controls associated with message
  298. 'hashing
  299. '------------------------------------------------------------------------------------
  300. Private Sub optFileHashing_Click()
  301.     If Me.Visible Then
  302.         optMessageHashing.Value = False
  303.         Call EnableControls
  304.         Call SaveObjectToHash
  305.     End If
  306. End Sub
  307. '------------------------------------------------------------------------------------
  308. 'The user selected message hashing, disable controls associated with file
  309. 'hashing
  310. '------------------------------------------------------------------------------------
  311. Private Sub optMessageHashing_Click()
  312.     If Me.Visible Then
  313.         optFileHashing.Value = False
  314.         Call EnableControls
  315.         Call SaveObjectToHash
  316.     End If
  317. End Sub
  318. '====================================================================================
  319. ' FUNCTIONS
  320. '====================================================================================
  321. '------------------------------------------------------------------------------------
  322. 'Enable the control associated with the selected object to hash (file or
  323. 'message). Disable the other controls.
  324. '------------------------------------------------------------------------------------
  325. Private Sub EnableControls()
  326.     Dim bFileHashing As Boolean
  327.     bFileHashing = optFileHashing.Value
  328.     txtFileToHash.Enabled = bFileHashing
  329.     cmdSelectFile.Enabled = bFileHashing
  330.     txtMessageToHash.Enabled = Not bFileHashing
  331. End Sub
  332. '------------------------------------------------------------------------------------
  333. 'Load in the member variables the options saved in the registry the last
  334. 'time this sample file manager was called.
  335. '------------------------------------------------------------------------------------
  336. Private Sub LoadOption()
  337.     m_eHashingMethod = Val(GetSetting("XceedHasher", "Hashing", "HashingMethod", "0"))
  338.     m_nHashSize = Val(GetSetting("XceedHasher", "Hashing", "HashSize", "160"))
  339.     optFileHashing.Value = Val(GetSetting("XceedHasher", "Hashing", "FileHashing", CStr(CInt(True))))
  340.     optMessageHashing.Value = Val(GetSetting("XceedHasher", "Hashing", "MessageHashing", CStr(CInt(False))))
  341. End Sub
  342. '------------------------------------------------------------------------------------
  343. 'Save the current selected (and deselected) object to hash (file or message)
  344. '------------------------------------------------------------------------------------
  345. Private Sub SaveObjectToHash()
  346.     Call SaveSetting("XceedHasher", "Hashing", "FileHashing", CStr(CInt(optFileHashing.Value)))
  347.     Call SaveSetting("XceedHasher", "Hashing", "MessageHashing", CStr(CInt(optMessageHashing.Value)))
  348. End Sub
  349. '------------------------------------------------------------------------------------
  350. 'Save the current options from the member variables in the registry
  351. '------------------------------------------------------------------------------------
  352. Private Sub SaveOption()
  353.     Call SaveSetting("XceedHasher", "Hashing", "HashingMethod", m_eHashingMethod)
  354.     Call SaveSetting("XceedHasher", "Hashing", "HashSize", m_nHashSize)
  355. End Sub
  356.