This repository was archived by the owner on Mar 4, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMessagingLib.bas
202 lines (154 loc) · 5.4 KB
/
MessagingLib.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
Attribute VB_Name = "MessagingLib"
Option Explicit
Public Function SHA256(ByVal sTextToHash As String, ByVal sSecretKey As String) As String
Dim asc As Object, enc As Object
Dim TextToHash() As Byte
Dim SecretKey() As Byte
Set asc = CreateObject("System.Text.UTF8Encoding")
Set enc = CreateObject("System.Security.Cryptography.HMACSHA256")
TextToHash = asc.Getbytes_4(sTextToHash)
SecretKey = asc.Getbytes_4(sSecretKey)
enc.key = SecretKey
Dim bytes() As Byte
Dim sig As String
bytes = enc.ComputeHash_2((TextToHash))
SHA256 = ConvToHexString(bytes)
Set enc = Nothing
Set asc = 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
Function GetSignature(ApiKey As String, data As String, ApiSecret As String)
Dim hexStr
hexStr = SHA256(data, ApiSecret)
GetSignature = hexStr
End Function
'Salt 생성
Function GetSalt( _
Optional ByVal Length As Long = 32, _
Optional charset As String = "abcdefghijklmnopqrstuvwxyz0123456789" _
) As String
Dim chars() As Byte, value() As Byte, chrUprBnd As Long, i As Long
If Length > 0& Then
Randomize
chars = charset
chrUprBnd = Len(charset) - 1&
Length = (Length * 2&) - 1&
ReDim value(Length) As Byte
For i = 0& To Length Step 2&
value(i) = chars(CLng(chrUprBnd * Rnd) * 2&)
Next
End If
GetSalt = value
End Function
Function GetAuth()
Dim salt
Dim dateStr
Dim data As String
salt = GetSalt()
dateStr = GetUDTDateTime
data = dateStr & salt
GetAuth = "HMAC-SHA256 apiKey=" & ApiKey & ", date=" & dateStr & ", salt=" & salt & ", signature=" & GetSignature(ApiKey, data, ApiSecret)
End Function
Public Function Request(path As String, Optional method As String = "GET", Optional data As Dictionary = vbNull) As WebResponse
Dim Client As New WebClient
Client.BaseUrl = Protocol & "://" & Domain & Prefix
Dim AuthString
AuthString = GetAuth()
Dim req As New WebRequest
req.Resource = path
Select Case method
Case "GET"
req.method = WebMethod.HttpGet
Case "POST"
req.method = WebMethod.HttpPost
End Select
req.Format = WebFormat.JSON
req.AddHeader "Authorization", AuthString
If Not IsNull(data) Then
Set req.Body = data
End If
Dim res As WebResponse
Set res = Client.Execute(req)
Dim line As Integer
Dim indent As Integer
Debug.Print (res.Content)
Set Request = res
End Function
Function ReadFile(path As String) As Byte()
On Error GoTo Handler
Dim fileNum As Integer
Dim bytes() As Byte
fileNum = FreeFile
Debug.Print (path)
Open path For Binary As fileNum
ReDim bytes(LOF(fileNum) - 1)
Get fileNum, , bytes
Close fileNum
ReadFile = bytes
Exit Function
Handler:
Debug.Print "파일을 읽어올 수 없습니다."
Debug.Print "Error " & Err.Number & Err.Description
Err.Raise Err.Number, "Source: " & Err.Source, Err.Description
End Function
Function ConvertBytesToBase64(web_Bytes() As Byte)
' Use XML to convert to Base64
Dim web_XmlObj As Object
Dim web_Node As Object
Set web_XmlObj = CreateObject("MSXML2.DOMDocument")
Set web_Node = web_XmlObj.createElement("b64")
web_Node.DataType = "bin.base64"
web_Node.nodeTypedValue = web_Bytes
ConvertBytesToBase64 = RemoveLines(web_Node.Text)
Set web_Node = Nothing
Set web_XmlObj = Nothing
End Function
Function RemoveLines(myString As String)
myString = Replace(myString, vbTab, vbNullString) ' tab 문자열을 제거
myString = Replace(myString, Chr(13), vbNullString)
myString = Replace(myString, Chr(10), vbNullString)
myString = Replace(myString, vbCrLf, vbNullString)
myString = Replace(myString, vbNewLine, vbNullString)
RemoveLines = myString
End Function
Function UploadImage(path As String) As WebResponse
Dim bytes() As Byte
Dim b64Image As String
bytes = ReadFile(path)
Dim i As Integer
b64Image = ConvertBytesToBase64(bytes)
Debug.Print (Left(b64Image, 100))
Dim Body As New Dictionary
Body.Add "type", "MMS"
Body.Add "file", b64Image
Dim Response As WebResponse
Set UploadImage = Request("storage/v1/files", "POST", Body)
End Function
Function GetFileNameFromPath(strFullPath As String) As String
GetFileNameFromPath = Right(strFullPath, Len(strFullPath) - InStrRev(strFullPath, "\"))
End Function
Function UploadKakaoImage(path As String, url As String) As WebResponse
Dim bytes() As Byte
Dim b64Image As String
bytes = ReadFile(path)
b64Image = ConvertBytesToBase64(bytes)
Dim Body As New Dictionary
Body.Add "type", "KAKAO"
Body.Add "file", b64Image
Body.Add "name", GetFileNameFromPath(path)
Body.Add "link", url
Dim Response As WebResponse
Set UploadKakaoImage = Request("storage/v1/files", "POST", Body)
End Function