Dim Digest 
Digest= "01234567890123456"

Dim TheForm
Dim bErr
Dim h
Dim w

Sub openlogon()
	h = screen.availHeight-27
	w = screen.availWidth-10
	window.open "about:blank","console_sh","height="&h&", width="&w&", top=0, left=0, toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, status=no"	
End Sub

Sub ShowErr(Msg)
	bErr = true
    TheForm.userpin.value = "------ERROR!------"
	MsgBox Msg
End Sub

Sub SetUserPIN (Pin)
    TheForm.userpin.readonly = true
	TheForm.userpin.value = Pin 
	TheForm.action = "logon.u"
	TheForm.target = "console_sh" 
	openlogon
end Sub

function Validate()
	Digest = "01234567890123456"
	On Error Resume Next
	
	Set TheForm = Document.forms("logon")
	If Len(TheForm.UserPIN.Value) < 4  Then
		MsgBox "错误(E001)：PIN码为空或长度小于4位！"	 
		Validate = false
		Exit Function
	End If

	bErr = false

	eToken.GetLibVersion

	If Err Then
		ShowErr "错误(E002)：加载密钥盘驱动失败！请安装密钥盘驱动程序。"
		Validate = false
		Exit function
	Else

		eToken.OpenDevice 1, ""
		
		If Err then
			ShowErr "错误(E003)：密钥盘读取失败！请确认已正确插入密钥盘。"
			Validate = false
			Exit function
		End if
			
		'you can use this function to reset securityState
		'eToken.ResetSecurityState 0
		dim Results
		Results = "01234567890123456"
		Results = eToken.GetStrProperty(7, 0, 0)
		
		eToken.VerifyPIN 0, CStr(TheForm.userpin.value)
		If Err Then
			ShowErr "错误(E004)：用户PIN码错误！"
			Validate = false
			eToken.CloseDevice
			Exit function
		End If		

		If Not bErr Then
			eToken.ChangeDir &H300, 0, "ASP_DEMO"
			If Err then 
				ShowErr "错误(E005)：读取身份文件失败！"
				Validate = false
				eToken.CloseDevice
				Exit function
			End If
		End If


		'Open the first key file.
		If Not bErr Then
			eToken.OpenFile 0, 1
			If Err Then 
				ShowErr "错误(E006)：读取身份文件失败！"
				Validate = false
				eToken.CloseDevice
				Exit function
			End If
		End If

		'Do HASH-MD5-HMAC compute.
		If Not bErr Then
			Digest = eToken.HashToken (1, 2, RandomData)
			If Err Then 
				ShowErr "错误(E007)：读取身份文件失败！"
				Validate = false
				eToken.CloseDevice
				Exit function
			End If		
		    SetUserPIN(Results & Digest)
		End If
	End If
	eToken.CloseDevice
End function