Link to home
Start Free TrialLog in
Avatar of bill smily
bill smily

asked on

help with sha512 key Implementation in VB6

I need Help with code implementing Sha512 in VB6

here is part of the code and the ???????? is what i need help with
think I am getting close if you review the text below

'1
Dim My_Url_ToSend As String
My_Url_ToSend = "https://bittrex.com/api/v1.1/market/buylimit?" & tx_APIKEY & "&nonce=" & Trim(str(Time())) _
& "&market=" & txCurrencyBaseSelection & "&quantity=" & txLimitOrderAmount & "&rate=" & txLimitOrderPrice
'2
Dim signback As String
signback = ????????(My_Url_ToSend, tx_APIKEY_SECRET)
'3
G_xmlhttp.Open "GET", signback
G_xmlhttp.send ""
txResponseText = G_xmlhttp.responseText

Open in new window


 the ???????? is I think what i need help with

i found this code here:
VB6 Hash Class - MD5 , SHA-1, SHA-256, SHA-384, SHA-512
http://khoiriyyah.blogspot.com/2012/06/vb6-hash-class-md5-sha-1-sha-256-sha.html
(code is pasted below), is uses "advapi32.dll"

It seems to work well

if i use   txEncripted = CreateSHA512HashString(txToEncrypt)

"The quick brown fox jumps over the lazy dog"

it does return the correct  SHA512Hash  

"07e547d9586f6a73f73fbac0435ed76951218fb7d0c8d788a309d785436bbb642e93a252a954f23912547d1e8a3b5ed6e1bfd7097821233fa0538f3db854fee6"


Now a server is giving me a Secret_Hash_Key , and a Sign_In Key.
I have a URL to send back
I'm not sure what to do next?

the php? example that was provided is:
$apikey='xxx';
$apisecret='xxx'; 
$nonce=time(); 
$uri='https://bittrex.com/api/v1.1/market/getopenorders?apikey='.$apikey.'&nonce='.$nonce; 
$sign=hash_hmac('sha512',$uri,$apisecret); 
$ch = curl_init($uri); 
curl_setopt($ch, CURLOPT_HTTPHEADER, array('apisign:'.$sign)); 
$execResult = curl_exec($ch); 

Open in new window


VBA example
nonce=int(round(time.time()*1000))
url ='https://bittrex.com/api/v1.1/market/getopenorders?apikey='+api_key+'&market=BTC-QTUM&nonce=' + str(int(time.time()))
signature = hmac.new(secret_key,url.encode(),hashlib.sha512).hexdigest()
headers = {'apisign': signature}
req = urllib.request.Request(url, headers=headers)
response = json.loads(urllib.request.urlopen(req).read())

Open in new window


This is my Current VB6 implementation without the encryption above added
Dim G_xmlhttp
Set G_xmlhttp = CreateObject("MSXML2.ServerXMLHTTP.6.0")
Dim URLA As String
URLA = "https://bittrex.com/api/v1.1/market/buylimit?" & tx_APIKEY & "&nonce=" & Trim(str(Time())) _
& "&market=" & txCurrencyBaseSelection & "&quantity=" & txLimitOrderAmount & "&rate=" & txLimitOrderPrice
G_xmlhttp.Open "GET", URLA
G_xmlhttp.send ""
txResponseText = G_xmlhttp.responseText

Open in new window



Ive got the encryption part working ok I think with

 txEncripted = CreateSHA512HashString(txToEncrypt)
(the code below)

could someone show me "how the PHP example with encryption would look in my current VB6 " 
could someone help me put the next steps together

Thank you so much experts


For Reference only This is the Code of the VB6  SHA-512 that I am using and it seems ok.
from http://khoiriyyah.blogspot.com/2012/06/vb6-hash-class-md5-sha-1-sha-256-sha.html
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 hKey As Long, ByVal dwFlags As Long, _
                             ByRef phHash As Long) As Long
Private Declare Function CryptDestroyHash Lib "advapi32.dll" _
                            (ByVal hHash As Long) As Long
Private Declare Function CryptHashData Lib "advapi32.dll" _
                            (ByVal hHash As Long, pbData As Any, ByVal cbData As Long, ByVal dwFlags As Long) As Long
Private Declare Function CryptGetHashParam Lib "advapi32.dll" _
                            (ByVal hHash As Long, ByVal dwParam As Long, pbData As Any, ByRef pcbData As Long, _
                             ByVal dwFlags As Long) As Long

