· 6 years ago · Oct 10, 2019, 12:56 AM
1#noenv
2#persistent
3#singleinstance force
4#include include\MKM LIB.ahk
5P()
6
7P()
8{
9 /*
10 data={"App name":"MKMAPP","App type":"Dedicated"
11 . "App token":"bfaD9xOU0SXBhtBP"
12 . "App secret":"pChvrpp6AEOEwxBIIUBOvWcRG3X9xL4Y"
13 . "Access token":"lBY1xptUJ7ZJSK01x4fNwzw8kAe5b10Q "
14 . "Access token secret":"hc1wJAOX02pGGJK2uAv1ZOiwS7I9Tpoe"}
15
16 regexmatch(data,".*App token\x22:\x22(?<token>.*?)\x22.*",APP)
17 regexmatch(data,".*App secret\x22:\x22(?<secret>.*?)\x22.*",APP)
18 regexmatch(data,".*Access token\x22:\x22(?<token>.*?)\x22.*",ACCESS)
19 regexmatch(data,".*Access token secret\x22:\x22(?<tokensecret>.*?)\x22.*",ACCESS)
20 ;msgbox % APPtoken "`n" APPsecret "`n" ACCESStoken "`n" ACCESStokensecret
21
22
23
24 realm:="https://api.cardmarket.com/ws/v2.0/users/karmacrow/articles"
25 oauth_consumer_key:=APPtoken
26 oauth_nonce:=oauth_nonce()
27 oauth_signature_method:="HMAC-SHA1"
28 oauth_timestamp:=oauth_timestamp()
29 oauth_token:=ACCESStoken
30 oauth_version:="1.0"
31 oauth_signature:="I have no idea yet"
32 */
33
34
35 APPtoken:="bfaD9xOU0SXBhtBP"
36 APPsecret:="pChvrpp6AEOEwxBIIUBOvWcRG3X9xL4Y"
37 ACCESStoken:="lBY1xptUJ7ZJSK01x4fNwzw8kAe5b10Q"
38 ACCESStokensecret:="hc1wJAOX02pGGJK2uAv1ZOiwS7I9Tpoe"
39 ;I'm trying to make an API call.
40 CMD:="GET"
41 URL:=rawurlencode("https://api.cardmarket.com/ws/v1.1/account")
42 PARAM:="oauth_consumer_key%3DbfaD9xOU0SXBhtBP%26oauth_nonce%3D53eb1f44909d6%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1407917892%26oauth_token%3DlBY1xptUJ7ZJSK01x4fNwzw8kAe5b10Q%26oauth_version%3D1.0"
43 basestring:=CMD "&" URL "&" PARAM ;GET&https%3A%2F%2Fapi.cardmarket.com%2Fws%2Fv1.1%2Faccount&oauth_consumer_key%3DbfaD9xOU0SXBhtBP%26oauth_nonce%3D53eb1f44909d6%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1407917892%26oauth_token%3DlBY1xptUJ7ZJSK01x4fNwzw8kAe5b10Q%26oauth_version%3D1.0
44
45 signingkey:=rawurlencode(APPsecret) "&" rawurlencode(ACCESStokensecret)
46 ;pChvrpp6AEOEwxBIIUBOvWcRG3X9xL4Y&hc1wJAOX02pGGJK2uAv1ZOiwS7I9Tpoe
47
48
49 rawsignature:=bcrypt_sha1_hmac(basestring,signingkey)
50 ;d7adea51470fb4614bc54cea782202844ad36ca5
51 signature:=hex2base64(rawsignature)
52 ;163qUUcPtGFLxUzqeCIChErTbKU=
53 msgbox % "My result:`t" signature "`nTutorial says:`tDLGHHYV9OsbB/ARf73psEYaNWkI="
54
55 ;msgbox % realm "`n" oauth_consumer_key "`n" oauth_token "`n" oauth_nonce "`n" oauth_timestamp "`n" oauth_signature_method "`n" oauth_version "`n" oauth_signature
56}
57
58rctrl::reload
59
60
61;##### FUNCTIONS ########################################################################################
62oauth_nonce()
63{ random,nonce,-2147483648,2147483647
64 return hex2base64(SHA1(a_now a_msec nonce))
65}
66oauth_timestamp()
67{ static unixstart:=116444736000000000
68 dllcall("GetSystemTimeAsFileTime","int64p",filetime)
69 return (filetime-unixstart) // 10000000
70}
71
72SHA1(byref data,len=-1)
73{ static m32:=0xffffffff
74 if (len < 0)
75 stringlen,len,data
76 _a:=0x67452301,_b :=0xEFCDAB89,_c:=0x98BADCFE,_d:=0x10325476,_e:=0xC3D2E1F0
77 loop % blocks:=(len+8 >> 6)+1
78 { varsetcapacity(block,320,0)
79 a:=_a,b:=_b,c:=_c,d:=_d,e:=_e
80 if (len-64>=o:=a_index-1 << 6)
81 dllcall("RtlMoveMemory","Uint",&block,"UInt",&data+o,"Int",64)
82 else if (len-o>0)
83 dllcall("RtlMoveMemory","UInt",&block,"UInt",&data+o,"Int",len-o)
84 If ( len - o >= 0 && len - o < 64 )
85 NumPut(128,block,len-o,"Char")
86 If (a_index=blocks)
87 loop 8
88 numput(len << 3 >> (8-a_index << 3 )&255,block,55+a_index,"Char")
89 loop 80
90 { if (64>o:=a_index-1 << 2)
91 numput(w:=numget(block,o+3,"UChar")
92 | numget( block,o +2,"UChar") << 8
93 | numget( block,o + 1,"UChar") << 16
94 | numget( block,o,"UChar") << 24, block, o,"UInt")
95 Else
96 {
97 w := NumGet( block, o - 4 * 3, "UInt" ) ^ NumGet( block, o - 4 * 8, "UInt" )
98 ^ NumGet( block, o - 4 * 14, "UInt" ) ^ NumGet( block, o - 4 * 16, "UInt" )
99 NumPut( w := ( w << 1 | ( w >> 31 )) & m32, block, o, "UInt" )
100 }
101 If ( o < 80 )
102 f := m32 & 0x5A827999 + ( ( b & c ) | ((~b) & d) ) + ( a << 5 | ( a >> 27 )) + e + w
103 Else If ( o < 160 )
104 f := m32 & 0x6ED9EBA1 + ( b ^ c ^ d ) + ( a << 5 | ( a >> 27 )) + e + w
105 Else If ( o < 240 )
106 f := m32 & 0x8F1BBCDC + ( ( b & c ) | ( b & d ) | ( c & d ) ) + ( a << 5 | ( a >> 27 )) + e + w
107 Else f := m32 & 0xCA62C1D6 + ( b ^ c ^ d ) + ( a << 5 | ( a >> 27 )) + e + w
108 e := d, d := c, c := m32 & ( b << 30 | ( b >> 2 )), b := a, a := f
109 }
110 _a := _a + a & m32, _b := _b + b & m32, _c := _c + c & m32, _d := _d + d & m32, _e := _e + e & m32
111 }
112 VarSetCapacity( block, 64, 0 )
113 Loop 40
114 j:=chr(97+(a_index-1 >> 3))
115 , j := _%j% >> ( ( 8 - A_Index & 7 ) << 2 ) & 15
116 , block .= Chr( 48 + j + 39 * ( j > 9 ) )
117 Return block
118}
119
120
121hex2base64(hex)
122{ stringleft,n,hex,2
123 stringtrimleft,hex,hex,(n="0x") << 1
124 stringlen,l,hex
125 n:=0,l:=mod(l >> 1,3)
126 loop,parse,hex
127 { i:="0x" a_loopfield,n:=i|(n << 4)
128 if !mod(a_index,6)
129 { loop 4
130 v:=chr(101-a_index),i:=63 & n,n >>= 6
131 ,%v%:=chr(i<26?i+65:i<52?i+71:i<62?i-4:i=62?43:47)
132 if (a_index=6)
133 hex:=a b c d
134 else hex.=a b c d
135 }
136 }
137 if !(l)
138 return hex
139 n <<= 3-l << 3
140 loop 4
141 v:=chr(101-a_index),i:=63&n,n >>= 6
142 ,%v%:=chr(i<26?i+65:i<52?i+71:i<62?i-4:i=62?43:47)
143 return l=1?hex a b "==":hex a b c "="
144}
145
146rawurlencode(str)
147{ formatinteger:=a_formatinteger
148 setformat,integer,h
149 stringreplace,str,str,`%,`%25,all
150 loop
151 { if regexmatch(str,"i)[^%a-z\d-_\.]",char)
152 { code:=substr(asc(char),3)
153 if strlen(code)<2
154 code=0%Code%
155 stringupper,code,code
156 stringreplace,str,str,%Char%,`%%Code%,all
157 } else break
158 }
159 setformat,integer,%formatinteger%
160 return str
161}
162bcrypt_sha1_hmac(string, hmac, encoding := "utf-8")
163{
164 static BCRYPT_SHA1_ALGORITHM := "SHA1"
165 static BCRYPT_ALG_HANDLE_HMAC_FLAG := 0x00000008
166 static BCRYPT_OBJECT_LENGTH := "ObjectLength"
167 static BCRYPT_HASH_LENGTH := "HashDigestLength"
168
169 try
170 {
171 ; loads the specified module into the address space of the calling process
172 if !(hBCRYPT := DllCall("LoadLibrary", "str", "bcrypt.dll", "ptr"))
173 throw Exception("Failed to load bcrypt.dll", -1)
174
175 ; open an algorithm handle
176 if (NT_STATUS := DllCall("bcrypt\BCryptOpenAlgorithmProvider", "ptr*", hAlg, "ptr", &BCRYPT_SHA1_ALGORITHM, "ptr", 0, "uint", BCRYPT_ALG_HANDLE_HMAC_FLAG) != 0)
177 throw Exception("BCryptOpenAlgorithmProvider: " NT_STATUS, -1)
178
179 ; calculate the size of the buffer to hold the hash object
180 if (NT_STATUS := DllCall("bcrypt\BCryptGetProperty", "ptr", hAlg, "ptr", &BCRYPT_OBJECT_LENGTH, "uint*", cbHashObject, "uint", 4, "uint*", cbData, "uint", 0) != 0)
181 throw Exception("BCryptGetProperty: " NT_STATUS, -1)
182
183 ; allocate the hash object
184 VarSetCapacity(pbHashObject, cbHashObject, 0)
185 ; throw Exception("Memory allocation failed", -1)
186
187 ; calculate the length of the hash
188 if (NT_STATUS := DllCall("bcrypt\BCryptGetProperty", "ptr", hAlg, "ptr", &BCRYPT_HASH_LENGTH, "uint*", cbHash, "uint", 4, "uint*", cbData, "uint", 0) != 0)
189 throw Exception("BCryptGetProperty: " NT_STATUS, -1)
190
191 ; allocate the hash buffer
192 VarSetCapacity(pbHash, cbHash, 0)
193 ; throw Exception("Memory allocation failed", -1)
194
195 ; create a hash
196 VarSetCapacity(pbSecret, (StrPut(hmac, encoding) - 1) * ((encoding = "utf-16" || encoding = "cp1200") ? 2 : 1), 0) && cbSecret := StrPut(hmac, &pbSecret, encoding) - 1
197 if (NT_STATUS := DllCall("bcrypt\BCryptCreateHash", "ptr", hAlg, "ptr*", hHash, "ptr", &pbHashObject, "uint", cbHashObject, "ptr", &pbSecret, "uint", cbSecret, "uint", 0) != 0)
198 throw Exception("BCryptCreateHash: " NT_STATUS, -1)
199
200 ; hash some data
201 VarSetCapacity(pbInput, (StrPut(string, encoding) - 1) * ((encoding = "utf-16" || encoding = "cp1200") ? 2 : 1), 0) && cbInput := StrPut(string, &pbInput, encoding) - 1
202 if (NT_STATUS := DllCall("bcrypt\BCryptHashData", "ptr", hHash, "ptr", &pbInput, "uint", cbInput, "uint", 0) != 0)
203 throw Exception("BCryptHashData: " NT_STATUS, -1)
204
205 ; close the hash
206 if (NT_STATUS := DllCall("bcrypt\BCryptFinishHash", "ptr", hHash, "ptr", &pbHash, "uint", cbHash, "uint", 0) != 0)
207 throw Exception("BCryptFinishHash: " NT_STATUS, -1)
208
209 loop % cbHash
210 hash .= Format("{:02x}", NumGet(pbHash, A_Index - 1, "uchar"))
211 }
212 catch exception
213 {
214 ; represents errors that occur during application execution
215 throw Exception
216 }
217 finally
218 {
219 ; cleaning up resources
220 if (pbInput)
221 VarSetCapacity(pbInput, 0)
222 if (hHash)
223 DllCall("bcrypt\BCryptDestroyHash", "ptr", hHash)
224 if (pbHash)
225 VarSetCapacity(pbHash, 0)
226 if (pbHashObject)
227 VarSetCapacity(pbHashObject, 0)
228 if (hAlg)
229 DllCall("bcrypt\BCryptCloseAlgorithmProvider", "ptr", hAlg, "uint", 0)
230 if (hBCRYPT)
231 DllCall("FreeLibrary", "ptr", hBCRYPT)
232 }
233
234 return hash
235}
236
237;END