-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathmod_Hashing.bas
67 lines (61 loc) · 2.79 KB
/
mod_Hashing.bas
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
Attribute VB_Name = "mod_Hashing"
Option Explicit
'HMAC256 Functions
Public Function HMAC_256(ByRef Key As Variant, ByRef InputString As Variant) As Byte()
With CreateObject("System.Security.Cryptography.HMACSHA256")
.Key = S2B(Key)
HMAC_256 = .ComputeHash_2(S2B(InputString))
End With
End Function
Public Function HMAC_256_String(ByRef Key As Variant, ByRef InputString As Variant, Optional UpperCase As Boolean) As String
With CreateObject("System.Security.Cryptography.HMACSHA256")
.Key = S2B(Key)
Dim BArr() As Byte: BArr = .ComputeHash_2(S2B(InputString))
HMAC_256_String = B2H(BArr, UpperCase)
End With
End Function
'HMAC512 Functions
Public Function HMAC_512(ByRef Key As Variant, ByRef InputString As Variant) As Byte()
With CreateObject("System.Security.Cryptography.HMACSHA512")
.Key = S2B(Key)
HMAC_512 = .ComputeHash_2(S2B(InputString))
End With
End Function
Public Function HMAC_512_String(ByRef Key As Variant, ByRef InputString As Variant, Optional UpperCase As Boolean) As String
With CreateObject("System.Security.Cryptography.HMACSHA512")
.Key = S2B(Key)
Dim BArr() As Byte: BArr = .ComputeHash_2(S2B(InputString))
HMAC_512_String = B2H(BArr, UpperCase)
End With
End Function
'SHA256 Functions
Public Function SHA256(ByRef InputString As Variant) As Byte()
With CreateObject("System.Security.Cryptography.SHA256Managed"): SHA256 = .ComputeHash_2(S2B(InputString)): End With
End Function
Public Function SHA256_String(ByRef InputString As Variant, Optional UpperCase As Boolean) As String
With CreateObject("System.Security.Cryptography.SHA256Managed")
Dim BArr() As Byte: BArr = .ComputeHash_2(S2B(InputString))
SHA256_String = B2H(BArr, UpperCase)
End With
End Function
'SHA512 Functions
Public Function SHA512(ByRef InputString As Variant) As Byte()
With CreateObject("System.Security.Cryptography.SHA512Managed"): SHA512 = .ComputeHash_2(S2B(InputString)): End With
End Function
Public Function SHA512_String(ByRef InputString As Variant, Optional UpperCase As Boolean) As String
With CreateObject("System.Security.Cryptography.SHA512Managed")
Dim BArr() As Byte: BArr = .ComputeHash_2(S2B(InputString))
SHA512_String = B2H(BArr, UpperCase)
End With
End Function
Private Function S2B(ByRef InputStr As Variant) As Byte()
If VarType(InputStr) = vbArray + vbByte Then
S2B = InputStr
ElseIf VarType(InputStr) = vbString Then
S2B = StrConv(InputStr, vbFromUnicode)
End If
End Function
Private Function B2H(ByRef ByteArr() As Byte, Optional UpperCase As Boolean) As String
Dim i As Long: For i = 0 To UBound(ByteArr): B2H = B2H & Right(Hex(256 Or ByteArr(i)), 2): Next
B2H = IIf(UpperCase, UCase(B2H), LCase(B2H))
End Function