Private Const PROV_RSA_FULL   As Long = 1
Private Const PROV_RSA_AES    As Long = 24
Private Const CRYPT_VERIFYCONTEXT As Long = &HF0000000

Private Const HP_HASHVAL      As Long = 2
Private Const HP_HASHSIZE     As Long = 4

Private Const ALG_TYPE_ANY    As Long = 0
Private Const ALG_CLASS_HASH  As Long = 32768

Private Const ALG_SID_MD2     As Long = 1
Private Const ALG_SID_MD4     As Long = 2
Private Const ALG_SID_MD5     As Long = 3
Private Const ALG_SID_SHA     As Long = 4
Private Const ALG_SID_SHA_256 As Long = 12
Private Const ALG_SID_SHA_384 As Long = 13
Private Const ALG_SID_SHA_512 As Long = 14

Private Const CALG_MD2        As Long = (ALG_CLASS_HASH Or ALG_TYPE_ANY Or ALG_SID_MD2)
Private Const CALG_MD4        As Long = (ALG_CLASS_HASH Or ALG_TYPE_ANY Or ALG_SID_MD4)
Private Const CALG_MD5        As Long = (ALG_CLASS_HASH Or ALG_TYPE_ANY Or ALG_SID_MD5)
Private Const CALG_SHA        As Long = (ALG_CLASS_HASH Or ALG_TYPE_ANY Or ALG_SID_SHA)
Private Const CALG_SHA_256    As Long = (ALG_CLASS_HASH Or ALG_TYPE_ANY Or ALG_SID_SHA_256)
Private Const CALG_SHA_384    As Long = (ALG_CLASS_HASH Or ALG_TYPE_ANY Or ALG_SID_SHA_384)
Private Const CALG_SHA_512    As Long = (ALG_CLASS_HASH Or ALG_TYPE_ANY Or ALG_SID_SHA_512)

' Create Hash
Private Function CreateHash(abytData() As Byte, ByVal lngAlgID As Long) As String
    Dim hProv As Long, hHash As Long
    Dim abytHash(0 To 63) As Byte
    Dim lngLength As Long
    Dim lngResult As Long
    Dim strHash As String
    Dim i As Long
    strHash = ""
    If CryptAcquireContext(hProv, vbNullString, vbNullString, _
                           IIf(lngAlgID >= CALG_SHA_256, PROV_RSA_AES, PROV_RSA_FULL), _
                           CRYPT_VERIFYCONTEXT) <> 0& Then
        If CryptCreateHash(hProv, lngAlgID, 0&, 0&, hHash) <> 0& Then
            lngLength = UBound(abytData()) - LBound(abytData()) + 1
            If lngLength > 0 Then lngResult = CryptHashData(hHash, abytData(LBound(abytData())), lngLength, 0&) _
                             Else lngResult = CryptHashData(hHash, ByVal 0&, 0&, 0&)
            If lngResult <> 0& Then
                lngLength = UBound(abytHash()) - LBound(abytHash()) + 1
                If CryptGetHashParam(hHash, HP_HASHVAL, abytHash(LBound(abytHash())), lngLength, 0&) <> 0& Then
                    For i = 0 To lngLength - 1
                        strHash = strHash & Right$("0" & Hex$(abytHash(LBound(abytHash()) + i)), 2)
                    Next
                End If
            End If
            CryptDestroyHash hHash
        End If
        CryptReleaseContext hProv, 0&
    End If
    CreateHash = LCase$(strHash)
End Function

' Create Hash From String(Shift_JIS)
Private Function CreateHashString(ByVal strData As String, ByVal lngAlgID As Long) As String
    CreateHashString = CreateHash(StrConv(strData, vbFromUnicode), lngAlgID)
End Function

