Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VB6 SHA1 Hash help

Status
Not open for further replies.

tdong

Programmer
Mar 12, 2004
112
CA
Hi
I have vb6 program and an asp.net website using the same database I need to be able to create the user log in for the web using the vb6 program.
asp.net is using SHA1 has to store the password. I am trying to use the ebCrypt 1.0 but it generate a different value. Anyone know any code algorithm to generate the same password as asp.net library ?

Thanks
 
Try this:

Option Explicit

Private Declare Function CryptAcquireContext Lib "advapi32.dll" Alias "CryptAcquireContextA" (ByRef phProv As Long, ByVal pszContainer As String, ByVal pszProvider As String, ByVal dwProvType As Long, ByVal dwFlags As Long) As Long
Private Declare Function CryptReleaseContext Lib "advapi32.dll" (ByVal hProv As Long, ByVal dwFlags As Long) As Long
Private Declare Function CryptCreateHash Lib "advapi32.dll" (ByVal hProv As Long, ByVal Algid As Long, ByVal hSessionKey As Long, ByVal dwFlags As Long, ByRef phHash As Long) As Long
Private Declare Function CryptHashData Lib "advapi32.dll" (ByVal hHash As Long, ByVal pbData As String, ByVal dwDataLen As Long, ByVal dwFlags As Long) As Long
Private Declare Function CryptGetHashParam Lib "advapi32.dll" (ByVal hHash As Long, ByVal dwParam As Long, ByVal pbData As String, ByRef pdwDataLen As Long, ByVal dwFlags As Long) As Long
Private Declare Function CryptDestroyHash Lib "advapi32.dll" (ByVal hHash As Long) As Long

Private Const SERVICE_PROVIDER As String = "Microsoft Enhanced Cryptographic Provider v1.0" & vbNullChar
Private Const KEY_CONTAINER As String = "GCN SSL Container" & vbNullChar
Private Const HP_HASHVAL As Long = 2
Private Const PROV_RSA_FULL As Long = 1
Private Const CRYPT_VERIFYCONTEXT = &HF0000000
Private Const CRYPT_NEWKEYSET As Long = 8
Private Const CALG_SHA1 As Long = 32772
Public Function GetHash(ByVal strData As String, Optional boolHexOut As Boolean = False) As String
Dim strHash As String
Dim hCryptProv As Long
Dim hHash As Long
Dim lngHashLen As Long
Dim bytActiveChar As Byte
Dim strTmp As String
Call CryptAcquireContext(hCryptProv, KEY_CONTAINER, SERVICE_PROVIDER, PROV_RSA_FULL, 0)
Call CryptCreateHash(hCryptProv, CALG_SHA1, 0, 0, hHash)
Call CryptHashData(hHash, strData, Len(strData), 0)
Call CryptGetHashParam(hHash, HP_HASHVAL, vbNull, lngHashLen, 0)
strHash = String(lngHashLen, vbNullChar)
Call CryptGetHashParam(hHash, HP_HASHVAL, strHash, lngHashLen, 0)
If hHash <> 0 Then CryptDestroyHash hHash
If hCryptProv <> 0 Then CryptReleaseContext hCryptProv, 0
Dim i As Integer
If boolHexOut = True Then
For i = 1 To Len(strHash)
strTmp = strTmp & Right("0" & Hex(Asc(Mid(strHash, i, 1))), 2)
Next
GetHash = strTmp
Else
GetHash = strHash
End If
End Function



Two strings walk into a bar. The first string says to the bartender: 'Bartender, I'll have a beer. u.5n$x5t?*&4ru!2[sACC~ErJ'. The second string says: 'Pardon my friend, he isn't NULL terminated'.
 
(quick note: you don't really need a key container when doing simple hashes)
 
Hi
when I test it this function return nothing
I create a simple form
two textbox and one button

Private Sub Command1_Click()
Text2 = GetHash(Text1)
End Sub

text2 doesn't display anything
 
hmmm, works here on Windows XP SP2. What operating system are you running, also what value do you have in Text1, though it should matter? Is Text2 a multiline text box?




Two strings walk into a bar. The first string says to the bartender: 'Bartender, I'll have a beer. u.5n$x5t?*&4ru!2[sACC~ErJ'. The second string says: 'Pardon my friend, he isn't NULL terminated'.
 
text1 I just type it in text2 doesn't have multiline I will try again thanks for your help. I am using XP SP2 too
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top