-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathclass_Crypt.ahk
591 lines (557 loc) · 20 KB
/
class_Crypt.ahk
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
class Crypt{
/*
Crypt class
Currently Contains two classes and different methods for encryption and hashing
Classes:
Crypt.Encrypt - Encryption class
Crypt.Hash - Hashing class
=====================================================================
Methods:
=====================================================================
Crypt.Encrypt.FileEncrypt(pFileIn,pFileOut,password,CryptAlg = 1, HashAlg = 1)
Encrypts the file
Parameters:
pFileIn - path to file which to encrypt
pFileOut - path to save encrypted file
password - no way, it's just a password...
(optional) CryptAlg - Encryption algorithm ID, for details see below
(optional) HashAlg - hashing algorithm ID, for details see below
Return:
on success, - Number of bytes writen to pFileOut
on fail, - ""
--------
Crypt.Encrypt.FileDecrypt(pFileIn,pFileOut,password,CryptAlg = 1, HashAlg = 1)
Decrypts the file, the parameters are identical to FileEncrypt, except:
pFileIn - path to encrypted file which to decrypt
pFileOut - path to save decrypted file
=====================================================================
Crypt.Encrypt.StrEncrypt(string,password,CryptAlg = 1, HashAlg = 1)
Encrypts the string
Parameters:
string - UTF string, means any string you use in AHK_L Unicode
password - no way, it's just a password...
(optional) CryptAlg - Encryption algorithm ID, for details see below
(optional) HashAlg - hashing algorithm ID, for details see below
Return:
on success, - HASH representaion of encrypted buffer, which is easily transferable.
You can get actual encrypted buffer from HASH by using function HashToByte()
on fail, - ""
--------
Crypt.Encrypt.StrDecrypt(EncryptedHash,password,CryptAlg = 1, HashAlg = 1)
Decrypts the string, the parameters are identical to StrEncrypt, except:
EncryptedHash - hash string returned by StrEncrypt()
=====================================================================
Crypt.Hash.FileHash(pFile,HashAlg = 1,pwd = "",hmac_alg = 1)
--------
Gets the HASH of file
Parameters:
pFile - path to file which hash will be calculated
(optional) HashAlg - hashing algorithm ID, for details see below
(optional) pwd - password, if present - the hashing algorith will use HMAC to calculate hash
(optional) hmac_alg - Encryption algorithm ID of HMAC key, will be used if pwd parameter present
Return:
on success, - HASH of target file calculated using choosen algorithm
on fail, - ""
--------
Crypt.Hash.StrHash(string,HashAlg = 1,pwd = "",hmac_alg = 1)
Gets the HASH of string. HASH will be calculated for ANSI representation of passed string
Parameters:
string - UTF string
other parameters same as for FileHash
=====================================================================
FileEncryptToStr(pFileIn,password,CryptAlg = 1, HashAlg = 1)
--------
Encrypt file and returns it's hash
Parameters:
pFileIn - path to file which will be encrypted
password - no way, it's just a password...
(optional) CryptAlg - Encryption algorithm ID, for details see below
(optional) HashAlg - hashing algorithm ID, for details see below
Return:
on success, - HASH of target file calculated using choosen algorithm
on fail, - ""
=====================================================================
StrDecryptToFile(EncryptedHash,pFileOut,password,CryptAlg = 1, HashAlg = 1)
Decrypt EncryptedHash to file and returns amount of bytes writen to file
Parameters:
EncryptedHash - hash of formerly encrypted data
pFileOut - path to destination file where decrypted data will be writen
password - no way, it's just a password...
(optional) CryptAlg - Encryption algorithm ID, for details see below
(optional) HashAlg - hashing algorithm ID, for details see below
Return:
on success, - amount of bytes writen to the destination file
on fail, - ""
=====================================================================
Crypt.Encrypt class contain following fields
Crypt.Encrypt.StrEncoding - encoding of string passed to Crypt.Encrypt.StrEncrypt()
Crypt.Encrypt.PassEncoding - password encoding for each of Crypt.Encrypt methods
Same is valid for Crypt.Hash class
HASH and Encryption algorithms currently available:
HashAlg IDs:
1 - MD5
2 - MD2
3 - SHA
4 - SHA_256 ;Vista+ only
5 - SHA_384 ;Vista+ only
6 - SHA_512 ;Vista+ only
--------
CryptAlg and hmac_alg IDs:
1 - RC4
2 - RC2
3 - 3DES
4 - 3DES_112
5 - AES_128 ;not supported for win 2000
6 - AES_192 ;not supported for win 2000
7 - AES_256 ;not supported for win 2000
=====================================================================
*/
class Encrypt {
static StrEncoding := "UTF-16"
static PassEncoding := "UTF-16"
StrDecryptToFile(EncryptedHash,pFileOut,password,CryptAlg = 1, HashAlg = 1) {
if !EncryptedHash
return ""
if !len := b64Decode( EncryptedHash, encr_Buf )
return ""
temp_file := "crypt.temp"
f := FileOpen(temp_file,"w","CP0")
if !IsObject(f)
return ""
if !f.RawWrite(encr_Buf,len)
return ""
f.close()
bytes := this._Encrypt( p, pp, password, 0, temp_file, pFileOut, CryptAlg, HashAlg )
FileDelete,% temp_file
return bytes
}
FileEncryptToStr(pFileIn,password,CryptAlg = 1, HashAlg = 1) {
temp_file := "crypt.temp"
if !this._Encrypt( p, pp, password, 1, pFileIn, temp_file, CryptAlg, HashAlg )
return ""
f := FileOpen(temp_file,"r","CP0")
if !IsObject(f)
{
FileDelete,% temp_file
return ""
}
f.Pos := 0
fLen := f.Length
VarSetCapacity(tembBuf,fLen,0)
if !f.RawRead(tembBuf,fLen)
{
Free(tembBuf)
return ""
}
f.Close()
FileDelete,% temp_file
return b64Encode( tembBuf, fLen )
}
FileEncrypt(pFileIn,pFileOut,password,CryptAlg = 1, HashAlg = 1) {
return this._Encrypt( p, pp, password, 1, pFileIn, pFileOut, CryptAlg, HashAlg )
}
FileDecrypt(pFileIn,pFileOut,password,CryptAlg = 1, HashAlg = 1) {
return this._Encrypt( p, pp, password, 0, pFileIn, pFileOut, CryptAlg, HashAlg )
}
StrEncrypt(string,password,CryptAlg = 1, HashAlg = 1) {
len := StrPutVar(string, str_buf,100,this.StrEncoding)
if this._Encrypt(str_buf,len, password, 1,0,0,CryptAlg,HashAlg)
return b64Encode( str_buf, len )
else
return ""
}
StrDecrypt(EncryptedHash,password,CryptAlg = 1, HashAlg = 1) {
if !EncryptedHash
return ""
if !len := b64Decode( EncryptedHash, encr_Buf )
return 0
if sLen := this._Encrypt(encr_Buf,len, password, 0,0,0,CryptAlg,HashAlg) {
if ( this.StrEncoding = "utf-16" || this.StrEncoding = "cp1200" )
sLen /= 2
return strget(&encr_Buf,sLen,this.StrEncoding)
}
else
return ""
}
_Encrypt(ByRef encr_Buf,ByRef Buf_Len, password, mode, pFileIn=0, pFileOut=0, CryptAlg = 1,HashAlg = 1) { ;mode - 1 encrypt, 0 - decrypt
c := CryptConst
;password hashing algorithms
CUR_PWD_HASH_ALG := HashAlg == 1 || HashAlg = "MD5" ?c.CALG_MD5
:HashAlg==2 || HashAlg = "MD2" ?c.CALG_MD2
:HashAlg==3 || HashAlg = "SHA" ?c.CALG_SHA
:HashAlg==4 || HashAlg = "SHA256" ?c.CALG_SHA_256 ;Vista+ only
:HashAlg==5 || HashAlg = "SHA384" ?c.CALG_SHA_384 ;Vista+ only
:HashAlg==6 || HashAlg = "SHA512" ?c.CALG_SHA_512 ;Vista+ only
:0
;encryption algorithms
CUR_ENC_ALG := CryptAlg==1 || CryptAlg = "RC4" ? ( c.CALG_RC4, KEY_LENGHT:=0x80 )
:CryptAlg==2 || CryptAlg = "RC2" ? ( c.CALG_RC2, KEY_LENGHT:=0x80 )
:CryptAlg==3 || CryptAlg = "3DES" ? ( c.CALG_3DES, KEY_LENGHT:=0xC0 )
:CryptAlg==4 || CryptAlg = "3DES112" ? ( c.CALG_3DES_112, KEY_LENGHT:=0x80 )
:CryptAlg==5 || CryptAlg = "AES128" ? ( c.CALG_AES_128, KEY_LENGHT:=0x80 ) ;not supported for win 2000
:CryptAlg==6 || CryptAlg = "AES192" ? ( c.CALG_AES_192, KEY_LENGHT:=0xC0 ) ;not supported for win 2000
:CryptAlg==7 || CryptAlg = "AES256" ? ( c.CALG_AES_256, KEY_LENGHT:=0x100 ) ;not supported for win 2000
:0
KEY_LENGHT <<= 16
if (CUR_PWD_HASH_ALG = 0 || CUR_ENC_ALG = 0)
return 0
if !dllCall("Advapi32\CryptAcquireContextW","Ptr*",hCryptProv,"Uint",0,"Uint",0,"Uint",c.PROV_RSA_AES,"UInt",c.CRYPT_VERIFYCONTEXT)
{foo := "CryptAcquireContextW", err := GetLastError(), err2 := ErrorLevel
GoTO FINITA_LA_COMEDIA
}
if !dllCall("Advapi32\CryptCreateHash","Ptr",hCryptProv,"Uint",CUR_PWD_HASH_ALG,"Uint",0,"Uint",0,"Ptr*",hHash )
{foo := "CryptCreateHash", err := GetLastError(), err2 := ErrorLevel
GoTO FINITA_LA_COMEDIA
}
;hashing password
passLen := StrPutVar(password, passBuf,0,this.PassEncoding)
if !dllCall("Advapi32\CryptHashData","Ptr",hHash,"Ptr",&passBuf,"Uint",passLen,"Uint",0 )
{foo := "CryptHashData", err := GetLastError(), err2 := ErrorLevel
GoTO FINITA_LA_COMEDIA
}
;getting encryption key from password
if !dllCall("Advapi32\CryptDeriveKey","Ptr",hCryptProv,"Uint",CUR_ENC_ALG,"Ptr",hHash,"Uint",KEY_LENGHT,"Ptr*",hKey )
{foo := "CryptDeriveKey", err := GetLastError(), err2 := ErrorLevel
GoTO FINITA_LA_COMEDIA
}
;~ SetKeySalt(hKey,hCryptProv)
if !dllCall("Advapi32\CryptGetKeyParam","Ptr",hKey,"Uint",c.KP_BLOCKLEN,"Uint*",BlockLen,"Uint*",dwCount := 4,"Uint",0)
{foo := "CryptGetKeyParam", err := GetLastError(), err2 := ErrorLevel
GoTO FINITA_LA_COMEDIA
}
BlockLen /= 8
if (mode == 1) ;Encrypting
{
if (pFileIn && pFileOut) ;encrypting file
{
ReadBufSize := 10240 - mod(10240,BlockLen==0?1:BlockLen ) ;10KB
pfin := FileOpen(pFileIn,"r","CP0")
pfout := FileOpen(pFileOut,"w","CP0")
if !IsObject(pfin)
{foo := "File Opening " . pFileIn
GoTO FINITA_LA_COMEDIA
}
if !IsObject(pfout)
{foo := "File Opening " . pFileOut
GoTO FINITA_LA_COMEDIA
}
pfin.Pos := 0
VarSetCapacity(ReadBuf,ReadBufSize+BlockLen,0)
isFinal := 0
hModule := DllCall("LoadLibrary", "Str", "Advapi32.dll","UPtr")
CryptEnc := DllCall("GetProcAddress", "Ptr", hModule, "AStr", "CryptEncrypt","UPtr")
while !pfin.AtEOF
{
BytesRead := pfin.RawRead(ReadBuf, ReadBufSize)
if pfin.AtEOF
isFinal := 1
if !dllCall(CryptEnc
,"Ptr",hKey ;key
,"Ptr",0 ;hash
,"Uint",isFinal ;final
,"Uint",0 ;dwFlags
,"Ptr",&ReadBuf ;pbdata
,"Uint*",BytesRead ;dwsize
,"Uint",ReadBufSize+BlockLen ) ;dwbuf
{foo := "CryptEncrypt", err := GetLastError(), err2 := ErrorLevel
GoTO FINITA_LA_COMEDIA
}
pfout.RawWrite(ReadBuf,BytesRead)
Buf_Len += BytesRead
}
DllCall("FreeLibrary", "Ptr", hModule)
pfin.Close()
pfout.Close()
}
else
{
if !dllCall("Advapi32\CryptEncrypt"
,"Ptr",hKey ;key
,"Ptr",0 ;hash
,"Uint",1 ;final
,"Uint",0 ;dwFlags
,"Ptr",&encr_Buf ;pbdata
,"Uint*",Buf_Len ;dwsize
,"Uint",Buf_Len + BlockLen ) ;dwbuf
{foo := "CryptEncrypt", err := GetLastError(), err2 := ErrorLevel
GoTO FINITA_LA_COMEDIA
}
}
}
else if (mode == 0) ;decrypting
{
if (pFileIn && pFileOut) ;decrypting file
{
ReadBufSize := 10240 - mod(10240,BlockLen==0?1:BlockLen ) ;10KB
pfin := FileOpen(pFileIn,"r","CP0")
pfout := FileOpen(pFileOut,"w","CP0")
if !IsObject(pfin)
{foo := "File Opening " . pFileIn
GoTO FINITA_LA_COMEDIA
}
if !IsObject(pfout)
{foo := "File Opening " . pFileOut
GoTO FINITA_LA_COMEDIA
}
pfin.Pos := 0
VarSetCapacity(ReadBuf,ReadBufSize+BlockLen,0)
isFinal := 0
hModule := DllCall("LoadLibrary", "Str", "Advapi32.dll","UPtr")
CryptDec := DllCall("GetProcAddress", "Ptr", hModule, "AStr", "CryptDecrypt","UPtr")
while !pfin.AtEOF
{
BytesRead := pfin.RawRead(ReadBuf, ReadBufSize)
if pfin.AtEOF
isFinal := 1
if !dllCall(CryptDec
,"Ptr",hKey ;key
,"Ptr",0 ;hash
,"Uint",isFinal ;final
,"Uint",0 ;dwFlags
,"Ptr",&ReadBuf ;pbdata
,"Uint*",BytesRead ) ;dwsize
{foo := "CryptDecrypt", err := GetLastError(), err2 := ErrorLevel
GoTO FINITA_LA_COMEDIA
}
pfout.RawWrite(ReadBuf,BytesRead)
Buf_Len += BytesRead
}
DllCall("FreeLibrary", "Ptr", hModule)
pfin.Close()
pfout.Close()
}
else if !dllCall("Advapi32\CryptDecrypt"
,"Ptr",hKey ;key
,"Ptr",0 ;hash
,"Uint",1 ;final
,"Uint",0 ;dwFlags
,"Ptr",&encr_Buf ;pbdata
,"Uint*",Buf_Len ) ;dwsize
{foo := "CryptDecrypt", err := GetLastError(), err2 := ErrorLevel
GoTO FINITA_LA_COMEDIA
}
}
FINITA_LA_COMEDIA:
dllCall("Advapi32\CryptDestroyKey","Ptr",hKey )
dllCall("Advapi32\CryptDestroyHash","Ptr",hHash)
dllCall("Advapi32\CryptReleaseContext","Ptr",hCryptProv,"UInt",0)
if (A_ThisLabel = "FINITA_LA_COMEDIA")
{
if (A_IsCompiled = 1)
return ""
else
msgbox % foo " call failed with:`nErrorLevel: " err2 "`nLastError: " err "`n" ErrorFormat(err)
return ""
}
return Buf_Len
}
}
class Hash {
static StrEncoding := "CP0"
static PassEncoding := "UTF-16"
FileHash(pFile,HashAlg = 1,pwd = "",hmac_alg = 1) {
return this._CalcHash(p,pp,pFile,HashAlg,pwd,hmac_alg)
}
StrHash(string,HashAlg = 1,pwd = "",hmac_alg = 1) { ;strType 1 for ASC, 0 for UTF
buf_len := StrPutVar(string, buf,0,this.StrEncoding)
return this._CalcHash(buf,buf_len,0,HashAlg,pwd,hmac_alg)
}
_CalcHash(ByRef bBuffer,BufferLen,pFile,HashAlg = 1,pwd = "",hmac_alg = 1) {
c := CryptConst
;password hashing algorithms
HASH_ALG := HashAlg==1?c.CALG_MD5
:HashAlg==2?c.CALG_MD2
:HashAlg==3?c.CALG_SHA
:HashAlg==4?c.CALG_SHA_256 ;Vista+ only
:HashAlg==5?c.CALG_SHA_384 ;Vista+ only
:HashAlg==6?c.CALG_SHA_512 ;Vista+ only
:0
;encryption algorithms
HMAC_KEY_ALG := hmac_alg==1?c.CALG_RC4
:hmac_alg==2?c.CALG_RC2
:hmac_alg==3?c.CALG_3DES
:hmac_alg==4?c.CALG_3DES_112
:hmac_alg==5?c.CALG_AES_128 ;not supported for win 2000
:hmac_alg==6?c.CALG_AES_192 ;not supported for win 2000
:hmac_alg==7?c.CALG_AES_256 ;not supported for win 2000
:0
KEY_LENGHT := hmac_alg==1?0x80
:hmac_alg==2?0x80
:hmac_alg==3?0xC0
:hmac_alg==4?0x80
:hmac_alg==5?0x80
:hmac_alg==6?0xC0
:hmac_alg==7?0x100
:0
KEY_LENGHT <<= 16
if (!HASH_ALG || !HMAC_KEY_ALG)
return 0
if !dllCall("Advapi32\CryptAcquireContextW","Ptr*",hCryptProv,"Uint",0,"Uint",0,"Uint",c.PROV_RSA_AES,"UInt",c.CRYPT_VERIFYCONTEXT )
{foo := "CryptAcquireContextW", err := GetLastError(), err2 := ErrorLevel
GoTO FINITA_DA_COMEDIA
}
if !dllCall("Advapi32\CryptCreateHash","Ptr",hCryptProv,"Uint",HASH_ALG,"Uint",0,"Uint",0,"Ptr*",hHash )
{foo := "CryptCreateHash1", err := GetLastError(), err2 := ErrorLevel
GoTO FINITA_DA_COMEDIA
}
if (pwd != "") ;going HMAC
{
passLen := StrPutVar(pwd, passBuf,0,this.PassEncoding)
if !dllCall("Advapi32\CryptHashData","Ptr",hHash,"Ptr",&passBuf,"Uint",passLen,"Uint",0 )
{foo := "CryptHashData Pwd", err := GetLastError(), err2 := ErrorLevel
GoTO FINITA_DA_COMEDIA
}
;getting encryption key from password
if !dllCall("Advapi32\CryptDeriveKey","Ptr",hCryptProv,"Uint",HMAC_KEY_ALG,"Ptr",hHash,"Uint",KEY_LENGHT,"Ptr*",hKey )
{foo := "CryptDeriveKey Pwd", err := GetLastError(), err2 := ErrorLevel
GoTO FINITA_DA_COMEDIA
}
dllCall("Advapi32\CryptDestroyHash","Ptr",hHash)
if !dllCall("Advapi32\CryptCreateHash","Ptr",hCryptProv,"Uint",c.CALG_HMAC,"Ptr",hKey,"Uint",0,"Ptr*",hHash )
{foo := "CryptCreateHash2", err := GetLastError(), err2 := ErrorLevel
GoTO FINITA_DA_COMEDIA
}
VarSetCapacity(HmacInfoStruct,4*A_PtrSize + 4,0)
NumPut(HASH_ALG,HmacInfoStruct,0,"UInt")
if !dllCall("Advapi32\CryptSetHashParam","Ptr",hHash,"Uint",c.HP_HMAC_INFO,"Ptr",&HmacInfoStruct,"Uint",0)
{foo := "CryptSetHashParam", err := GetLastError(), err2 := ErrorLevel
GoTO FINITA_DA_COMEDIA
}
}
if pFile
{
f := FileOpen(pFile,"r","CP0")
BUFF_SIZE := 1024 * 1024 ; 1 MB
if !IsObject(f)
{foo := "File Opening"
GoTO FINITA_DA_COMEDIA
}
if !hModule := DllCall( "GetModuleHandleW", "str", "Advapi32.dll", "UPtr" )
hModule := DllCall( "LoadLibraryW", "str", "Advapi32.dll", "UPtr" )
hCryptHashData := DllCall("GetProcAddress", "Ptr", hModule, "AStr", "CryptHashData", "UPtr")
VarSetCapacity(read_buf,BUFF_SIZE,0)
f.Pos := 0
While (cbCount := f.RawRead(read_buf, BUFF_SIZE))
{
if (cbCount = 0)
break
if !dllCall(hCryptHashData
,"Ptr",hHash
,"Ptr",&read_buf
,"Uint",cbCount
,"Uint",0 )
{foo := "CryptHashData", err := GetLastError(), err2 := ErrorLevel
GoTO FINITA_DA_COMEDIA
}
}
f.Close()
}
else
{
if !dllCall("Advapi32\CryptHashData"
,"Ptr",hHash
,"Ptr",&bBuffer
,"Uint",BufferLen
,"Uint",0 )
{foo := "CryptHashData", err := GetLastError(), err2 := ErrorLevel
GoTO FINITA_DA_COMEDIA
}
}
if !dllCall("Advapi32\CryptGetHashParam","Ptr",hHash,"Uint",c.HP_HASHSIZE,"Uint*",HashLen,"Uint*",HashLenSize := 4,"UInt",0 )
{foo := "CryptGetHashParam HP_HASHSIZE", err := GetLastError(), err2 := ErrorLevel
GoTO FINITA_DA_COMEDIA
}
VarSetCapacity(pbHash,HashLen,0)
if !dllCall("Advapi32\CryptGetHashParam","Ptr",hHash,"Uint",c.HP_HASHVAL,"Ptr",&pbHash,"Uint*",HashLen,"UInt",0 )
{foo := "CryptGetHashParam HP_HASHVAL", err := GetLastError(), err2 := ErrorLevel
GoTO FINITA_DA_COMEDIA
}
hashval := b2a_hex( pbHash, HashLen )
FINITA_DA_COMEDIA:
DllCall("FreeLibrary", "Ptr", hModule)
dllCall("Advapi32\CryptDestroyHash","Ptr",hHash)
dllCall("Advapi32\CryptDestroyKey","Ptr",hKey )
dllCall("Advapi32\CryptReleaseContext","Ptr",hCryptProv,"UInt",0)
if (A_ThisLabel = "FINITA_LA_COMEDIA")
{
if (A_IsCompiled = 1)
return ""
else
msgbox % foo " call failed with:`nErrorLevel: " err2 "`nLastError: " err "`n" ErrorFormat(err)
return 0
}
return hashval
}
}
}
;returns positive hex value of last error
GetLastError(){
return ToHex(A_LastError < 0 ? A_LastError & 0xFFFFFFFF : A_LastError)
}
;converting decimal to hex value
ToHex(num){
if num is not integer
return num
oldFmt := A_FormatInteger
SetFormat, integer, hex
num := num + 0
SetFormat, integer,% oldFmt
return num
}
;And this function returns error description based on error number passed. ;
;Error number is one returned by GetLastError() or from A_LastError
ErrorFormat(error_id){
VarSetCapacity(msg,1000,0)
if !len := DllCall("FormatMessageW"
,"UInt",FORMAT_MESSAGE_FROM_SYSTEM := 0x00001000 | FORMAT_MESSAGE_IGNORE_INSERTS := 0x00000200 ;dwflags
,"Ptr",0 ;lpSource
,"UInt",error_id ;dwMessageId
,"UInt",0 ;dwLanguageId
,"Ptr",&msg ;lpBuffer
,"UInt",500) ;nSize
return
return strget(&msg,len)
}
StrPutVar(string, ByRef var, addBufLen = 0,encoding="UTF-16"){
tlen := ((encoding="utf-16"||encoding="cp1200") ? 2 : 1)
str_len := StrPut(string, encoding) * tlen
VarSetCapacity( var, str_len + addBufLen,0 )
; Copy or convert the string.
StrPut( string, &var, encoding )
return str_len - tlen
}
SetKeySalt(hKey,hProv){
KP_SALT_EX := 10
SALT := "89ABF9C1005EDD40"
;~ len := HashToByte(SALT,pb)
VarSetCapacity(st,2*A_PtrSize,0)
NumPut(len,st,0,"UInt")
NumPut(&pb,st,A_PtrSize,"UPtr")
if !dllCall("Advapi32\CryptSetKeyParam"
,"Ptr",hKey
,"Uint",KP_SALT_EX
,"Ptr",&st
,"Uint",0)
msgbox % ErrorFormat(GetLastError())
}
GetKeySalt(hKey){
KP_IV := 1 ; Initialization vector
KP_SALT := 2 ; Salt value
if !dllCall("Advapi32\CryptGetKeyParam"
,"Ptr",hKey
,"Uint",KP_SALT
,"Uint",0
,"Uint*",dwCount
,"Uint",0)
msgbox % "Fail to get SALT length."
msgbox % "SALT length.`n" dwCount
VarSetCapacity(pb,dwCount,0)
if !dllCall("Advapi32\CryptGetKeyParam"
,"Ptr",hKey
,"Uint",KP_SALT
,"Ptr",&pb
,"Uint*",dwCount
,"Uint",0)
msgbox % "Fail to get SALT"
;~ msgbox % ByteToHash(pb,dwCount) "`n" dwCount
}