' Create Hash From File
Private Function CreateHashFile(ByVal strFileName As String, ByVal lngAlgID As Long) As String
    Dim abytData() As Byte
    Dim intFile As Integer
    Dim lngError As Long
    On Error Resume Next
        If Len(Dir(strFileName)) > 0 Then
            intFile = FreeFile
            Open strFileName For Binary Access Read Shared As #intFile
            abytData() = InputB(LOF(intFile), #intFile)
            Close #intFile
        End If
        lngError = Err.Number
    On Error GoTo 0
    If lngError = 0 Then CreateHashFile = CreateHash(abytData(), lngAlgID) _
                    Else CreateHashFile = ""
End Function

' MD5
Public Function CreateMD5Hash(abytData() As Byte) As String
    CreateMD5Hash = CreateHash(abytData(), CALG_MD5)
End Function
Public Function CreateMD5HashString(ByVal strData As String) As String
    CreateMD5HashString = CreateHashString(strData, CALG_MD5)
End Function
Public Function CreateMD5HashFile(ByVal strFileName As String) As String
    CreateMD5HashFile = CreateHashFile(strFileName, CALG_MD5)
End Function

' SHA-1
Public Function CreateSHA1Hash(abytData() As Byte) As String
    CreateSHA1Hash = CreateHash(abytData(), CALG_SHA)
End Function
Public Function CreateSHA1HashString(ByVal strData As String) As String
    CreateSHA1HashString = CreateHashString(strData, CALG_SHA)
End Function
Public Function CreateSHA1HashFile(ByVal strFileName As String) As String
    CreateSHA1HashFile = CreateHashFile(strFileName, CALG_SHA)
End Function

' SHA-256
Public Function CreateSHA256Hash(abytData() As Byte) As String
    CreateSHA256Hash = CreateHash(abytData(), CALG_SHA_256)
End Function
Public Function CreateSHA256HashString(ByVal strData As String) As String
    CreateSHA256HashString = CreateHashString(strData, CALG_SHA_256)
End Function
Public Function CreateSHA256HashFile(ByVal strFileName As String) As String
    CreateSHA256HashFile = CreateHashFile(strFileName, CALG_SHA_256)
End Function

' SHA-384
Public Function CreateSHA384Hash(abytData() As Byte) As String
    CreateSHA384Hash = CreateHash(abytData(), CALG_SHA_384)
End Function
Public Function CreateSHA384HashString(ByVal strData As String) As String
    CreateSHA384HashString = CreateHashString(strData, CALG_SHA_384)
End Function
Public Function CreateSHA384HashFile(ByVal strFileName As String) As String
    CreateSHA384HashFile = CreateHashFile(strFileName, CALG_SHA_384)
End Function

' SHA-512
Public Function CreateSHA512Hash(abytData() As Byte) As String
    CreateSHA512Hash = CreateHash(abytData(), CALG_SHA_512)
End Function
Public Function CreateSHA512HashString(ByVal strData As String) As String
    CreateSHA512HashString = CreateHashString(strData, CALG_SHA_512)
End Function
Public Function CreateSHA512HashFile(ByVal strFileName As String) As String
    CreateSHA512HashFile = CreateHashFile(strFileName, CALG_SHA_512)
End Function

Open in new window

Avatar of aikimark
aikimark
Flag of United States of America image

Can you surround your question code with CODE tags?
Avatar of bill smily
bill smily

ASKER

oh cool thanks aikimark re code tags
Thanks.  This is much easier to read
I think I found a working example here
'https://fujori.com/wp-content/uploads/2018/02/bittrex_connect.txt

i think if.net framework installed it will work ok
I am testing this tonight

Public Function hashHmacSHA512(uri As String, apikey As String)
   
    Dim keyArray(64) As Byte
    Dim i As Long
    Dim text As Object
    Dim SHA512 As Object
    Dim xml As Object
   
    On Error GoTo erre5423:
   
    For i = 0 To 63
        keyArray(i) = Asc(Mid(apikey, i + 1, 1))
    Next
    On Error GoTo 0
   
    Set text = CreateObject("System.Text.UTF8Encoding")
    Set SHA512 = CreateObject("System.Security.Cryptography.HMACSHA512")
    Set xml = CreateObject("MSXML2.DOMDocument")
   
    SHA512.key = keyArray()
    xml.loadXML "<root />"
    xml.DocumentElement.DataType = "bin.Hex"
    xml.DocumentElement.nodeTypedValue = SHA512.ComputeHash_2((text.GetBytes_4(uri)))
    hashHmacSHA512 = Replace(xml.DocumentElement.text, vbLf, "")
Exit Function
erre5423:
MsgBox "err hashHmacSHA512 " & Err.Description
End Function
ASKER CERTIFIED SOLUTION
Avatar of bill smily
bill smily

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial