Bonjour,

Petit soucis sur le code VBA (code qui hache des données avec une clé secrète via l'algo HMAC SHA1) ci-dessous :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
Public Function HEX_HMACSHA1(ByVal sTextToHash As String, ByVal sSharedSecretKey As String)
    Dim asc As Object, enc As Object
    Dim TextToHash() As Byte
    Dim SharedSecretKey() As Byte
 
    Set asc = CreateObject("System.Text.UTF8Encoding")
    Set enc = CreateObject("System.Security.Cryptography.HMACSHA1")
 
 
    TextToHash = asc.Getbytes_4(sTextToHash)
    SharedSecretKey = asc.Getbytes_4(sSharedSecretKey)
    enc.Key = SharedSecretKey
 
    Dim Bytes() As Byte
    Bytes = enc.ComputeHash_2((TextToHash))
    HEX_HMACSHA1 = ConvToHexString(Bytes)
    Set asc = Nothing
    Set enc = Nothing
 
End Function
 
 
Private Function ConvToHexString(vIn As Variant) As Variant
    'Check that Net Framework 3.5 (includes .Net 2 and .Net 3 is installed in windows
    'and not just Net Advanced Services
 
    Dim oD As Object
 
    Set oD = CreateObject("MSXML2.DOMDocument")
 
      With oD
        .LoadXML "<root />"
        .DocumentElement.DataType = "bin.Hex"
        .DocumentElement.nodeTypedValue = vIn
      End With
    ConvToHexString = Replace(oD.DocumentElement.Text, vbLf, "")
 
    Set oD = Nothing
 
End Function
La variable "sSharedSecretKey" contient des caractères spéciaux, or les caractères spéciaux sont de la forme "«". Le résultat du code n'est pas celui attendu car la conversion en Byte n'est pas bonne.
Je suppose que c'est à cause de l'encodage UTF8 mais je ne sais pas du tout comment changer ça.

Pourriez vous m'aider ?

Jérémy