├── LICENSE ├── README.md └── src ├── Class_CNG.ahk └── v1.1_deprecated ├── Class_CNG.ahk └── functions ├── bcrypt_md2.ahk ├── bcrypt_md2_file.ahk ├── bcrypt_md2_hmac.ahk ├── bcrypt_md4.ahk ├── bcrypt_md4_file.ahk ├── bcrypt_md4_hmac.ahk ├── bcrypt_md5.ahk ├── bcrypt_md5_file.ahk ├── bcrypt_md5_hmac.ahk ├── bcrypt_pbkdf2_md2.ahk ├── bcrypt_pbkdf2_md4.ahk ├── bcrypt_pbkdf2_md5.ahk ├── bcrypt_pbkdf2_sha1.ahk ├── bcrypt_pbkdf2_sha256.ahk ├── bcrypt_pbkdf2_sha384.ahk ├── bcrypt_pbkdf2_sha512.ahk ├── bcrypt_sha1.ahk ├── bcrypt_sha1_file.ahk ├── bcrypt_sha1_hmac.ahk ├── bcrypt_sha256.ahk ├── bcrypt_sha256_file.ahk ├── bcrypt_sha256_hmac.ahk ├── bcrypt_sha384.ahk ├── bcrypt_sha384_file.ahk ├── bcrypt_sha384_hmac.ahk ├── bcrypt_sha512.ahk ├── bcrypt_sha512_file.ahk └── bcrypt_sha512_hmac.ahk /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 jNizM 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # AHK implementation for CNG 2 | Cryptography API: Next Generation (CNG) is the long-term replacement for the CryptoAPI. 3 | CNG is designed to be extensible at many levels and cryptography agnostic in behavior. 4 | 5 | [![AHK](https://img.shields.io/badge/ahk-2.0--beta.3%20(x64)-C3D69B.svg?style=flat-square)]() 6 | [![OS](https://img.shields.io/badge/os-windows%2011%20(x64)-C3D69B.svg?style=flat-square)]() 7 | 8 | 9 | ## Creating a Hash with CNG 10 | 11 | ### Hash Algorithm Identifiers 12 | * MD2 13 | * MD4 14 | * MD5 15 | * SHA1 16 | * SHA256 17 | * SHA384 18 | * SHA512 19 | * PBKDF2 20 | 21 | 22 | ## Encrypt and Decrypt with CNG 23 | 24 | ### Encryption Algorithm Identifiers 25 | * AES + ECB [Key] 26 | * AES + CBC [Key + IV] 27 | * AES + CFB [Key + IV] 28 | * DES + ECB [Key] 29 | * DES + CBC [Key + IV] 30 | * RC2 [Key] 31 | * RC4 [Key] 32 | 33 | 34 | 35 | ## Examples 36 | 37 | **Create a SHA-1 Hash from String** 38 | ```AutoHotkey 39 | MsgBox Hash.String("SHA1", "The quick brown fox jumps over the lazy dog") 40 | ; -> 2fd4e1c67a2d28fced849ee1bb76e7391b93eb12 41 | ``` 42 | 43 | **Create a SHA-256 Hash with HMAC from String** 44 | ```AutoHotkey 45 | MsgBox Hash.HMAC("SHA256", "The quick brown fox jumps over the lazy dog", "Secret Salt") 46 | ; -> 68dba4b3a6d5c36b6e3567e1a925fe87c7386162e8fb6e2e9f17ade4aa7dc262 47 | ``` 48 | 49 | **Create a SHA-256 Hash from a File** 50 | ```AutoHotkey 51 | MsgBox Hash.File("SHA256", "C:\Program Files\AutoHotkey\AutoHotkey.exe") 52 | ; -> c93fde911140a7330f6d2d89bdb8e011b86153b43d64c7e2b66a741abacf9472 53 | ``` 54 | 55 | **Create a PBKDF2 Hash with SHA-1, 1500 Iterations and a Keysize of 192 from a String** 56 | ```AutoHotkey 57 | MsgBox Hash.PBKDF2("SHA1", "The quick brown fox jumps over the lazy dog", "Secret Salt", 1500, 192) 58 | ; -> 531c1bbae7c3de019d1f53adcac7d85bf2b04caba9d6d6d1 59 | ``` 60 | 61 | **Encrypt a String with AES + CBC and with Key + IV and Base64 Output** 62 | ```AutoHotkey 63 | MsgBox Encrypt.String("AES", "CBC", "abcdefghijklmnop", "1234567890123456", "1234567890123456") 64 | ; -> Nn9CFFuC+/O84cV1NiwLYoyd25Z9nmWv16dIFKzf2b4= 65 | ``` 66 | 67 | **Decrypt a String with AES + CBC and with Key + IV and Base64 Input** 68 | ```AutoHotkey 69 | MsgBox Decrypt.String("AES", "CBC", "Nn9CFFuC+/O84cV1NiwLYoyd25Z9nmWv16dIFKzf2b4=", "1234567890123456", "1234567890123456") 70 | ; -> abcdefghijklmnop 71 | ``` 72 | 73 | **Encrypt a File with AES + ECB with Key** 74 | ```AutoHotkey 75 | Encrypt.File("AES", "ECB", "test.txt", "test.enc", "1234567890123456") 76 | ``` 77 | 78 | **Decrypt a File with AES + ECB with Key** 79 | ```AutoHotkey 80 | Decrypt.File("AES", "ECB", "test.enc", "test.txt", "1234567890123456") 81 | ``` 82 | 83 | **HashCalc (Gui)** 84 | 85 | [![HashCalc](https://raw.githubusercontent.com/jNizM/HashCalc/master/img/HashCalc_01.png)](https://github.com/jNizM/HashCalc) 86 | 87 | 88 | ## Questions / Bugs / Issues 89 | Report any bugs or issues on the [AHK Thread](https://www.autohotkey.com/boards/viewtopic.php?f=6&t=96117) ([v1.1](https://www.autohotkey.com/boards/viewtopic.php?f=6&t=23413)). Same for any questions. 90 | 91 | 92 | ## Copyright and License 93 | [![MIT License](https://img.shields.io/github/license/jNizM/AHK_CNG.svg?style=flat-square&color=C3D69B)](LICENSE) 94 | 95 | 96 | ## Donations (PayPal) 97 | [![PayPal](https://img.shields.io/badge/paypal-donate-B2A2C7.svg?style=flat-square)](https://www.paypal.me/smithz) -------------------------------------------------------------------------------- /src/Class_CNG.ahk: -------------------------------------------------------------------------------- 1 | ; =========================================================================================================================================================================== 2 | 3 | /* 4 | AutoHotkey wrapper for CNG (Cryptography API: Next Generation) 5 | 6 | Author ....: jNizM 7 | Released ..: 2016-09-16 8 | Modified ..: 2021-11-03 9 | License ...: MIT 10 | GitHub ....: https://github.com/jNizM/AHK_CNG 11 | Forum .....: https://www.autohotkey.com/boards/viewtopic.php?t=96117 12 | */ 13 | 14 | ; SCRIPT DIRECTIVES ========================================================================================================================================================= 15 | 16 | #Requires AutoHotkey v2.0- 17 | 18 | 19 | ; =========================================================================================================================================================================== 20 | 21 | 22 | class Encrypt extends CNG 23 | { 24 | 25 | static String(AlgId, Mode, String, Key, IV := "", Encoding := "UTF-8", Output := "Base64") 26 | { 27 | static hAlgorithm := 0, hKey := 0, IVBuf := 0, IVSize := 0 28 | 29 | try 30 | { 31 | ; verify the encryption algorithm 32 | if !(ALGORITHM := this.BCrypt.EncryptionAlgorithm(AlgId)) 33 | throw Error("Unrecognized encryption algorithm identifier: " AlgId, -1) 34 | 35 | ; open an algorithm handle 36 | hAlgorithm := this.BCrypt.OpenAlgorithmProvider(ALGORITHM) 37 | 38 | ; set chaining mode 39 | if (CHAIN_MODE := this.BCrypt.ChainingMode(Mode)) 40 | this.BCrypt.SetProperty(hAlgorithm, this.BCrypt.Constants.BCRYPT_CHAINING_MODE, CHAIN_MODE) 41 | 42 | ; generate the key from supplied input key bytes 43 | KeyBuf := this.StrBuf(Key, Encoding) 44 | hKey := this.BCrypt.GenerateSymmetricKey(hAlgorithm, KeyBuf, KeyBuf.Size - 1) 45 | 46 | ; calculate the block length for the IV 47 | BLOCK_LENGTH := this.BCrypt.GetProperty(hAlgorithm, this.BCrypt.Constants.BCRYPT_BLOCK_LENGTH, 4) 48 | 49 | ; use the key to encrypt the plaintext buffer. for block sized messages, block padding will add an extra block 50 | DataBuf := this.StrBuf(String, Encoding) 51 | if (IV) { 52 | IVBuf := Buffer(BLOCK_LENGTH, 0) 53 | StrPut(IV, IVBuf, BLOCK_LENGTH, Encoding) 54 | IVSize := IVBuf.Size 55 | } 56 | CIPHER_LENGTH := this.BCrypt.Encrypt(hKey, DataBuf, DataBuf.Size - 1, IVBuf, IVSize, &CIPHER_DATA, this.BCrypt.Constants.BCRYPT_BLOCK_PADDING) 57 | 58 | ; convert binary data to string (base64 / hex / hexraw) 59 | ENCRYPT := this.Crypt.BinaryToString(CIPHER_DATA, CIPHER_LENGTH, Output) 60 | } 61 | catch as Exception 62 | { 63 | ; represents errors that occur during application execution 64 | throw Exception 65 | } 66 | finally 67 | { 68 | ; cleaning up resources 69 | if (hKey) 70 | this.BCrypt.DestroyKey(hKey) 71 | 72 | if (hAlgorithm) 73 | this.BCrypt.CloseAlgorithmProvider(hAlgorithm) 74 | } 75 | 76 | return ENCRYPT 77 | } 78 | 79 | 80 | static File(AlgId, Mode, FileNameIn, FileNameOut, Key, IV := "", Bytes := 1048576, Offset := 0, Length := -1, Encoding := "UTF-8") 81 | { 82 | static hAlgorithm := 0, hKey := 0, IVBuf := 0, IVSize := 0 83 | 84 | try 85 | { 86 | ; verify the encryption algorithm 87 | if !(ALGORITHM := this.BCrypt.EncryptionAlgorithm(AlgId)) 88 | throw Error("Unrecognized encryption algorithm identifier: " AlgId, -1) 89 | 90 | ; open an algorithm handle 91 | hAlgorithm := this.BCrypt.OpenAlgorithmProvider(ALGORITHM) 92 | 93 | ; set chaining mode 94 | if (CHAIN_MODE := this.BCrypt.ChainingMode(Mode)) 95 | this.BCrypt.SetProperty(hAlgorithm, this.BCrypt.Constants.BCRYPT_CHAINING_MODE, CHAIN_MODE) 96 | 97 | ; generate the key from supplied input key bytes 98 | KeyBuf := this.StrBuf(Key, Encoding) 99 | hKey := this.BCrypt.GenerateSymmetricKey(hAlgorithm, KeyBuf, KeyBuf.Size - 1) 100 | 101 | ; calculate the block length for the IV 102 | BLOCK_LENGTH := this.BCrypt.GetProperty(hAlgorithm, this.BCrypt.Constants.BCRYPT_BLOCK_LENGTH, 4) 103 | 104 | ; use the key to encrypt the plaintext buffer. for block sized messages, block padding will add an extra block 105 | if (IV) { 106 | IVBuf := Buffer(BLOCK_LENGTH, 0) 107 | StrPut(IV, IVBuf, BLOCK_LENGTH, Encoding) 108 | IVSize := IVBuf.Size 109 | } 110 | 111 | if !(FileIn := FileOpen(FileNameIn, "r", Encoding)) 112 | throw Error("Failed to open file: " FileNameIn, -1) 113 | if !(FileOut := FileOpen(FileNameOut, "rw", Encoding)) 114 | throw Error("Failed to create file: " FileNameOut, -1) 115 | 116 | FileOut.Seek(0) 117 | Length := Length < 0 ? FileIn.Length - Offset : Length 118 | Data := Buffer(Bytes) 119 | if ((Offset + Length) > FileIn.Length) 120 | throw Error("Invalid parameters offset / length!", -1) 121 | while (Length > Bytes) && (Dataread := FileIn.RawRead(Data, Bytes)) 122 | { 123 | CIPHER_LENGTH := this.BCrypt.Encrypt(hKey, Data, Dataread, IVBuf, IVSize, &CIPHER_DATA, this.BCrypt.Constants.BCRYPT_BLOCK_PADDING) 124 | FileOut.RawWrite(CIPHER_DATA, CIPHER_LENGTH) 125 | Length -= Dataread 126 | } 127 | if (Length > 0) 128 | { 129 | if (Dataread := FileIn.RawRead(Data, Length)) 130 | { 131 | CIPHER_LENGTH := this.BCrypt.Encrypt(hKey, Data, Dataread, IVBuf, IVSize, &CIPHER_DATA, this.BCrypt.Constants.BCRYPT_BLOCK_PADDING) 132 | FileOut.RawWrite(CIPHER_DATA, CIPHER_LENGTH) 133 | } 134 | } 135 | } 136 | catch as Exception 137 | { 138 | ; represents errors that occur during application execution 139 | throw Exception 140 | } 141 | finally 142 | { 143 | ; cleaning up resources 144 | if (FileIn) 145 | FileIn.Close() 146 | 147 | if (FileOut) 148 | FileOut.Close() 149 | 150 | if (hKey) 151 | this.BCrypt.DestroyKey(hKey) 152 | 153 | if (hAlgorithm) 154 | this.BCrypt.CloseAlgorithmProvider(hAlgorithm) 155 | } 156 | 157 | return true 158 | } 159 | } 160 | 161 | 162 | ; =========================================================================================================================================================================== 163 | 164 | 165 | class Decrypt extends CNG 166 | { 167 | 168 | static String(AlgId, Mode, String, Key, IV := "", Encoding := "UTF-8", Input := "Base64") 169 | { 170 | static hAlgorithm := 0, hKey := 0, IVBuf := 0, IVSize := 0 171 | 172 | try 173 | { 174 | ; verify the encryption algorithm 175 | if !(ALGORITHM := this.BCrypt.EncryptionAlgorithm(AlgId)) 176 | throw Error("Unrecognized encryption algorithm identifier: " AlgId, -1) 177 | 178 | ; open an algorithm handle 179 | hAlgorithm := this.BCrypt.OpenAlgorithmProvider(ALGORITHM) 180 | 181 | ; set chaining mode 182 | if (CHAIN_MODE := this.BCrypt.ChainingMode(Mode)) 183 | this.BCrypt.SetProperty(hAlgorithm, this.BCrypt.Constants.BCRYPT_CHAINING_MODE, CHAIN_MODE) 184 | 185 | ; generate the key from supplied input key bytes 186 | KeyBuf := this.StrBuf(Key, Encoding) 187 | hKey := this.BCrypt.GenerateSymmetricKey(hAlgorithm, KeyBuf, KeyBuf.Size - 1) 188 | 189 | ; convert encrypted string (base64 / hex / hexraw) to binary data 190 | CIPHER_LENGTH := this.Crypt.StringToBinary(String, &CIPHER_DATA, Input) 191 | 192 | ; calculate the block length for the IV 193 | BLOCK_LENGTH := this.BCrypt.GetProperty(hAlgorithm, this.BCrypt.Constants.BCRYPT_BLOCK_LENGTH, 4) 194 | 195 | ; use the key to encrypt the plaintext buffer. for block sized messages, block padding will add an extra block 196 | if (IV) { 197 | IVBuf := Buffer(BLOCK_LENGTH, 0) 198 | StrPut(IV, IVBuf, BLOCK_LENGTH, Encoding) 199 | IVSize := IVBuf.Size 200 | } 201 | DECRYPT_LENGTH := this.BCrypt.Decrypt(hKey, CIPHER_DATA, CIPHER_LENGTH, IVBuf, IVSize, &DECRYPT_DATA, this.BCrypt.Constants.BCRYPT_BLOCK_PADDING) 202 | 203 | ; receive the decrypted plaintext 204 | DECRYPT := StrGet(DECRYPT_DATA, DECRYPT_LENGTH, Encoding) 205 | } 206 | catch as Exception 207 | { 208 | ; represents errors that occur during application execution 209 | throw Exception 210 | } 211 | finally 212 | { 213 | ; cleaning up resources 214 | if (hKey) 215 | this.BCrypt.DestroyKey(hKey) 216 | 217 | if (hAlgorithm) 218 | this.BCrypt.CloseAlgorithmProvider(hAlgorithm) 219 | } 220 | 221 | return DECRYPT 222 | } 223 | 224 | 225 | static File(AlgId, Mode, FileNameIn, FileNameOut, Key, IV := "", Bytes := 1048576, Offset := 0, Length := -1, Encoding := "UTF-8") 226 | { 227 | static hAlgorithm := 0, hKey := 0, IVBuf := 0, IVSize := 0 228 | 229 | try 230 | { 231 | ; verify the encryption algorithm 232 | if !(ALGORITHM := this.BCrypt.EncryptionAlgorithm(AlgId)) 233 | throw Error("Unrecognized encryption algorithm identifier: " AlgId, -1) 234 | 235 | ; open an algorithm handle 236 | hAlgorithm := this.BCrypt.OpenAlgorithmProvider(ALGORITHM) 237 | 238 | ; set chaining mode 239 | if (CHAIN_MODE := this.BCrypt.ChainingMode(Mode)) 240 | this.BCrypt.SetProperty(hAlgorithm, this.BCrypt.Constants.BCRYPT_CHAINING_MODE, CHAIN_MODE) 241 | 242 | ; generate the key from supplied input key bytes 243 | KeyBuf := this.StrBuf(Key, Encoding) 244 | hKey := this.BCrypt.GenerateSymmetricKey(hAlgorithm, KeyBuf, KeyBuf.Size - 1) 245 | 246 | ; calculate the block length for the IV 247 | BLOCK_LENGTH := this.BCrypt.GetProperty(hAlgorithm, this.BCrypt.Constants.BCRYPT_BLOCK_LENGTH, 4) 248 | 249 | ; use the key to encrypt the plaintext buffer. for block sized messages, block padding will add an extra block 250 | if (IV) { 251 | IVBuf := Buffer(BLOCK_LENGTH, 0) 252 | StrPut(IV, IVBuf, BLOCK_LENGTH, Encoding) 253 | IVSize := IVBuf.Size 254 | } 255 | 256 | if !(FileIn := FileOpen(FileNameIn, "r", Encoding)) 257 | throw Error("Failed to open file: " FileNameIn, -1) 258 | if !(FileOut := FileOpen(FileNameOut, "rw", Encoding)) 259 | throw Error("Failed to create file: " FileNameOut, -1) 260 | 261 | FileOut.Seek(0) 262 | Length := Length < 0 ? FileIn.Length - Offset : Length 263 | Data := Buffer(Bytes) 264 | if ((Offset + Length) > FileIn.Length) 265 | throw Error("Invalid parameters offset / length!", -1) 266 | while (Length > Bytes) && (Dataread := FileIn.RawRead(Data, Bytes)) 267 | { 268 | DECRYPT_LENGTH := this.BCrypt.Decrypt(hKey, Data, Dataread, IVBuf, IVSize, &DECRYPT_DATA, this.BCrypt.Constants.BCRYPT_BLOCK_PADDING) 269 | DECRYPT := StrGet(DECRYPT_DATA, DECRYPT_LENGTH, Encoding) 270 | FileOut.Write(DECRYPT) 271 | Length -= Dataread 272 | } 273 | if (Length > 0) 274 | { 275 | if (Dataread := FileIn.RawRead(Data, Length)) 276 | { 277 | DECRYPT_LENGTH := this.BCrypt.Decrypt(hKey, Data, Length, IVBuf, IVSize, &DECRYPT_DATA, this.BCrypt.Constants.BCRYPT_BLOCK_PADDING) 278 | DECRYPT := StrGet(DECRYPT_DATA, DECRYPT_LENGTH, Encoding) 279 | FileOut.Write(DECRYPT) 280 | } 281 | } 282 | } 283 | catch as Exception 284 | { 285 | ; represents errors that occur during application execution 286 | throw Exception 287 | } 288 | finally 289 | { 290 | ; cleaning up resources 291 | if (FileIn) 292 | FileIn.Close() 293 | 294 | if (FileOut) 295 | FileOut.Close() 296 | 297 | if (hKey) 298 | this.BCrypt.DestroyKey(hKey) 299 | 300 | if (hAlgorithm) 301 | this.BCrypt.CloseAlgorithmProvider(hAlgorithm) 302 | } 303 | 304 | return true 305 | } 306 | } 307 | 308 | 309 | ; =========================================================================================================================================================================== 310 | 311 | 312 | class Hash extends CNG 313 | { 314 | 315 | static String(AlgId, String, Encoding := "UTF-8", Output := "HEXRAW") 316 | { 317 | static hAlgorithm := 0, hHash := 0 318 | 319 | try 320 | { 321 | ; verify the hash algorithm identifier 322 | if !(ALGORITHM := this.BCrypt.HashAlgorithm(AlgId)) 323 | throw Error("Unrecognized hash algorithm identifier: " AlgId, -1) 324 | 325 | ; open an algorithm handle 326 | hAlgorithm := this.BCrypt.OpenAlgorithmProvider(ALGORITHM) 327 | 328 | ; create a hash 329 | hHash := this.BCrypt.CreateHash(hAlgorithm) 330 | 331 | ; hash some data 332 | Data := this.StrBuf(String, Encoding) 333 | this.BCrypt.HashData(hHash, Data, Data.Size - 1) 334 | 335 | ; calculate the length of the hash 336 | HASH_LENGTH := this.BCrypt.GetProperty(hAlgorithm, this.BCrypt.Constants.BCRYPT_HASH_LENGTH, 4) 337 | 338 | ; close the hash 339 | HASH_DATA := Buffer(HASH_LENGTH, 0) 340 | FINISH_HASH := this.BCrypt.FinishHash(hHash, &HASH_DATA, HASH_LENGTH) 341 | 342 | ; convert bin to string (base64 / hex) 343 | HASH := this.Crypt.BinaryToString(HASH_DATA, HASH_LENGTH, Output) 344 | } 345 | catch as Exception 346 | { 347 | ; represents errors that occur during application execution 348 | throw Exception 349 | } 350 | finally 351 | { 352 | ; cleaning up resources 353 | if (hHash) 354 | this.BCrypt.DestroyHash(hHash) 355 | 356 | if (hAlgorithm) 357 | this.BCrypt.CloseAlgorithmProvider(hAlgorithm) 358 | } 359 | 360 | return HASH 361 | } 362 | 363 | 364 | static File(AlgId, FileName, Bytes := 1048576, Offset := 0, Length := -1, Encoding := "UTF-8", Output := "HEXRAW") 365 | { 366 | static hAlgorithm := 0, hHash := 0, File := 0 367 | 368 | try 369 | { 370 | ; verify the hash algorithm identifier 371 | if !(ALGORITHM := this.BCrypt.HashAlgorithm(AlgId)) 372 | throw Error("Unrecognized hash algorithm identifier: " AlgId, -1) 373 | 374 | ; open an algorithm handle 375 | hAlgorithm := this.BCrypt.OpenAlgorithmProvider(ALGORITHM) 376 | 377 | ; create a hash 378 | hHash := this.BCrypt.CreateHash(hAlgorithm) 379 | 380 | ; hash some data 381 | if !(File := FileOpen(FileName, "r", Encoding)) 382 | throw Error("Failed to open file: " FileName, -1) 383 | Length := Length < 0 ? File.Length - Offset : Length 384 | Data := Buffer(Bytes) 385 | if ((Offset + Length) > File.Length) 386 | throw Error("Invalid parameters offset / length!", -1) 387 | while (Length > Bytes) && (Dataread := File.RawRead(Data, Bytes)) { 388 | this.BCrypt.HashData(hHash, Data, Dataread) 389 | Length -= Dataread 390 | } 391 | if (Length > 0) { 392 | if (Dataread := File.RawRead(Data, Length)) 393 | this.BCrypt.HashData(hHash, Data, Dataread) 394 | } 395 | 396 | ; calculate the length of the hash 397 | HASH_LENGTH := this.BCrypt.GetProperty(hAlgorithm, this.BCrypt.Constants.BCRYPT_HASH_LENGTH, 4) 398 | 399 | ; close the hash 400 | HASH_DATA := Buffer(HASH_LENGTH, 0) 401 | FINISH_HASH := this.BCrypt.FinishHash(hHash, &HASH_DATA, HASH_LENGTH) 402 | 403 | ; convert bin to string (base64 / hex) 404 | HASH := this.Crypt.BinaryToString(HASH_DATA, HASH_LENGTH, Output) 405 | } 406 | catch as Exception 407 | { 408 | ; represents errors that occur during application execution 409 | throw Exception 410 | } 411 | finally 412 | { 413 | ; cleaning up resources 414 | if (File) 415 | File.Close() 416 | 417 | if (hHash) 418 | this.BCrypt.DestroyHash(hHash) 419 | 420 | if (hAlgorithm) 421 | this.BCrypt.CloseAlgorithmProvider(hAlgorithm) 422 | } 423 | 424 | return HASH 425 | } 426 | 427 | 428 | 429 | static HMAC(AlgId, String, Hmac, Encoding := "UTF-8", Output := "HEXRAW") 430 | { 431 | static hAlgorithm := 0, hHash := 0 432 | 433 | try 434 | { 435 | ; verify the hash algorithm identifier 436 | if !(ALGORITHM := this.BCrypt.HashAlgorithm(AlgId)) 437 | throw Error("Unrecognized hash algorithm identifier: " AlgId, -1) 438 | 439 | ; open an algorithm handle 440 | hAlgorithm := this.BCrypt.OpenAlgorithmProvider(ALGORITHM, this.BCrypt.Constants.BCRYPT_ALG_HANDLE_HMAC_FLAG) 441 | 442 | ; create a hash 443 | Mac := this.StrBuf(Hmac, Encoding) 444 | hHash := this.BCrypt.CreateHash(hAlgorithm, Mac, Mac.Size - 1) 445 | 446 | ; hash some data 447 | Data := this.StrBuf(String, Encoding) 448 | this.BCrypt.HashData(hHash, Data, Data.Size - 1) 449 | 450 | ; calculate the length of the hash 451 | HASH_LENGTH := this.BCrypt.GetProperty(hAlgorithm, this.BCrypt.Constants.BCRYPT_HASH_LENGTH, 4) 452 | 453 | ; close the hash 454 | HASH_DATA := Buffer(HASH_LENGTH, 0) 455 | FINISH_HASH := this.BCrypt.FinishHash(hHash, &HASH_DATA, HASH_LENGTH) 456 | 457 | ; convert bin to string (base64 / hex) 458 | HASH := this.Crypt.BinaryToString(HASH_DATA, HASH_LENGTH, Output) 459 | } 460 | catch as Exception 461 | { 462 | ; represents errors that occur during application execution 463 | throw Exception 464 | } 465 | finally 466 | { 467 | ; cleaning up resources 468 | if (hHash) 469 | this.BCrypt.DestroyHash(hHash) 470 | 471 | if (hAlgorithm) 472 | this.BCrypt.CloseAlgorithmProvider(hAlgorithm) 473 | } 474 | 475 | return HASH 476 | } 477 | 478 | 479 | 480 | static PBKDF2(AlgId, Password, Salt, Iterations := 4096, KeySize := 256, Encoding := "UTF-8", Output := "HEXRAW") 481 | { 482 | static hAlgorithm := 0, hHash := 0 483 | 484 | try 485 | { 486 | ; verify the hash algorithm identifier 487 | if !(ALGORITHM := this.BCrypt.HashAlgorithm(AlgId)) 488 | throw Error("Unrecognized hash algorithm identifier: " AlgId, -1) 489 | 490 | ; check key bit length 491 | if (Mod(KeySize, 8) != 0) 492 | throw Error("The desired key bit length must be a multiple of 8!", -1) 493 | 494 | ; open an algorithm handle 495 | hAlgorithm := this.BCrypt.OpenAlgorithmProvider(ALGORITHM, this.BCrypt.Constants.BCRYPT_ALG_HANDLE_HMAC_FLAG) 496 | 497 | ; derives a key from a hash value 498 | PBKDF2_DATA := this.BCrypt.DeriveKeyPBKDF2(hAlgorithm, Password, Salt, Iterations, KeySize / 8, Encoding) 499 | 500 | ; convert bin to string (base64 / hex) 501 | PBKDF2 := this.Crypt.BinaryToString(PBKDF2_DATA, PBKDF2_DATA.size, Output) 502 | } 503 | catch as Exception 504 | { 505 | ; represents errors that occur during application execution 506 | throw Exception 507 | } 508 | finally 509 | { 510 | ; cleaning up resources 511 | if (hAlgorithm) 512 | this.BCrypt.CloseAlgorithmProvider(hAlgorithm) 513 | } 514 | 515 | return PBKDF2 516 | } 517 | } 518 | 519 | 520 | ; =========================================================================================================================================================================== 521 | 522 | 523 | class CNG 524 | { 525 | 526 | class BCrypt 527 | { 528 | 529 | #DllLoad "*i bcrypt.dll" 530 | 531 | 532 | ; //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 533 | ; // 534 | ; // FUNCTION NAME: BCrypt.CloseAlgorithmProvider 535 | ; // 536 | ; // This function closes an algorithm provider. 537 | ; // 538 | ; //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 539 | static CloseAlgorithmProvider(hAlgorithm) 540 | { 541 | NT_STATUS := DllCall("bcrypt\BCryptCloseAlgorithmProvider", "Ptr", hAlgorithm 542 | , "UInt", Flags := 0 543 | , "UInt") 544 | 545 | if (NT_STATUS = this.NT.SUCCESS) 546 | return true 547 | throw Error(this.GetErrorMessage(NT_STATUS), -1) 548 | } 549 | 550 | 551 | ; //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 552 | ; // 553 | ; // FUNCTION NAME: BCrypt.CreateHash 554 | ; // 555 | ; // This function is called to create a hash or Message Authentication Code (MAC) object. 556 | ; // 557 | ; //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 558 | static CreateHash(hAlgorithm, Buf := 0, Size := 0) 559 | { 560 | NT_STATUS := DllCall("bcrypt\BCryptCreateHash", "Ptr", hAlgorithm 561 | , "Ptr*", &hHash := 0 562 | , "Ptr", 0 563 | , "UInt", 0 564 | , "Ptr", Buf 565 | , "UInt", Size 566 | , "UInt", Flags := 0 567 | , "UInt") 568 | 569 | if (NT_STATUS = this.NT.SUCCESS) 570 | return hHash 571 | throw Error(this.GetErrorMessage(NT_STATUS), -1) 572 | } 573 | 574 | 575 | ; //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 576 | ; // 577 | ; // FUNCTION NAME: BCrypt.Decrypt 578 | ; // 579 | ; // This function decrypts a block of data. 580 | ; // 581 | ; //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 582 | static Decrypt(hKey, InputBuf, InputSize, IVBuf := 0, IVSize := 0, &OutputBuf := 0, Flags := 0) 583 | { 584 | NT_STATUS := DllCall("bcrypt\BCryptDecrypt", "Ptr", hKey 585 | , "Ptr", InputBuf 586 | , "UInt", InputSize 587 | , "Ptr", 0 588 | , "Ptr", IVBuf 589 | , "UInt", IVSize 590 | , "Ptr", 0 591 | , "UInt", 0 592 | , "UInt*", &Result := 0 593 | , "UInt", Flags 594 | , "UInt") 595 | 596 | if (NT_STATUS != this.NT.SUCCESS) 597 | throw Error(this.GetErrorMessage(NT_STATUS), -1) 598 | 599 | OutputBuf := Buffer(Result, 0) 600 | NT_STATUS := DllCall("bcrypt\BCryptDecrypt", "Ptr", hKey 601 | , "Ptr", InputBuf 602 | , "UInt", InputSize 603 | , "Ptr", 0 604 | , "Ptr", IVBuf 605 | , "UInt", IVSize 606 | , "Ptr", OutputBuf 607 | , "UInt", OutputBuf.Size 608 | , "UInt*", &Result := 0 609 | , "UInt", Flags 610 | , "UInt") 611 | 612 | if (NT_STATUS = this.NT.SUCCESS) 613 | return OutputBuf.Size 614 | throw Error(this.GetErrorMessage(NT_STATUS), -1) 615 | } 616 | 617 | 618 | ; //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 619 | ; // 620 | ; // FUNCTION NAME: BCrypt.DeriveKeyPBKDF2 621 | ; // 622 | ; // This function derives a key from a hash value by using the PBKDF2 key derivation algorithm as defined by RFC 2898. 623 | ; // 624 | ; //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 625 | static DeriveKeyPBKDF2(hAlgorithm, Pass, Salt, Iterations, DerivedKey, Encoding := "UTF-8") 626 | { 627 | Passwd := CNG.StrBuf(Pass, Encoding) 628 | Salt := CNG.StrBuf(Salt, Encoding) 629 | DKey := Buffer(DerivedKey, 0) 630 | 631 | NT_STATUS := DllCall("bcrypt\BCryptDeriveKeyPBKDF2", "Ptr", hAlgorithm 632 | , "Ptr", Passwd 633 | , "UInt", Passwd.Size - 1 634 | , "Ptr", Salt 635 | , "UInt", Salt.Size - 1 636 | , "Int64", Iterations 637 | , "Ptr", DKey 638 | , "UInt", DerivedKey 639 | , "UInt", Flags := 0 640 | , "UInt") 641 | 642 | if (NT_STATUS = this.NT.SUCCESS) 643 | return DKey 644 | throw Error(this.GetErrorMessage(NT_STATUS), -1) 645 | } 646 | 647 | 648 | ; //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 649 | ; // 650 | ; // FUNCTION NAME: BCrypt.DestroyHash 651 | ; // 652 | ; // This function destroys a hash or Message Authentication Code (MAC) object. 653 | ; // 654 | ; //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 655 | static DestroyHash(hHash) 656 | { 657 | NT_STATUS := DllCall("bcrypt\BCryptDestroyHash", "Ptr", hHash, "UInt") 658 | 659 | if (NT_STATUS = this.NT.SUCCESS) 660 | return true 661 | throw Error(this.GetErrorMessage(NT_STATUS), -1) 662 | } 663 | 664 | 665 | ; //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 666 | ; // 667 | ; // FUNCTION NAME: BCrypt.DestroyKey 668 | ; // 669 | ; // This function destroys a key. 670 | ; // 671 | ; //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 672 | static DestroyKey(hKey) 673 | { 674 | NT_STATUS := DllCall("bcrypt\BCryptDestroyKey", "Ptr", hKey, "UInt") 675 | 676 | if (NT_STATUS = this.NT.SUCCESS) 677 | return true 678 | throw Error(this.GetErrorMessage(NT_STATUS), -1) 679 | } 680 | 681 | 682 | ; //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 683 | ; // 684 | ; // FUNCTION NAME: BCrypt.Encrypt 685 | ; // 686 | ; // This function encrypts a block of data. 687 | ; // 688 | ; //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 689 | static Encrypt(hKey, InputBuf, InputSize, IVBuf := 0, IVSize := 0, &OutputBuf := 0, Flags := 0) 690 | { 691 | NT_STATUS := DllCall("bcrypt\BCryptEncrypt", "Ptr", hKey 692 | , "Ptr", InputBuf 693 | , "UInt", InputSize 694 | , "Ptr", 0 695 | , "Ptr", IVBuf 696 | , "UInt", IVSize 697 | , "Ptr", 0 698 | , "UInt", 0 699 | , "UInt*", &Result := 0 700 | , "UInt", Flags 701 | , "UInt") 702 | 703 | if (NT_STATUS != this.NT.SUCCESS) 704 | throw Error(this.GetErrorMessage(NT_STATUS), -1) 705 | 706 | OutputBuf := Buffer(Result, 0) 707 | NT_STATUS := DllCall("bcrypt\BCryptEncrypt", "Ptr", hKey 708 | , "Ptr", InputBuf 709 | , "UInt", InputSize 710 | , "Ptr", 0 711 | , "Ptr", IVBuf 712 | , "UInt", IVSize 713 | , "Ptr", OutputBuf 714 | , "UInt", OutputBuf.Size 715 | , "UInt*", &Result := 0 716 | , "UInt", Flags 717 | , "UInt") 718 | 719 | if (NT_STATUS = this.NT.SUCCESS) 720 | return OutputBuf.Size 721 | throw Error(this.GetErrorMessage(NT_STATUS), -1) 722 | } 723 | 724 | 725 | ; //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 726 | ; // 727 | ; // FUNCTION NAME: BCrypt.FinishHash 728 | ; // 729 | ; // This function retrieves the hash or Message Authentication Code (MAC) value for the data accumulated from prior calls to BCrypt.HashData. 730 | ; // 731 | ; //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 732 | static FinishHash(hHash, &Buf, Size) 733 | { 734 | Buf := Buffer(Size, 0) 735 | NT_STATUS := DllCall("bcrypt\BCryptFinishHash", "Ptr", hHash 736 | , "Ptr", Buf 737 | , "UInt", Size 738 | , "UInt", Flags := 0 739 | , "UInt") 740 | 741 | if (NT_STATUS = this.NT.SUCCESS) 742 | return Size 743 | throw Error(this.GetErrorMessage(NT_STATUS), -1) 744 | } 745 | 746 | 747 | ; //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 748 | ; // 749 | ; // FUNCTION NAME: BCrypt.GenerateSymmetricKey 750 | ; // 751 | ; // This function creates a key object for use with a symmetrical key encryption algorithm from a supplied key. 752 | ; // 753 | ; //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 754 | static GenerateSymmetricKey(hAlgorithm, Buf := 0, Size := 0) 755 | { 756 | NT_STATUS := DllCall("bcrypt\BCryptGenerateSymmetricKey", "Ptr", hAlgorithm 757 | , "Ptr*", &hKey := 0 758 | , "Ptr", 0 759 | , "UInt", 0 760 | , "Ptr", Buf 761 | , "UInt", Size 762 | , "UInt", Flags := 0 763 | , "UInt") 764 | 765 | if (NT_STATUS = this.NT.SUCCESS) 766 | return hKey 767 | throw Error(this.GetErrorMessage(NT_STATUS), -1) 768 | } 769 | 770 | 771 | ; //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 772 | ; // 773 | ; // FUNCTION NAME: BCrypt.GetProperty 774 | ; // 775 | ; // This function retrieves the value of a named property for a CNG object. 776 | ; // 777 | ; //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 778 | static GetProperty(hObject, Property, Size) 779 | { 780 | NT_STATUS := DllCall("bcrypt\BCryptGetProperty", "Ptr", hObject 781 | , "Ptr", StrPtr(Property) 782 | , "Ptr*", &Buf := 0 783 | , "UInt", Size 784 | , "UInt*", &Result := 0 785 | , "UInt", Flags := 0 786 | , "UInt") 787 | 788 | if (NT_STATUS = this.NT.SUCCESS) 789 | return Buf 790 | throw Error(this.GetErrorMessage(NT_STATUS), -1) 791 | } 792 | 793 | 794 | ; //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 795 | ; // 796 | ; // FUNCTION NAME: BCrypt.HashData 797 | ; // 798 | ; // This function performs a one way hash or Message Authentication Code (MAC) on a data buffer. 799 | ; // 800 | ; //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 801 | static HashData(hHash, Buf, Size) 802 | { 803 | NT_STATUS := DllCall("bcrypt\BCryptHashData", "Ptr", hHash 804 | , "Ptr", Buf 805 | , "UInt", Size 806 | , "UInt", Flags := 0 807 | , "UInt") 808 | 809 | if (NT_STATUS = this.NT.SUCCESS) 810 | return true 811 | throw Error(this.GetErrorMessage(NT_STATUS), -1) 812 | } 813 | 814 | 815 | ; //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 816 | ; // 817 | ; // FUNCTION NAME: BCrypt.OpenAlgorithmProvider 818 | ; // 819 | ; // This function loads and initializes a CNG provider. 820 | ; // 821 | ; //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 822 | static OpenAlgorithmProvider(AlgId, Flags := 0) 823 | { 824 | NT_STATUS := DllCall("bcrypt\BCryptOpenAlgorithmProvider", "Ptr*", &hAlgorithm := 0 825 | , "Str", AlgId 826 | , "Ptr", Implementation := 0 827 | , "UInt", Flags 828 | , "UInt") 829 | 830 | if (NT_STATUS = this.NT.SUCCESS) 831 | return hAlgorithm 832 | throw Error(this.GetErrorMessage(NT_STATUS), -1) 833 | } 834 | 835 | 836 | ; //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 837 | ; // 838 | ; // FUNCTION NAME: BCrypt.SetProperty 839 | ; // 840 | ; // This function sets the value of a named property for a CNG object. 841 | ; // 842 | ; //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 843 | static SetProperty(hObject, Property, Input) 844 | { 845 | NT_STATUS := DllCall("bcrypt\BCryptSetProperty", "Ptr", hObject 846 | , "Ptr", StrPtr(Property) 847 | , "Ptr", StrPtr(Input) 848 | , "UInt", StrLen(Input) 849 | , "UInt", Flags := 0 850 | , "UInt") 851 | 852 | if (NT_STATUS = this.NT.SUCCESS) 853 | return true 854 | throw Error(this.GetErrorMessage(NT_STATUS), -1) 855 | } 856 | 857 | 858 | static GetErrorMessage(STATUS_CODE) 859 | { 860 | switch STATUS_CODE 861 | { 862 | case this.NT.AUTH_TAG_MISMATCH: 863 | return "The computed authentication tag did not match the input authentication tag." 864 | case this.NT.BUFFER_TOO_SMALL: 865 | return "The buffer is too small to contain the entry. No information has been written to the buffer." 866 | case this.NT.INVALID_BUFFER_SIZE: 867 | return "The size of the buffer is invalid for the specified operation." 868 | case this.NT.INVALID_HANDLE: 869 | return "An invalid HANDLE was specified." 870 | case this.NT.INVALID_PARAMETER: 871 | return "An invalid parameter was passed to a service or function." 872 | case this.NT.NOT_FOUND: 873 | return "The object was not found." 874 | case this.NT.NOT_SUPPORTED: 875 | return "The request is not supported." 876 | case this.NT.NO_MEMORY: 877 | return "Not enough virtual memory or paging file quota is available to complete the specified operation." 878 | default: 879 | return "BCrypt failed " STATUS_CODE 880 | } 881 | } 882 | 883 | 884 | class Constants 885 | { 886 | static BCRYPT_ALG_HANDLE_HMAC_FLAG := 0x00000008 887 | static BCRYPT_HASH_REUSABLE_FLAG := 0x00000020 888 | static BCRYPT_BLOCK_PADDING := 0x00000001 889 | 890 | 891 | ; AlgOperations flags for use with BCryptEnumAlgorithms() 892 | static BCRYPT_CIPHER_OPERATION := 0x00000001 893 | static BCRYPT_HASH_OPERATION := 0x00000002 894 | static BCRYPT_ASYMMETRIC_ENCRYPTION_OPERATION := 0x00000004 895 | static BCRYPT_SECRET_AGREEMENT_OPERATION := 0x00000008 896 | static BCRYPT_SIGNATURE_OPERATION := 0x00000010 897 | static BCRYPT_RNG_OPERATION := 0x00000020 898 | static BCRYPT_KEY_DERIVATION_OPERATION := 0x00000040 899 | 900 | 901 | ; https://docs.microsoft.com/en-us/windows/win32/seccng/cng-algorithm-identifiers 902 | static BCRYPT_3DES_ALGORITHM := "3DES" 903 | static BCRYPT_3DES_112_ALGORITHM := "3DES_112" 904 | static BCRYPT_AES_ALGORITHM := "AES" 905 | static BCRYPT_AES_CMAC_ALGORITHM := "AES-CMAC" 906 | static BCRYPT_AES_GMAC_ALGORITHM := "AES-GMAC" 907 | static BCRYPT_DES_ALGORITHM := "DES" 908 | static BCRYPT_DESX_ALGORITHM := "DESX" 909 | static BCRYPT_MD2_ALGORITHM := "MD2" 910 | static BCRYPT_MD4_ALGORITHM := "MD4" 911 | static BCRYPT_MD5_ALGORITHM := "MD5" 912 | static BCRYPT_RC2_ALGORITHM := "RC2" 913 | static BCRYPT_RC4_ALGORITHM := "RC4" 914 | static BCRYPT_RNG_ALGORITHM := "RNG" 915 | static BCRYPT_SHA1_ALGORITHM := "SHA1" 916 | static BCRYPT_SHA256_ALGORITHM := "SHA256" 917 | static BCRYPT_SHA384_ALGORITHM := "SHA384" 918 | static BCRYPT_SHA512_ALGORITHM := "SHA512" 919 | static BCRYPT_PBKDF2_ALGORITHM := "PBKDF2" 920 | static BCRYPT_XTS_AES_ALGORITHM := "XTS-AES" 921 | 922 | 923 | ; https://docs.microsoft.com/en-us/windows/win32/seccng/cng-property-identifiers 924 | static BCRYPT_BLOCK_LENGTH := "BlockLength" 925 | static BCRYPT_CHAINING_MODE := "ChainingMode" 926 | static BCRYPT_CHAIN_MODE_CBC := "ChainingModeCBC" 927 | static BCRYPT_CHAIN_MODE_CCM := "ChainingModeCCM" 928 | static BCRYPT_CHAIN_MODE_CFB := "ChainingModeCFB" 929 | static BCRYPT_CHAIN_MODE_ECB := "ChainingModeECB" 930 | static BCRYPT_CHAIN_MODE_GCM := "ChainingModeGCM" 931 | static BCRYPT_HASH_LENGTH := "HashDigestLength" 932 | static BCRYPT_OBJECT_LENGTH := "ObjectLength" 933 | } 934 | 935 | 936 | class NT 937 | { 938 | static SUCCESS := 0x00000000 939 | static AUTH_TAG_MISMATCH := 0xC000A002 940 | static BUFFER_TOO_SMALL := 0xC0000023 941 | static INVALID_BUFFER_SIZE := 0xC0000206 942 | static INVALID_HANDLE := 0xC0000008 943 | static INVALID_PARAMETER := 0xC000000D 944 | static NO_MEMORY := 0xC0000017 945 | static NOT_FOUND := 0xC0000225 946 | static NOT_SUPPORTED := 0xC00000BB 947 | } 948 | 949 | 950 | static EncryptionAlgorithm(Algorithm) 951 | { 952 | switch Algorithm 953 | { 954 | case "AES": return this.Constants.BCRYPT_AES_ALGORITHM 955 | case "DES": return this.Constants.BCRYPT_DES_ALGORITHM 956 | case "RC2": return this.Constants.BCRYPT_RC2_ALGORITHM 957 | case "RC4": return this.Constants.BCRYPT_RC4_ALGORITHM 958 | default: return "" 959 | } 960 | } 961 | 962 | 963 | static ChainingMode(ChainMode) 964 | { 965 | switch ChainMode 966 | { 967 | case "CBC", "ChainingModeCBC": return this.Constants.BCRYPT_CHAIN_MODE_CBC 968 | case "ECB", "ChainingModeECB": return this.Constants.BCRYPT_CHAIN_MODE_ECB 969 | default: return "" 970 | } 971 | } 972 | 973 | 974 | static HashAlgorithm(Algorithm) 975 | { 976 | switch Algorithm 977 | { 978 | case "MD2": return this.Constants.BCRYPT_MD2_ALGORITHM 979 | case "MD4": return this.Constants.BCRYPT_MD4_ALGORITHM 980 | case "MD5": return this.Constants.BCRYPT_MD5_ALGORITHM 981 | case "SHA1", "SHA-1": return this.Constants.BCRYPT_SHA1_ALGORITHM 982 | case "SHA256", "SHA-256": return this.Constants.BCRYPT_SHA256_ALGORITHM 983 | case "SHA384", "SHA-384": return this.Constants.BCRYPT_SHA384_ALGORITHM 984 | case "SHA512", "SHA-512": return this.Constants.BCRYPT_SHA512_ALGORITHM 985 | default: return "" 986 | } 987 | } 988 | } 989 | 990 | 991 | ; ======================================================================================================================================================================= 992 | 993 | 994 | class Crypt 995 | { 996 | 997 | #DllLoad "*i crypt32.dll" 998 | 999 | 1000 | ; //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 1001 | ; // 1002 | ; // FUNCTION NAME: Crypt.BinaryToString 1003 | ; // 1004 | ; // This function converts an array of bytes into a formatted string. 1005 | ; // 1006 | ; //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 1007 | static BinaryToString(BufIn, SizeIn, Flags := "BASE64") 1008 | { 1009 | static CRYPT_STRING := { BASE64: 0x1, BINARY: 0x2, HEX: 0x4, HEXRAW: 0xc } 1010 | static CRYPT_STRING_NOCRLF := 0x40000000 1011 | 1012 | if !(DllCall("crypt32\CryptBinaryToStringW", "Ptr", BufIn 1013 | , "UInt", SizeIn 1014 | , "UInt", (CRYPT_STRING.%Flags% | CRYPT_STRING_NOCRLF) 1015 | , "Ptr", 0 1016 | , "UInt*", &Size := 0)) 1017 | throw Error("Can't compute the destination buffer size, error: " A_LastError, -1) 1018 | 1019 | BufOut := Buffer(Size << 1, 0) 1020 | if !(DllCall("crypt32\CryptBinaryToStringW", "Ptr", BufIn 1021 | , "UInt", SizeIn 1022 | , "UInt", (CRYPT_STRING.%Flags% | CRYPT_STRING_NOCRLF) 1023 | , "Ptr", BufOut 1024 | , "UInt*", Size)) 1025 | throw Error("Can't convert source buffer to " Flags ", error: " A_LastError, -1) 1026 | 1027 | return StrGet(BufOut) 1028 | } 1029 | 1030 | 1031 | ; //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 1032 | ; // 1033 | ; // FUNCTION NAME: Crypt.StringToBinary 1034 | ; // 1035 | ; // This function converts a formatted string into an array of bytes. 1036 | ; // 1037 | ; //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 1038 | static StringToBinary(String, &Binary, Flags := "BASE64") 1039 | { 1040 | static CRYPT_STRING := { BASE64: 0x1, BINARY: 0x2, HEX: 0x4, HEXRAW: 0xc } 1041 | 1042 | if !(DllCall("crypt32\CryptStringToBinaryW", "Ptr", StrPtr(String) 1043 | , "UInt", 0 1044 | , "UInt", CRYPT_STRING.%Flags% 1045 | , "Ptr", 0 1046 | , "UInt*", &Size := 0 1047 | , "Ptr", 0 1048 | , "Ptr", 0)) 1049 | throw Error("Can't compute the destination buffer size, error: " A_LastError, -1) 1050 | 1051 | Binary := Buffer(Size, 0) 1052 | if !(DllCall("crypt32\CryptStringToBinaryW", "Ptr", StrPtr(String) 1053 | , "UInt", 0 1054 | , "UInt", CRYPT_STRING.%Flags% 1055 | , "Ptr", Binary 1056 | , "UInt*", Binary.Size 1057 | , "Ptr", 0 1058 | , "Ptr", 0)) 1059 | throw Error("Can't convert source buffer to " Flags ", error: " A_LastError, -1) 1060 | 1061 | return Binary.Size 1062 | } 1063 | } 1064 | 1065 | 1066 | ; ======================================================================================================================================================================= 1067 | 1068 | 1069 | static StrBuf(Str, Encoding := "UTF-8") 1070 | { 1071 | Buf := Buffer(StrPut(Str, Encoding)) 1072 | StrPut(Str, Buf, Encoding) 1073 | return Buf 1074 | } 1075 | 1076 | } 1077 | 1078 | ; =========================================================================================================================================================================== 1079 | -------------------------------------------------------------------------------- /src/v1.1_deprecated/Class_CNG.ahk: -------------------------------------------------------------------------------- 1 | ; =============================================================================================================================== 2 | ; AutoHotkey wrapper for Cryptography API: Next Generation 3 | ; 4 | ; Author ....: jNizM 5 | ; Released ..: 2016-09-15 6 | ; Modified ..: 2021-01-04 7 | ; Github ....: https://github.com/jNizM/AHK_CNG 8 | ; Forum .....: https://www.autohotkey.com/boards/viewtopic.php?f=6&t=23413 9 | ; =============================================================================================================================== 10 | 11 | 12 | class Crypt 13 | { 14 | 15 | ; ===== PUBLIC CLASS / METHODS ============================================================================================== 16 | 17 | class Encrypt 18 | { 19 | 20 | String(AlgId, Mode := "", String := "", Key := "", IV := "", Encoding := "utf-8", Output := "BASE64") 21 | { 22 | try 23 | { 24 | ; verify the encryption algorithm 25 | if !(ALGORITHM_IDENTIFIER := Crypt.Verify.EncryptionAlgorithm(AlgId)) 26 | throw Exception("Wrong ALGORITHM_IDENTIFIER", -1) 27 | 28 | ; open an algorithm handle. 29 | if !(ALG_HANDLE := Crypt.BCrypt.OpenAlgorithmProvider(ALGORITHM_IDENTIFIER)) 30 | throw Exception("BCryptOpenAlgorithmProvider failed", -1) 31 | 32 | ; verify the chaining mode 33 | if (CHAINING_MODE := Crypt.Verify.ChainingMode(Mode)) 34 | ; set chaining mode property. 35 | if !(Crypt.BCrypt.SetProperty(ALG_HANDLE, Crypt.Constants.BCRYPT_CHAINING_MODE, CHAINING_MODE)) 36 | throw Exception("SetProperty failed", -1) 37 | 38 | ; generate the key from supplied input key bytes. 39 | if !(KEY_HANDLE := Crypt.BCrypt.GenerateSymmetricKey(ALG_HANDLE, Key, Encoding)) 40 | throw Exception("GenerateSymmetricKey failed", -1) 41 | 42 | ; calculate the block length for the IV. 43 | if !(BLOCK_LENGTH := Crypt.BCrypt.GetProperty(ALG_HANDLE, Crypt.Constants.BCRYPT_BLOCK_LENGTH, 4)) 44 | throw Exception("GetProperty failed", -1) 45 | 46 | ; use the key to encrypt the plaintext buffer. for block sized messages, block padding will add an extra block. 47 | cbInput := Crypt.Helper.StrPutVar(String, pbInput, Encoding) 48 | if !(CIPHER_LENGTH := Crypt.BCrypt.Encrypt(KEY_HANDLE, pbInput, cbInput, IV, BLOCK_LENGTH, CIPHER_DATA, Crypt.Constants.BCRYPT_BLOCK_PADDING)) 49 | throw Exception("Encrypt failed", -1) 50 | 51 | ; convert binary data to string (base64 / hex / hexraw) 52 | if !(ENCRYPT := Crypt.Helper.CryptBinaryToString(CIPHER_DATA, CIPHER_LENGTH, Output)) 53 | throw Exception("CryptBinaryToString failed", -1) 54 | } 55 | catch Exception 56 | { 57 | ; represents errors that occur during application execution 58 | throw Exception 59 | } 60 | finally 61 | { 62 | ; cleaning up resources 63 | if (KEY_HANDLE) 64 | Crypt.BCrypt.DestroyKey(KEY_HANDLE) 65 | 66 | if (ALG_HANDLE) 67 | Crypt.BCrypt.CloseAlgorithmProvider(ALG_HANDLE) 68 | } 69 | return ENCRYPT 70 | } 71 | } 72 | 73 | 74 | class Decrypt 75 | { 76 | 77 | String(AlgId, Mode := "", String := "", Key := "", IV := "", Encoding := "utf-8", Input := "BASE64") 78 | { 79 | try 80 | { 81 | ; verify the encryption algorithm 82 | if !(ALGORITHM_IDENTIFIER := Crypt.Verify.EncryptionAlgorithm(AlgId)) 83 | throw Exception("Wrong ALGORITHM_IDENTIFIER", -1) 84 | 85 | ; open an algorithm handle. 86 | if !(ALG_HANDLE := Crypt.BCrypt.OpenAlgorithmProvider(ALGORITHM_IDENTIFIER)) 87 | throw Exception("BCryptOpenAlgorithmProvider failed", -1) 88 | 89 | ; verify the chaining mode 90 | if (CHAINING_MODE := Crypt.Verify.ChainingMode(Mode)) 91 | ; set chaining mode property. 92 | if !(Crypt.BCrypt.SetProperty(ALG_HANDLE, Crypt.Constants.BCRYPT_CHAINING_MODE, CHAINING_MODE)) 93 | throw Exception("SetProperty failed", -1) 94 | 95 | ; generate the key from supplied input key bytes. 96 | if !(KEY_HANDLE := Crypt.BCrypt.GenerateSymmetricKey(ALG_HANDLE, Key, Encoding)) 97 | throw Exception("GenerateSymmetricKey failed", -1) 98 | 99 | ; convert encrypted string (base64 / hex / hexraw) to binary data 100 | if !(CIPHER_LENGTH := Crypt.Helper.CryptStringToBinary(String, CIPHER_DATA, Input)) 101 | throw Exception("CryptStringToBinary failed", -1) 102 | 103 | ; calculate the block length for the IV. 104 | if !(BLOCK_LENGTH := Crypt.BCrypt.GetProperty(ALG_HANDLE, Crypt.Constants.BCRYPT_BLOCK_LENGTH, 4)) 105 | throw Exception("GetProperty failed", -1) 106 | 107 | ; use the key to decrypt the data to plaintext buffer 108 | if !(DECRYPT_LENGTH := Crypt.BCrypt.Decrypt(KEY_HANDLE, CIPHER_DATA, CIPHER_LENGTH, IV, BLOCK_LENGTH, DECRYPT_DATA, Crypt.Constants.BCRYPT_BLOCK_PADDING)) 109 | throw Exception("Decrypt failed", -1) 110 | 111 | ; receive the decrypted plaintext 112 | DECRYPT := StrGet(&DECRYPT_DATA, DECRYPT_LENGTH, Encoding) 113 | } 114 | catch Exception 115 | { 116 | ; represents errors that occur during application execution 117 | throw Exception 118 | } 119 | finally 120 | { 121 | ; cleaning up resources 122 | if (KEY_HANDLE) 123 | Crypt.BCrypt.DestroyKey(KEY_HANDLE) 124 | 125 | if (ALG_HANDLE) 126 | Crypt.BCrypt.CloseAlgorithmProvider(ALG_HANDLE) 127 | } 128 | return DECRYPT 129 | } 130 | 131 | } 132 | 133 | 134 | class Hash 135 | { 136 | 137 | String(AlgId, String, Encoding := "utf-8", Output := "HEXRAW") 138 | { 139 | try 140 | { 141 | ; verify the hash algorithm 142 | if !(ALGORITHM_IDENTIFIER := Crypt.Verify.HashAlgorithm(AlgId)) 143 | throw Exception("Wrong ALGORITHM_IDENTIFIER", -1) 144 | 145 | ; open an algorithm handle 146 | if !(ALG_HANDLE := Crypt.BCrypt.OpenAlgorithmProvider(ALGORITHM_IDENTIFIER)) 147 | throw Exception("BCryptOpenAlgorithmProvider failed", -1) 148 | 149 | ; create a hash 150 | if !(HASH_HANDLE := Crypt.BCrypt.CreateHash(ALG_HANDLE)) 151 | throw Exception("CreateHash failed", -1) 152 | 153 | ; hash some data 154 | cbInput := Crypt.Helper.StrPutVar(String, pbInput, Encoding) 155 | if !(Crypt.BCrypt.HashData(HASH_HANDLE, pbInput, cbInput)) 156 | throw Exception("HashData failed", -1) 157 | 158 | ; calculate the length of the hash 159 | if !(HASH_LENGTH := Crypt.BCrypt.GetProperty(ALG_HANDLE, Crypt.Constants.BCRYPT_HASH_LENGTH, 4)) 160 | throw Exception("GetProperty failed", -1) 161 | 162 | ; close the hash 163 | if !(Crypt.BCrypt.FinishHash(HASH_HANDLE, HASH_DATA, HASH_LENGTH)) 164 | throw Exception("FinishHash failed", -1) 165 | 166 | ; convert bin to string (base64 / hex) 167 | if !(HASH := Crypt.Helper.CryptBinaryToString(HASH_DATA, HASH_LENGTH, Output)) 168 | throw Exception("CryptBinaryToString failed", -1) 169 | } 170 | catch Exception 171 | { 172 | ; represents errors that occur during application execution 173 | throw Exception 174 | } 175 | finally 176 | { 177 | ; cleaning up resources 178 | if (HASH_HANDLE) 179 | Crypt.BCrypt.DestroyHash(HASH_HANDLE) 180 | 181 | if (ALG_HANDLE) 182 | Crypt.BCrypt.CloseAlgorithmProvider(ALG_HANDLE) 183 | } 184 | return HASH 185 | } 186 | 187 | 188 | File(AlgId, FileName, Bytes := 1048576, Offset := 0, Length := -1, Encoding := "utf-8", Output := "HEXRAW") 189 | { 190 | try 191 | { 192 | ; verify the hash algorithm 193 | if !(ALGORITHM_IDENTIFIER := Crypt.Verify.HashAlgorithm(AlgId)) 194 | throw Exception("Wrong ALGORITHM_IDENTIFIER", -1) 195 | 196 | ; open an algorithm handle 197 | if !(ALG_HANDLE := Crypt.BCrypt.OpenAlgorithmProvider(ALGORITHM_IDENTIFIER)) 198 | throw Exception("BCryptOpenAlgorithmProvider failed", -1) 199 | 200 | ; create a hash 201 | if !(HASH_HANDLE := Crypt.BCrypt.CreateHash(ALG_HANDLE)) 202 | throw Exception("CreateHash failed", -1) 203 | 204 | ; hash some data 205 | if !(IsObject(File := FileOpen(FileName, "r", Encoding))) 206 | throw Exception("Failed to open file: " FileName, -1) 207 | Length := Length < 0 ? File.Length - Offset : Length 208 | if ((Offset + Length) > File.Length) 209 | throw Exception("Invalid parameters offset / length!", -1) 210 | while (Length > Bytes) && (Dataread := File.RawRead(Data, Bytes)) 211 | { 212 | if !(Crypt.BCrypt.HashData(HASH_HANDLE, Data, Dataread)) 213 | throw Exception("HashData failed", -1) 214 | Length -= Dataread 215 | } 216 | if (Length > 0) 217 | { 218 | if (Dataread := File.RawRead(Data, Length)) 219 | { 220 | if !(Crypt.BCrypt.HashData(HASH_HANDLE, Data, Dataread)) 221 | throw Exception("HashData failed", -1) 222 | } 223 | } 224 | 225 | ; calculate the length of the hash 226 | if !(HASH_LENGTH := Crypt.BCrypt.GetProperty(ALG_HANDLE, Crypt.Constants.BCRYPT_HASH_LENGTH, 4)) 227 | throw Exception("GetProperty failed", -1) 228 | 229 | ; close the hash 230 | if !(Crypt.BCrypt.FinishHash(HASH_HANDLE, HASH_DATA, HASH_LENGTH)) 231 | throw Exception("FinishHash failed", -1) 232 | 233 | ; convert bin to string (base64 / hex) 234 | if !(HASH := Crypt.Helper.CryptBinaryToString(HASH_DATA, HASH_LENGTH, Output)) 235 | throw Exception("CryptBinaryToString failed", -1) 236 | } 237 | catch Exception 238 | { 239 | ; represents errors that occur during application execution 240 | throw Exception 241 | } 242 | finally 243 | { 244 | ; cleaning up resources 245 | if (File) 246 | File.Close() 247 | 248 | if (HASH_HANDLE) 249 | Crypt.BCrypt.DestroyHash(HASH_HANDLE) 250 | 251 | if (ALG_HANDLE) 252 | Crypt.BCrypt.CloseAlgorithmProvider(ALG_HANDLE) 253 | } 254 | return HASH 255 | } 256 | 257 | 258 | HMAC(AlgId, String, Hmac, Encoding := "utf-8", Output := "HEXRAW") 259 | { 260 | try 261 | { 262 | ; verify the hash algorithm 263 | if !(ALGORITHM_IDENTIFIER := Crypt.Verify.HashAlgorithm(AlgId)) 264 | throw Exception("Wrong ALGORITHM_IDENTIFIER", -1) 265 | 266 | ; open an algorithm handle 267 | if !(ALG_HANDLE := Crypt.BCrypt.OpenAlgorithmProvider(ALGORITHM_IDENTIFIER, Crypt.Constants.BCRYPT_ALG_HANDLE_HMAC_FLAG)) 268 | throw Exception("BCryptOpenAlgorithmProvider failed", -1) 269 | 270 | ; create a hash 271 | if !(HASH_HANDLE := Crypt.BCrypt.CreateHash(ALG_HANDLE, Hmac, Encoding)) 272 | throw Exception("CreateHash failed", -1) 273 | 274 | ; hash some data 275 | cbInput := Crypt.helper.StrPutVar(String, pbInput, Encoding) 276 | if !(Crypt.BCrypt.HashData(HASH_HANDLE, pbInput, cbInput)) 277 | throw Exception("HashData failed", -1) 278 | 279 | ; calculate the length of the hash 280 | if !(HASH_LENGTH := Crypt.BCrypt.GetProperty(ALG_HANDLE, Crypt.Constants.BCRYPT_HASH_LENGTH, 4)) 281 | throw Exception("GetProperty failed", -1) 282 | 283 | ; close the hash 284 | if !(Crypt.BCrypt.FinishHash(HASH_HANDLE, HASH_DATA, HASH_LENGTH)) 285 | throw Exception("FinishHash failed", -1) 286 | 287 | ; convert bin to string (base64 / hex) 288 | if !(HMAC := Crypt.Helper.CryptBinaryToString(HASH_DATA, HASH_LENGTH, Output)) 289 | throw Exception("CryptBinaryToString failed", -1) 290 | } 291 | catch Exception 292 | { 293 | ; represents errors that occur during application execution 294 | throw Exception 295 | } 296 | finally 297 | { 298 | ; cleaning up resources 299 | if (HASH_HANDLE) 300 | Crypt.BCrypt.DestroyHash(HASH_HANDLE) 301 | 302 | if (ALG_HANDLE) 303 | Crypt.BCrypt.CloseAlgorithmProvider(ALG_HANDLE) 304 | } 305 | return HMAC 306 | } 307 | 308 | 309 | PBKDF2(AlgId, Password, Salt, Iterations := 4096, KeySize := 256, Encoding := "utf-8", Output := "HEXRAW") 310 | { 311 | try 312 | { 313 | ; verify the hash algorithm 314 | if !(ALGORITHM_IDENTIFIER := Crypt.Verify.HashAlgorithm(AlgId)) 315 | throw Exception("Wrong ALGORITHM_IDENTIFIER", -1) 316 | 317 | ; open an algorithm handle 318 | if !(ALG_HANDLE := Crypt.BCrypt.OpenAlgorithmProvider(ALGORITHM_IDENTIFIER, Crypt.Constants.BCRYPT_ALG_HANDLE_HMAC_FLAG)) 319 | throw Exception("BCryptOpenAlgorithmProvider failed", -1) 320 | 321 | ; derives a key from a hash value 322 | if !(Crypt.BCrypt.DeriveKeyPBKDF2(ALG_HANDLE, Password, Salt, Iterations, PBKDF2_DATA, KeySize / 8, Encoding)) 323 | throw Exception("CreateHash failed", -1) 324 | 325 | ; convert bin to string (base64 / hex) 326 | if !(PBKDF2 := Crypt.Helper.CryptBinaryToString(PBKDF2_DATA , KeySize / 8, Output)) 327 | throw Exception("CryptBinaryToString failed", -1) 328 | } 329 | catch Exception 330 | { 331 | ; represents errors that occur during application execution 332 | throw Exception 333 | } 334 | finally 335 | { 336 | ; cleaning up resources 337 | if (ALG_HANDLE) 338 | Crypt.BCrypt.CloseAlgorithmProvider(ALG_HANDLE) 339 | } 340 | return PBKDF2 341 | } 342 | 343 | } 344 | 345 | 346 | 347 | ; ===== PRIVATE CLASS / METHODS ============================================================================================= 348 | 349 | 350 | /* 351 | CNG BCrypt Functions 352 | https://docs.microsoft.com/en-us/windows/win32/api/bcrypt/ 353 | */ 354 | class BCrypt 355 | { 356 | static hBCRYPT := DllCall("LoadLibrary", "str", "bcrypt.dll", "ptr") 357 | static STATUS_SUCCESS := 0 358 | 359 | 360 | CloseAlgorithmProvider(hAlgorithm) 361 | { 362 | DllCall("bcrypt\BCryptCloseAlgorithmProvider", "ptr", hAlgorithm, "uint", 0) 363 | } 364 | 365 | 366 | CreateHash(hAlgorithm, hmac := 0, encoding := "utf-8") 367 | { 368 | if (hmac) 369 | cbSecret := Crypt.helper.StrPutVar(hmac, pbSecret, encoding) 370 | NT_STATUS := DllCall("bcrypt\BCryptCreateHash", "ptr", hAlgorithm 371 | , "ptr*", phHash 372 | , "ptr", pbHashObject := 0 373 | , "uint", cbHashObject := 0 374 | , "ptr", (pbSecret ? &pbSecret : 0) 375 | , "uint", (cbSecret ? cbSecret : 0) 376 | , "uint", dwFlags := 0) 377 | 378 | if (NT_STATUS = this.STATUS_SUCCESS) 379 | return phHash 380 | return false 381 | } 382 | 383 | 384 | DeriveKeyPBKDF2(hPrf, Password, Salt, cIterations, ByRef pbDerivedKey, cbDerivedKey, Encoding := "utf-8") 385 | { 386 | cbPassword := Crypt.Helper.StrPutVar(Password, pbPassword, Encoding) 387 | cbSalt := Crypt.Helper.StrPutVar(Salt, pbSalt, Encoding) 388 | 389 | VarSetCapacity(pbDerivedKey, cbDerivedKey, 0) 390 | NT_STATUS := DllCall("bcrypt\BCryptDeriveKeyPBKDF2", "ptr", hPrf 391 | , "ptr", &pbPassword 392 | , "uint", cbPassword 393 | , "ptr", &pbSalt 394 | , "uint", cbSalt 395 | , "int64", cIterations 396 | , "ptr", &pbDerivedKey 397 | , "uint", cbDerivedKey 398 | , "uint", dwFlags := 0) 399 | 400 | if (NT_STATUS = this.STATUS_SUCCESS) 401 | return true 402 | return false 403 | } 404 | 405 | 406 | DestroyHash(hHash) 407 | { 408 | DllCall("bcrypt\BCryptDestroyHash", "ptr", hHash) 409 | } 410 | 411 | 412 | DestroyKey(hKey) 413 | { 414 | DllCall("bcrypt\BCryptDestroyKey", "ptr", hKey) 415 | } 416 | 417 | 418 | Decrypt(hKey, ByRef String, cbInput, IV, BCRYPT_BLOCK_LENGTH, ByRef pbOutput, dwFlags) 419 | { 420 | VarSetCapacity(pbInput, cbInput, 0) 421 | DllCall("msvcrt\memcpy", "ptr", &pbInput, "ptr", &String, "ptr", cbInput) 422 | 423 | if (IV != "") 424 | { 425 | cbIV := VarSetCapacity(pbIV, BCRYPT_BLOCK_LENGTH, 0) 426 | StrPut(IV, &pbIV, BCRYPT_BLOCK_LENGTH, Encoding) 427 | } 428 | 429 | NT_STATUS := DllCall("bcrypt\BCryptDecrypt", "ptr", hKey 430 | , "ptr", &pbInput 431 | , "uint", cbInput 432 | , "ptr", 0 433 | , "ptr", (pbIV ? &pbIV : 0) 434 | , "uint", (cbIV ? &cbIV : 0) 435 | , "ptr", 0 436 | , "uint", 0 437 | , "uint*", cbOutput 438 | , "uint", dwFlags) 439 | if (NT_STATUS = this.STATUS_SUCCESS) 440 | { 441 | VarSetCapacity(pbOutput, cbOutput, 0) 442 | NT_STATUS := DllCall("bcrypt\BCryptDecrypt", "ptr", hKey 443 | , "ptr", &pbInput 444 | , "uint", cbInput 445 | , "ptr", 0 446 | , "ptr", (pbIV ? &pbIV : 0) 447 | , "uint", (cbIV ? &cbIV : 0) 448 | , "ptr", &pbOutput 449 | , "uint", cbOutput 450 | , "uint*", cbOutput 451 | , "uint", dwFlags) 452 | if (NT_STATUS = this.STATUS_SUCCESS) 453 | { 454 | return cbOutput 455 | } 456 | } 457 | return false 458 | } 459 | 460 | 461 | Encrypt(hKey, ByRef pbInput, cbInput, IV, BCRYPT_BLOCK_LENGTH, ByRef pbOutput, dwFlags := 0) 462 | { 463 | ;cbInput := Crypt.Helper.StrPutVar(String, pbInput, Encoding) 464 | 465 | if (IV != "") 466 | { 467 | cbIV := VarSetCapacity(pbIV, BCRYPT_BLOCK_LENGTH, 0) 468 | StrPut(IV, &pbIV, BCRYPT_BLOCK_LENGTH, Encoding) 469 | } 470 | 471 | NT_STATUS := DllCall("bcrypt\BCryptEncrypt", "ptr", hKey 472 | , "ptr", &pbInput 473 | , "uint", cbInput 474 | , "ptr", 0 475 | , "ptr", (pbIV ? &pbIV : 0) 476 | , "uint", (cbIV ? &cbIV : 0) 477 | , "ptr", 0 478 | , "uint", 0 479 | , "uint*", cbOutput 480 | , "uint", dwFlags) 481 | if (NT_STATUS = this.STATUS_SUCCESS) 482 | { 483 | VarSetCapacity(pbOutput, cbOutput, 0) 484 | NT_STATUS := DllCall("bcrypt\BCryptEncrypt", "ptr", hKey 485 | , "ptr", &pbInput 486 | , "uint", cbInput 487 | , "ptr", 0 488 | , "ptr", (pbIV ? &pbIV : 0) 489 | , "uint", (cbIV ? &cbIV : 0) 490 | , "ptr", &pbOutput 491 | , "uint", cbOutput 492 | , "uint*", cbOutput 493 | , "uint", dwFlags) 494 | if (NT_STATUS = this.STATUS_SUCCESS) 495 | { 496 | return cbOutput 497 | } 498 | } 499 | return false 500 | } 501 | 502 | 503 | EnumAlgorithms(dwAlgOperations) 504 | { 505 | NT_STATUS := DllCall("bcrypt\BCryptEnumAlgorithms", "uint", dwAlgOperations 506 | , "uint*", pAlgCount 507 | , "ptr*", ppAlgList 508 | , "uint", dwFlags := 0) 509 | 510 | if (NT_STATUS = this.STATUS_SUCCESS) 511 | { 512 | addr := ppAlgList, BCRYPT_ALGORITHM_IDENTIFIER := [] 513 | loop % pAlgCount 514 | { 515 | BCRYPT_ALGORITHM_IDENTIFIER[A_Index, "Name"] := StrGet(NumGet(addr + A_PtrSize * 0, "uptr"), "utf-16") 516 | BCRYPT_ALGORITHM_IDENTIFIER[A_Index, "Class"] := NumGet(addr + A_PtrSize * 1, "uint") 517 | BCRYPT_ALGORITHM_IDENTIFIER[A_Index, "Flags"] := NumGet(addr + A_PtrSize * 1 + 4, "uint") 518 | addr += A_PtrSize * 2 519 | } 520 | return BCRYPT_ALGORITHM_IDENTIFIER 521 | } 522 | return false 523 | } 524 | 525 | 526 | EnumProviders(pszAlgId) 527 | { 528 | NT_STATUS := DllCall("bcrypt\BCryptEnumProviders", "ptr", pszAlgId 529 | , "uint*", pImplCount 530 | , "ptr*", ppImplList 531 | , "uint", dwFlags := 0) 532 | 533 | if (NT_STATUS = this.STATUS_SUCCESS) 534 | { 535 | addr := ppImplList, BCRYPT_PROVIDER_NAME := [] 536 | loop % pImplCount 537 | { 538 | BCRYPT_PROVIDER_NAME.Push(StrGet(NumGet(addr + A_PtrSize * 0, "uptr"), "utf-16")) 539 | addr += A_PtrSize 540 | } 541 | return BCRYPT_PROVIDER_NAME 542 | } 543 | return false 544 | } 545 | 546 | 547 | FinishHash(hHash, ByRef pbOutput, cbOutput) 548 | { 549 | VarSetCapacity(pbOutput, cbOutput, 0) 550 | NT_STATUS := DllCall("bcrypt\BCryptFinishHash", "ptr", hHash 551 | , "ptr", &pbOutput 552 | , "uint", cbOutput 553 | , "uint", dwFlags := 0) 554 | 555 | if (NT_STATUS = this.STATUS_SUCCESS) 556 | return cbOutput 557 | return false 558 | } 559 | 560 | 561 | GenerateSymmetricKey(hAlgorithm, Key, Encoding := "utf-8") 562 | { 563 | cbSecret := Crypt.Helper.StrPutVar(Key, pbSecret, Encoding) 564 | NT_STATUS := DllCall("bcrypt\BCryptGenerateSymmetricKey", "ptr", hAlgorithm 565 | , "ptr*", phKey 566 | , "ptr", 0 567 | , "uint", 0 568 | , "ptr", &pbSecret 569 | , "uint", cbSecret 570 | , "uint", dwFlags := 0) 571 | 572 | if (NT_STATUS = this.STATUS_SUCCESS) 573 | return phKey 574 | return false 575 | } 576 | 577 | 578 | GetProperty(hObject, pszProperty, cbOutput) 579 | { 580 | NT_STATUS := DllCall("bcrypt\BCryptGetProperty", "ptr", hObject 581 | , "ptr", &pszProperty 582 | , "uint*", pbOutput 583 | , "uint", cbOutput 584 | , "uint*", pcbResult 585 | , "uint", dwFlags := 0) 586 | 587 | if (NT_STATUS = this.STATUS_SUCCESS) 588 | return pbOutput 589 | return false 590 | } 591 | 592 | 593 | HashData(hHash, ByRef pbInput, cbInput) 594 | { 595 | NT_STATUS := DllCall("bcrypt\BCryptHashData", "ptr", hHash 596 | , "ptr", &pbInput 597 | , "uint", cbInput 598 | , "uint", dwFlags := 0) 599 | 600 | if (NT_STATUS = this.STATUS_SUCCESS) 601 | return true 602 | return false 603 | } 604 | 605 | 606 | OpenAlgorithmProvider(pszAlgId, dwFlags := 0, pszImplementation := 0) 607 | { 608 | NT_STATUS := DllCall("bcrypt\BCryptOpenAlgorithmProvider", "ptr*", phAlgorithm 609 | , "ptr", &pszAlgId 610 | , "ptr", pszImplementation 611 | , "uint", dwFlags) 612 | 613 | if (NT_STATUS = this.STATUS_SUCCESS) 614 | return phAlgorithm 615 | return false 616 | } 617 | 618 | 619 | SetProperty(hObject, pszProperty, pbInput) 620 | { 621 | bInput := StrLen(pbInput) 622 | NT_STATUS := DllCall("bcrypt\BCryptSetProperty", "ptr", hObject 623 | , "ptr", &pszProperty 624 | , "ptr", &pbInput 625 | , "uint", bInput 626 | , "uint", dwFlags := 0) 627 | 628 | if (NT_STATUS = this.STATUS_SUCCESS) 629 | return true 630 | return false 631 | } 632 | } 633 | 634 | 635 | class Helper 636 | { 637 | static hCRYPT32 := DllCall("LoadLibrary", "str", "crypt32.dll", "ptr") 638 | 639 | CryptBinaryToString(ByRef pbBinary, cbBinary, dwFlags := "BASE64") 640 | { 641 | static CRYPT_STRING := { "BASE64": 0x1, "BINARY": 0x2, "HEX": 0x4, "HEXRAW": 0xc } 642 | static CRYPT_STRING_NOCRLF := 0x40000000 643 | 644 | if (DllCall("crypt32\CryptBinaryToString", "ptr", &pbBinary 645 | , "uint", cbBinary 646 | , "uint", (CRYPT_STRING[dwFlags] | CRYPT_STRING_NOCRLF) 647 | , "ptr", 0 648 | , "uint*", pcchString)) 649 | { 650 | VarSetCapacity(pszString, pcchString << !!A_IsUnicode, 0) 651 | if (DllCall("crypt32\CryptBinaryToString", "ptr", &pbBinary 652 | , "uint", cbBinary 653 | , "uint", (CRYPT_STRING[dwFlags] | CRYPT_STRING_NOCRLF) 654 | , "ptr", &pszString 655 | , "uint*", pcchString)) 656 | { 657 | return StrGet(&pszString) 658 | } 659 | } 660 | return false 661 | } 662 | 663 | 664 | CryptStringToBinary(pszString, ByRef pbBinary, dwFlags := "BASE64") 665 | { 666 | static CRYPT_STRING := { "BASE64": 0x1, "BINARY": 0x2, "HEX": 0x4, "HEXRAW": 0xc } 667 | 668 | if (DllCall("crypt32\CryptStringToBinary", "ptr", &pszString 669 | , "uint", 0 670 | , "uint", CRYPT_STRING[dwFlags] 671 | , "ptr", 0 672 | , "uint*", pcbBinary 673 | , "ptr", 0 674 | , "ptr", 0)) 675 | { 676 | VarSetCapacity(pbBinary, pcbBinary, 0) 677 | if (DllCall("crypt32\CryptStringToBinary", "ptr", &pszString 678 | , "uint", 0 679 | , "uint", CRYPT_STRING[dwFlags] 680 | , "ptr", &pbBinary 681 | , "uint*", pcbBinary 682 | , "ptr", 0 683 | , "ptr", 0)) 684 | { 685 | return pcbBinary 686 | } 687 | } 688 | return false 689 | } 690 | 691 | 692 | StrPutVar(String, ByRef Data, Encoding) 693 | { 694 | if (Encoding = "hex") 695 | { 696 | String := InStr(String, "0x") ? SubStr(String, 3) : String 697 | VarSetCapacity(Data, (Length := StrLen(String) // 2), 0) 698 | loop % Length 699 | NumPut("0x" SubStr(String, 2 * A_Index - 1, 2), Data, A_Index - 1, "char") 700 | return Length 701 | } 702 | else 703 | { 704 | VarSetCapacity(Data, Length := StrPut(String, Encoding) * ((Encoding = "utf-16" || Encoding = "cp1200") ? 2 : 1) - 1) 705 | return StrPut(String, &Data, Length, Encoding) 706 | } 707 | } 708 | 709 | } 710 | 711 | 712 | class Verify 713 | { 714 | 715 | ChainingMode(ChainMode) 716 | { 717 | switch ChainMode 718 | { 719 | case "CBC", "ChainingModeCBC": return Crypt.Constants.BCRYPT_CHAIN_MODE_CBC 720 | case "CFB", "ChainingModeCFB": return Crypt.Constants.BCRYPT_CHAIN_MODE_CFB 721 | case "ECB", "ChainingModeECB": return Crypt.Constants.BCRYPT_CHAIN_MODE_ECB 722 | default: return "" 723 | } 724 | } 725 | 726 | 727 | EncryptionAlgorithm(Algorithm) 728 | { 729 | switch Algorithm 730 | { 731 | case "AES": return Crypt.Constants.BCRYPT_AES_ALGORITHM 732 | case "DES": return Crypt.Constants.BCRYPT_DES_ALGORITHM 733 | case "RC2": return Crypt.Constants.BCRYPT_RC2_ALGORITHM 734 | case "RC4": return Crypt.Constants.BCRYPT_RC4_ALGORITHM 735 | default: return "" 736 | } 737 | } 738 | 739 | 740 | HashAlgorithm(Algorithm) 741 | { 742 | switch Algorithm 743 | { 744 | case "MD2": return Crypt.Constants.BCRYPT_MD2_ALGORITHM 745 | case "MD4": return Crypt.Constants.BCRYPT_MD4_ALGORITHM 746 | case "MD5": return Crypt.Constants.BCRYPT_MD5_ALGORITHM 747 | case "SHA1", "SHA-1": return Crypt.Constants.BCRYPT_SHA1_ALGORITHM 748 | case "SHA256", "SHA-256": return Crypt.Constants.BCRYPT_SHA256_ALGORITHM 749 | case "SHA384", "SHA-384": return Crypt.Constants.BCRYPT_SHA384_ALGORITHM 750 | case "SHA512", "SHA-512": return Crypt.Constants.BCRYPT_SHA512_ALGORITHM 751 | default: return "" 752 | } 753 | } 754 | 755 | } 756 | 757 | 758 | 759 | ; ===== CONSTANTS =========================================================================================================== 760 | 761 | class Constants 762 | { 763 | static BCRYPT_ALG_HANDLE_HMAC_FLAG := 0x00000008 764 | static BCRYPT_BLOCK_PADDING := 0x00000001 765 | 766 | 767 | ; AlgOperations flags for use with BCryptEnumAlgorithms() 768 | static BCRYPT_CIPHER_OPERATION := 0x00000001 769 | static BCRYPT_HASH_OPERATION := 0x00000002 770 | static BCRYPT_ASYMMETRIC_ENCRYPTION_OPERATION := 0x00000004 771 | static BCRYPT_SECRET_AGREEMENT_OPERATION := 0x00000008 772 | static BCRYPT_SIGNATURE_OPERATION := 0x00000010 773 | static BCRYPT_RNG_OPERATION := 0x00000020 774 | static BCRYPT_KEY_DERIVATION_OPERATION := 0x00000040 775 | 776 | 777 | ; https://docs.microsoft.com/en-us/windows/win32/seccng/cng-algorithm-identifiers 778 | static BCRYPT_3DES_ALGORITHM := "3DES" 779 | static BCRYPT_3DES_112_ALGORITHM := "3DES_112" 780 | static BCRYPT_AES_ALGORITHM := "AES" 781 | static BCRYPT_AES_CMAC_ALGORITHM := "AES-CMAC" 782 | static BCRYPT_AES_GMAC_ALGORITHM := "AES-GMAC" 783 | static BCRYPT_DES_ALGORITHM := "DES" 784 | static BCRYPT_DESX_ALGORITHM := "DESX" 785 | static BCRYPT_MD2_ALGORITHM := "MD2" 786 | static BCRYPT_MD4_ALGORITHM := "MD4" 787 | static BCRYPT_MD5_ALGORITHM := "MD5" 788 | static BCRYPT_RC2_ALGORITHM := "RC2" 789 | static BCRYPT_RC4_ALGORITHM := "RC4" 790 | static BCRYPT_RNG_ALGORITHM := "RNG" 791 | static BCRYPT_SHA1_ALGORITHM := "SHA1" 792 | static BCRYPT_SHA256_ALGORITHM := "SHA256" 793 | static BCRYPT_SHA384_ALGORITHM := "SHA384" 794 | static BCRYPT_SHA512_ALGORITHM := "SHA512" 795 | static BCRYPT_PBKDF2_ALGORITHM := "PBKDF2" 796 | static BCRYPT_XTS_AES_ALGORITHM := "XTS-AES" 797 | 798 | 799 | ; https://docs.microsoft.com/en-us/windows/win32/seccng/cng-property-identifiers 800 | static BCRYPT_BLOCK_LENGTH := "BlockLength" 801 | static BCRYPT_CHAINING_MODE := "ChainingMode" 802 | static BCRYPT_CHAIN_MODE_CBC := "ChainingModeCBC" 803 | static BCRYPT_CHAIN_MODE_CCM := "ChainingModeCCM" 804 | static BCRYPT_CHAIN_MODE_CFB := "ChainingModeCFB" 805 | static BCRYPT_CHAIN_MODE_ECB := "ChainingModeECB" 806 | static BCRYPT_CHAIN_MODE_GCM := "ChainingModeGCM" 807 | static BCRYPT_HASH_LENGTH := "HashDigestLength" 808 | static BCRYPT_OBJECT_LENGTH := "ObjectLength" 809 | } 810 | } 811 | -------------------------------------------------------------------------------- /src/v1.1_deprecated/functions/bcrypt_md2.ahk: -------------------------------------------------------------------------------- 1 | MsgBox % bcrypt_md2("The quick brown fox jumps over the lazy dog") 2 | ; -> 03d85a0d629d2c442e987525319fc471 3 | 4 | 5 | 6 | bcrypt_md2(string, encoding := "utf-8") 7 | { 8 | static BCRYPT_MD2_ALGORITHM := "MD2" 9 | static BCRYPT_OBJECT_LENGTH := "ObjectLength" 10 | static BCRYPT_HASH_LENGTH := "HashDigestLength" 11 | 12 | try 13 | { 14 | ; loads the specified module into the address space of the calling process 15 | if !(hBCRYPT := DllCall("LoadLibrary", "str", "bcrypt.dll", "ptr")) 16 | throw Exception("Failed to load bcrypt.dll", -1) 17 | 18 | ; open an algorithm handle 19 | if (NT_STATUS := DllCall("bcrypt\BCryptOpenAlgorithmProvider", "ptr*", hAlg, "ptr", &BCRYPT_MD2_ALGORITHM, "ptr", 0, "uint", 0) != 0) 20 | throw Exception("BCryptOpenAlgorithmProvider: " NT_STATUS, -1) 21 | 22 | ; calculate the size of the buffer to hold the hash object 23 | if (NT_STATUS := DllCall("bcrypt\BCryptGetProperty", "ptr", hAlg, "ptr", &BCRYPT_OBJECT_LENGTH, "uint*", cbHashObject, "uint", 4, "uint*", cbData, "uint", 0) != 0) 24 | throw Exception("BCryptGetProperty: " NT_STATUS, -1) 25 | 26 | ; allocate the hash object 27 | VarSetCapacity(pbHashObject, cbHashObject, 0) 28 | ; throw Exception("Memory allocation failed", -1) 29 | 30 | ; calculate the length of the hash 31 | if (NT_STATUS := DllCall("bcrypt\BCryptGetProperty", "ptr", hAlg, "ptr", &BCRYPT_HASH_LENGTH, "uint*", cbHash, "uint", 4, "uint*", cbData, "uint", 0) != 0) 32 | throw Exception("BCryptGetProperty: " NT_STATUS, -1) 33 | 34 | ; allocate the hash buffer 35 | VarSetCapacity(pbHash, cbHash, 0) 36 | ; throw Exception("Memory allocation failed", -1) 37 | 38 | ; create a hash 39 | if (NT_STATUS := DllCall("bcrypt\BCryptCreateHash", "ptr", hAlg, "ptr*", hHash, "ptr", &pbHashObject, "uint", cbHashObject, "ptr", 0, "uint", 0, "uint", 0) != 0) 40 | throw Exception("BCryptCreateHash: " NT_STATUS, -1) 41 | 42 | ; hash some data 43 | VarSetCapacity(pbInput, (StrPut(string, encoding) - 1) * ((encoding = "utf-16" || encoding = "cp1200") ? 2 : 1), 0) 44 | cbInput := (StrPut(string, &pbInput, encoding) - 1) * ((encoding = "utf-16" || encoding = "cp1200") ? 2 : 1) 45 | if (NT_STATUS := DllCall("bcrypt\BCryptHashData", "ptr", hHash, "ptr", &pbInput, "uint", cbInput, "uint", 0) != 0) 46 | throw Exception("BCryptHashData: " NT_STATUS, -1) 47 | 48 | ; close the hash 49 | if (NT_STATUS := DllCall("bcrypt\BCryptFinishHash", "ptr", hHash, "ptr", &pbHash, "uint", cbHash, "uint", 0) != 0) 50 | throw Exception("BCryptFinishHash: " NT_STATUS, -1) 51 | 52 | loop % cbHash 53 | hash .= Format("{:02x}", NumGet(pbHash, A_Index - 1, "uchar")) 54 | } 55 | catch exception 56 | { 57 | ; represents errors that occur during application execution 58 | throw Exception 59 | } 60 | finally 61 | { 62 | ; cleaning up resources 63 | if (pbInput) 64 | VarSetCapacity(pbInput, 0) 65 | if (hHash) 66 | DllCall("bcrypt\BCryptDestroyHash", "ptr", hHash) 67 | if (pbHash) 68 | VarSetCapacity(pbHash, 0) 69 | if (pbHashObject) 70 | VarSetCapacity(pbHashObject, 0) 71 | if (hAlg) 72 | DllCall("bcrypt\BCryptCloseAlgorithmProvider", "ptr", hAlg, "uint", 0) 73 | if (hBCRYPT) 74 | DllCall("FreeLibrary", "ptr", hBCRYPT) 75 | } 76 | 77 | return hash 78 | } -------------------------------------------------------------------------------- /src/v1.1_deprecated/functions/bcrypt_md2_file.ahk: -------------------------------------------------------------------------------- 1 | v := 0x0 2 | file := FileOpen("TestFile", "rw") 3 | loop % 256 4 | file.WriteUChar(v++) 5 | file.Close() 6 | 7 | MsgBox % bcrypt_md2_file("TestFile") 8 | ; -> 9415bb1a3efd63923944e97c7acc7df2 9 | 10 | FileDelete, TestFile 11 | 12 | 13 | 14 | bcrypt_md2_file(filename, bytes := 1048576 , offset := 0, length := -1, encoding := "utf-8") 15 | { 16 | static BCRYPT_MD2_ALGORITHM := "MD2" 17 | static BCRYPT_OBJECT_LENGTH := "ObjectLength" 18 | static BCRYPT_HASH_LENGTH := "HashDigestLength" 19 | 20 | try 21 | { 22 | ; loads the specified module into the address space of the calling process 23 | if !(hBCRYPT := DllCall("LoadLibrary", "str", "bcrypt.dll", "ptr")) 24 | throw Exception("Failed to load bcrypt.dll", -1) 25 | 26 | ; open an algorithm handle 27 | if (NT_STATUS := DllCall("bcrypt\BCryptOpenAlgorithmProvider", "ptr*", hAlg, "ptr", &BCRYPT_MD2_ALGORITHM, "ptr", 0, "uint", 0) != 0) 28 | throw Exception("BCryptOpenAlgorithmProvider: " NT_STATUS, -1) 29 | 30 | ; calculate the size of the buffer to hold the hash object 31 | if (NT_STATUS := DllCall("bcrypt\BCryptGetProperty", "ptr", hAlg, "ptr", &BCRYPT_OBJECT_LENGTH, "uint*", cbHashObject, "uint", 4, "uint*", cbData, "uint", 0) != 0) 32 | throw Exception("BCryptGetProperty: " NT_STATUS, -1) 33 | 34 | ; allocate the hash object 35 | VarSetCapacity(pbHashObject, cbHashObject, 0) 36 | ; throw Exception("Memory allocation failed", -1) 37 | 38 | ; calculate the length of the hash 39 | if (NT_STATUS := DllCall("bcrypt\BCryptGetProperty", "ptr", hAlg, "ptr", &BCRYPT_HASH_LENGTH, "uint*", cbHash, "uint", 4, "uint*", cbData, "uint", 0) != 0) 40 | throw Exception("BCryptGetProperty: " NT_STATUS, -1) 41 | 42 | ; allocate the hash buffer 43 | VarSetCapacity(pbHash, cbHash, 0) 44 | ; throw Exception("Memory allocation failed", -1) 45 | 46 | ; create a hash 47 | if (NT_STATUS := DllCall("bcrypt\BCryptCreateHash", "ptr", hAlg, "ptr*", hHash, "ptr", &pbHashObject, "uint", cbHashObject, "ptr", 0, "uint", 0, "uint", 0) != 0) 48 | throw Exception("BCryptCreateHash: " NT_STATUS, -1) 49 | 50 | ; create a hash 51 | if !(IsObject(f := FileOpen(filename, "r", encoding))) 52 | throw Exception("Failed to open file: " filename, -1) 53 | length := length < 0 ? f.length - offset : length 54 | if ((offset + length) > f.length) 55 | throw Exception("Invalid parameters offset / length!", -1) 56 | f.Pos(offset) 57 | while (length > bytes) && (dataread := f.RawRead(data, bytes)) 58 | { 59 | if (NT_STATUS := DllCall("bcrypt\BCryptHashData", "ptr", hHash, "ptr", &data, "uint", dataread, "uint", 0) != 0) 60 | throw Exception("BCryptHashData: " NT_STATUS, -1) 61 | length -= dataread 62 | } 63 | if (length > 0) 64 | { 65 | if (dataread := f.RawRead(data, length)) 66 | if (NT_STATUS := DllCall("bcrypt\BCryptHashData", "ptr", hHash, "ptr", &data, "uint", dataread, "uint", 0) != 0) 67 | throw Exception("BCryptHashData: " NT_STATUS, -1) 68 | } 69 | 70 | ; close the hash 71 | if (NT_STATUS := DllCall("bcrypt\BCryptFinishHash", "ptr", hHash, "ptr", &pbHash, "uint", cbHash, "uint", 0) != 0) 72 | throw Exception("BCryptFinishHash: " NT_STATUS, -1) 73 | 74 | loop % cbHash 75 | hash .= Format("{:02x}", NumGet(pbHash, A_Index - 1, "uchar")) 76 | } 77 | catch exception 78 | { 79 | ; represents errors that occur during application execution 80 | throw Exception 81 | } 82 | finally 83 | { 84 | ; cleaning up resources 85 | if (f) 86 | f.Close() 87 | if (pbInput) 88 | VarSetCapacity(pbInput, 0) 89 | if (hHash) 90 | DllCall("bcrypt\BCryptDestroyHash", "ptr", hHash) 91 | if (pbHash) 92 | VarSetCapacity(pbHash, 0) 93 | if (pbHashObject) 94 | VarSetCapacity(pbHashObject, 0) 95 | if (hAlg) 96 | DllCall("bcrypt\BCryptCloseAlgorithmProvider", "ptr", hAlg, "uint", 0) 97 | if (hBCRYPT) 98 | DllCall("FreeLibrary", "ptr", hBCRYPT) 99 | } 100 | 101 | return hash 102 | } -------------------------------------------------------------------------------- /src/v1.1_deprecated/functions/bcrypt_md2_hmac.ahk: -------------------------------------------------------------------------------- 1 | MsgBox % bcrypt_md2_hmac("The quick brown fox jumps over the lazy dog", "Secret Salt") 2 | ; -> 6c05fa7a6f6de43ea70cf4e20fc1c648 3 | 4 | 5 | 6 | bcrypt_md2_hmac(string, hmac, encoding := "utf-8") 7 | { 8 | static BCRYPT_MD2_ALGORITHM := "MD2" 9 | static BCRYPT_ALG_HANDLE_HMAC_FLAG := 0x00000008 10 | static BCRYPT_OBJECT_LENGTH := "ObjectLength" 11 | static BCRYPT_HASH_LENGTH := "HashDigestLength" 12 | 13 | try 14 | { 15 | ; loads the specified module into the address space of the calling process 16 | if !(hBCRYPT := DllCall("LoadLibrary", "str", "bcrypt.dll", "ptr")) 17 | throw Exception("Failed to load bcrypt.dll", -1) 18 | 19 | ; open an algorithm handle 20 | if (NT_STATUS := DllCall("bcrypt\BCryptOpenAlgorithmProvider", "ptr*", hAlg, "ptr", &BCRYPT_MD2_ALGORITHM, "ptr", 0, "uint", BCRYPT_ALG_HANDLE_HMAC_FLAG) != 0) 21 | throw Exception("BCryptOpenAlgorithmProvider: " NT_STATUS, -1) 22 | 23 | ; calculate the size of the buffer to hold the hash object 24 | if (NT_STATUS := DllCall("bcrypt\BCryptGetProperty", "ptr", hAlg, "ptr", &BCRYPT_OBJECT_LENGTH, "uint*", cbHashObject, "uint", 4, "uint*", cbData, "uint", 0) != 0) 25 | throw Exception("BCryptGetProperty: " NT_STATUS, -1) 26 | 27 | ; allocate the hash object 28 | VarSetCapacity(pbHashObject, cbHashObject, 0) 29 | ; throw Exception("Memory allocation failed", -1) 30 | 31 | ; calculate the length of the hash 32 | if (NT_STATUS := DllCall("bcrypt\BCryptGetProperty", "ptr", hAlg, "ptr", &BCRYPT_HASH_LENGTH, "uint*", cbHash, "uint", 4, "uint*", cbData, "uint", 0) != 0) 33 | throw Exception("BCryptGetProperty: " NT_STATUS, -1) 34 | 35 | ; allocate the hash buffer 36 | VarSetCapacity(pbHash, cbHash, 0) 37 | ; throw Exception("Memory allocation failed", -1) 38 | 39 | ; create a hash 40 | VarSetCapacity(pbSecret, (StrPut(hmac, encoding) - 1) * ((encoding = "utf-16" || encoding = "cp1200") ? 2 : 1), 0) 41 | cbSecret := (StrPut(hmac, &pbSecret, encoding) - 1) * ((encoding = "utf-16" || encoding = "cp1200") ? 2 : 1) 42 | if (NT_STATUS := DllCall("bcrypt\BCryptCreateHash", "ptr", hAlg, "ptr*", hHash, "ptr", &pbHashObject, "uint", cbHashObject, "ptr", &pbSecret, "uint", cbSecret, "uint", 0) != 0) 43 | throw Exception("BCryptCreateHash: " NT_STATUS, -1) 44 | 45 | ; hash some data 46 | VarSetCapacity(pbInput, (StrPut(string, encoding) - 1) * ((encoding = "utf-16" || encoding = "cp1200") ? 2 : 1), 0) 47 | cbInput := (StrPut(string, &pbInput, encoding) - 1) * ((encoding = "utf-16" || encoding = "cp1200") ? 2 : 1) 48 | if (NT_STATUS := DllCall("bcrypt\BCryptHashData", "ptr", hHash, "ptr", &pbInput, "uint", cbInput, "uint", 0) != 0) 49 | throw Exception("BCryptHashData: " NT_STATUS, -1) 50 | 51 | ; close the hash 52 | if (NT_STATUS := DllCall("bcrypt\BCryptFinishHash", "ptr", hHash, "ptr", &pbHash, "uint", cbHash, "uint", 0) != 0) 53 | throw Exception("BCryptFinishHash: " NT_STATUS, -1) 54 | 55 | loop % cbHash 56 | hash .= Format("{:02x}", NumGet(pbHash, A_Index - 1, "uchar")) 57 | } 58 | catch exception 59 | { 60 | ; represents errors that occur during application execution 61 | throw Exception 62 | } 63 | finally 64 | { 65 | ; cleaning up resources 66 | if (pbInput) 67 | VarSetCapacity(pbInput, 0) 68 | if (hHash) 69 | DllCall("bcrypt\BCryptDestroyHash", "ptr", hHash) 70 | if (pbHash) 71 | VarSetCapacity(pbHash, 0) 72 | if (pbHashObject) 73 | VarSetCapacity(pbHashObject, 0) 74 | if (hAlg) 75 | DllCall("bcrypt\BCryptCloseAlgorithmProvider", "ptr", hAlg, "uint", 0) 76 | if (hBCRYPT) 77 | DllCall("FreeLibrary", "ptr", hBCRYPT) 78 | } 79 | 80 | return hash 81 | } -------------------------------------------------------------------------------- /src/v1.1_deprecated/functions/bcrypt_md4.ahk: -------------------------------------------------------------------------------- 1 | MsgBox % bcrypt_md4("The quick brown fox jumps over the lazy dog") 2 | ; -> 1bee69a46ba811185c194762abaeae90 3 | 4 | 5 | 6 | bcrypt_md4(string, encoding := "utf-8") 7 | { 8 | static BCRYPT_MD4_ALGORITHM := "MD4" 9 | static BCRYPT_OBJECT_LENGTH := "ObjectLength" 10 | static BCRYPT_HASH_LENGTH := "HashDigestLength" 11 | 12 | try 13 | { 14 | ; loads the specified module into the address space of the calling process 15 | if !(hBCRYPT := DllCall("LoadLibrary", "str", "bcrypt.dll", "ptr")) 16 | throw Exception("Failed to load bcrypt.dll", -1) 17 | 18 | ; open an algorithm handle 19 | if (NT_STATUS := DllCall("bcrypt\BCryptOpenAlgorithmProvider", "ptr*", hAlg, "ptr", &BCRYPT_MD4_ALGORITHM, "ptr", 0, "uint", 0) != 0) 20 | throw Exception("BCryptOpenAlgorithmProvider: " NT_STATUS, -1) 21 | 22 | ; calculate the size of the buffer to hold the hash object 23 | if (NT_STATUS := DllCall("bcrypt\BCryptGetProperty", "ptr", hAlg, "ptr", &BCRYPT_OBJECT_LENGTH, "uint*", cbHashObject, "uint", 4, "uint*", cbData, "uint", 0) != 0) 24 | throw Exception("BCryptGetProperty: " NT_STATUS, -1) 25 | 26 | ; allocate the hash object 27 | VarSetCapacity(pbHashObject, cbHashObject, 0) 28 | ; throw Exception("Memory allocation failed", -1) 29 | 30 | ; calculate the length of the hash 31 | if (NT_STATUS := DllCall("bcrypt\BCryptGetProperty", "ptr", hAlg, "ptr", &BCRYPT_HASH_LENGTH, "uint*", cbHash, "uint", 4, "uint*", cbData, "uint", 0) != 0) 32 | throw Exception("BCryptGetProperty: " NT_STATUS, -1) 33 | 34 | ; allocate the hash buffer 35 | VarSetCapacity(pbHash, cbHash, 0) 36 | ; throw Exception("Memory allocation failed", -1) 37 | 38 | ; create a hash 39 | if (NT_STATUS := DllCall("bcrypt\BCryptCreateHash", "ptr", hAlg, "ptr*", hHash, "ptr", &pbHashObject, "uint", cbHashObject, "ptr", 0, "uint", 0, "uint", 0) != 0) 40 | throw Exception("BCryptCreateHash: " NT_STATUS, -1) 41 | 42 | ; hash some data 43 | VarSetCapacity(pbInput, (StrPut(string, encoding) - 1) * ((encoding = "utf-16" || encoding = "cp1200") ? 2 : 1), 0) 44 | cbInput := (StrPut(string, &pbInput, encoding) - 1) * ((encoding = "utf-16" || encoding = "cp1200") ? 2 : 1) 45 | if (NT_STATUS := DllCall("bcrypt\BCryptHashData", "ptr", hHash, "ptr", &pbInput, "uint", cbInput, "uint", 0) != 0) 46 | throw Exception("BCryptHashData: " NT_STATUS, -1) 47 | 48 | ; close the hash 49 | if (NT_STATUS := DllCall("bcrypt\BCryptFinishHash", "ptr", hHash, "ptr", &pbHash, "uint", cbHash, "uint", 0) != 0) 50 | throw Exception("BCryptFinishHash: " NT_STATUS, -1) 51 | 52 | loop % cbHash 53 | hash .= Format("{:02x}", NumGet(pbHash, A_Index - 1, "uchar")) 54 | } 55 | catch exception 56 | { 57 | ; represents errors that occur during application execution 58 | throw Exception 59 | } 60 | finally 61 | { 62 | ; cleaning up resources 63 | if (pbInput) 64 | VarSetCapacity(pbInput, 0) 65 | if (hHash) 66 | DllCall("bcrypt\BCryptDestroyHash", "ptr", hHash) 67 | if (pbHash) 68 | VarSetCapacity(pbHash, 0) 69 | if (pbHashObject) 70 | VarSetCapacity(pbHashObject, 0) 71 | if (hAlg) 72 | DllCall("bcrypt\BCryptCloseAlgorithmProvider", "ptr", hAlg, "uint", 0) 73 | if (hBCRYPT) 74 | DllCall("FreeLibrary", "ptr", hBCRYPT) 75 | } 76 | 77 | return hash 78 | } -------------------------------------------------------------------------------- /src/v1.1_deprecated/functions/bcrypt_md4_file.ahk: -------------------------------------------------------------------------------- 1 | v := 0x0 2 | file := FileOpen("TestFile", "rw") 3 | loop % 256 4 | file.WriteUChar(v++) 5 | file.Close() 6 | 7 | MsgBox % bcrypt_md4_file("TestFile") 8 | ; -> 298a05bc506e1ecd5a47fd41f874f1d2 9 | 10 | FileDelete, TestFile 11 | 12 | 13 | 14 | bcrypt_md4_file(filename, bytes := 1048576 , offset := 0, length := -1, encoding := "utf-8") 15 | { 16 | static BCRYPT_MD4_ALGORITHM := "MD4" 17 | static BCRYPT_OBJECT_LENGTH := "ObjectLength" 18 | static BCRYPT_HASH_LENGTH := "HashDigestLength" 19 | 20 | try 21 | { 22 | ; loads the specified module into the address space of the calling process 23 | if !(hBCRYPT := DllCall("LoadLibrary", "str", "bcrypt.dll", "ptr")) 24 | throw Exception("Failed to load bcrypt.dll", -1) 25 | 26 | ; open an algorithm handle 27 | if (NT_STATUS := DllCall("bcrypt\BCryptOpenAlgorithmProvider", "ptr*", hAlg, "ptr", &BCRYPT_MD4_ALGORITHM, "ptr", 0, "uint", 0) != 0) 28 | throw Exception("BCryptOpenAlgorithmProvider: " NT_STATUS, -1) 29 | 30 | ; calculate the size of the buffer to hold the hash object 31 | if (NT_STATUS := DllCall("bcrypt\BCryptGetProperty", "ptr", hAlg, "ptr", &BCRYPT_OBJECT_LENGTH, "uint*", cbHashObject, "uint", 4, "uint*", cbData, "uint", 0) != 0) 32 | throw Exception("BCryptGetProperty: " NT_STATUS, -1) 33 | 34 | ; allocate the hash object 35 | VarSetCapacity(pbHashObject, cbHashObject, 0) 36 | ; throw Exception("Memory allocation failed", -1) 37 | 38 | ; calculate the length of the hash 39 | if (NT_STATUS := DllCall("bcrypt\BCryptGetProperty", "ptr", hAlg, "ptr", &BCRYPT_HASH_LENGTH, "uint*", cbHash, "uint", 4, "uint*", cbData, "uint", 0) != 0) 40 | throw Exception("BCryptGetProperty: " NT_STATUS, -1) 41 | 42 | ; allocate the hash buffer 43 | VarSetCapacity(pbHash, cbHash, 0) 44 | ; throw Exception("Memory allocation failed", -1) 45 | 46 | ; create a hash 47 | if (NT_STATUS := DllCall("bcrypt\BCryptCreateHash", "ptr", hAlg, "ptr*", hHash, "ptr", &pbHashObject, "uint", cbHashObject, "ptr", 0, "uint", 0, "uint", 0) != 0) 48 | throw Exception("BCryptCreateHash: " NT_STATUS, -1) 49 | 50 | ; create a hash 51 | if !(IsObject(f := FileOpen(filename, "r", encoding))) 52 | throw Exception("Failed to open file: " filename, -1) 53 | length := length < 0 ? f.length - offset : length 54 | if ((offset + length) > f.length) 55 | throw Exception("Invalid parameters offset / length!", -1) 56 | f.Pos(offset) 57 | while (length > bytes) && (dataread := f.RawRead(data, bytes)) 58 | { 59 | if (NT_STATUS := DllCall("bcrypt\BCryptHashData", "ptr", hHash, "ptr", &data, "uint", dataread, "uint", 0) != 0) 60 | throw Exception("BCryptHashData: " NT_STATUS, -1) 61 | length -= dataread 62 | } 63 | if (length > 0) 64 | { 65 | if (dataread := f.RawRead(data, length)) 66 | if (NT_STATUS := DllCall("bcrypt\BCryptHashData", "ptr", hHash, "ptr", &data, "uint", dataread, "uint", 0) != 0) 67 | throw Exception("BCryptHashData: " NT_STATUS, -1) 68 | } 69 | 70 | ; close the hash 71 | if (NT_STATUS := DllCall("bcrypt\BCryptFinishHash", "ptr", hHash, "ptr", &pbHash, "uint", cbHash, "uint", 0) != 0) 72 | throw Exception("BCryptFinishHash: " NT_STATUS, -1) 73 | 74 | loop % cbHash 75 | hash .= Format("{:02x}", NumGet(pbHash, A_Index - 1, "uchar")) 76 | } 77 | catch exception 78 | { 79 | ; represents errors that occur during application execution 80 | throw Exception 81 | } 82 | finally 83 | { 84 | ; cleaning up resources 85 | if (f) 86 | f.Close() 87 | if (pbInput) 88 | VarSetCapacity(pbInput, 0) 89 | if (hHash) 90 | DllCall("bcrypt\BCryptDestroyHash", "ptr", hHash) 91 | if (pbHash) 92 | VarSetCapacity(pbHash, 0) 93 | if (pbHashObject) 94 | VarSetCapacity(pbHashObject, 0) 95 | if (hAlg) 96 | DllCall("bcrypt\BCryptCloseAlgorithmProvider", "ptr", hAlg, "uint", 0) 97 | if (hBCRYPT) 98 | DllCall("FreeLibrary", "ptr", hBCRYPT) 99 | } 100 | 101 | return hash 102 | } -------------------------------------------------------------------------------- /src/v1.1_deprecated/functions/bcrypt_md4_hmac.ahk: -------------------------------------------------------------------------------- 1 | MsgBox % bcrypt_md4_hmac("The quick brown fox jumps over the lazy dog", "Secret Salt") 2 | ; -> 54864ebb1c93bbbfaa860d8b2d567133 3 | 4 | 5 | 6 | bcrypt_md4_hmac(string, hmac, encoding := "utf-8") 7 | { 8 | static BCRYPT_MD4_ALGORITHM := "MD4" 9 | static BCRYPT_ALG_HANDLE_HMAC_FLAG := 0x00000008 10 | static BCRYPT_OBJECT_LENGTH := "ObjectLength" 11 | static BCRYPT_HASH_LENGTH := "HashDigestLength" 12 | 13 | try 14 | { 15 | ; loads the specified module into the address space of the calling process 16 | if !(hBCRYPT := DllCall("LoadLibrary", "str", "bcrypt.dll", "ptr")) 17 | throw Exception("Failed to load bcrypt.dll", -1) 18 | 19 | ; open an algorithm handle 20 | if (NT_STATUS := DllCall("bcrypt\BCryptOpenAlgorithmProvider", "ptr*", hAlg, "ptr", &BCRYPT_MD4_ALGORITHM, "ptr", 0, "uint", BCRYPT_ALG_HANDLE_HMAC_FLAG) != 0) 21 | throw Exception("BCryptOpenAlgorithmProvider: " NT_STATUS, -1) 22 | 23 | ; calculate the size of the buffer to hold the hash object 24 | if (NT_STATUS := DllCall("bcrypt\BCryptGetProperty", "ptr", hAlg, "ptr", &BCRYPT_OBJECT_LENGTH, "uint*", cbHashObject, "uint", 4, "uint*", cbData, "uint", 0) != 0) 25 | throw Exception("BCryptGetProperty: " NT_STATUS, -1) 26 | 27 | ; allocate the hash object 28 | VarSetCapacity(pbHashObject, cbHashObject, 0) 29 | ; throw Exception("Memory allocation failed", -1) 30 | 31 | ; calculate the length of the hash 32 | if (NT_STATUS := DllCall("bcrypt\BCryptGetProperty", "ptr", hAlg, "ptr", &BCRYPT_HASH_LENGTH, "uint*", cbHash, "uint", 4, "uint*", cbData, "uint", 0) != 0) 33 | throw Exception("BCryptGetProperty: " NT_STATUS, -1) 34 | 35 | ; allocate the hash buffer 36 | VarSetCapacity(pbHash, cbHash, 0) 37 | ; throw Exception("Memory allocation failed", -1) 38 | 39 | ; create a hash 40 | VarSetCapacity(pbSecret, (StrPut(hmac, encoding) - 1) * ((encoding = "utf-16" || encoding = "cp1200") ? 2 : 1), 0) 41 | cbSecret := (StrPut(hmac, &pbSecret, encoding) - 1) * ((encoding = "utf-16" || encoding = "cp1200") ? 2 : 1) 42 | if (NT_STATUS := DllCall("bcrypt\BCryptCreateHash", "ptr", hAlg, "ptr*", hHash, "ptr", &pbHashObject, "uint", cbHashObject, "ptr", &pbSecret, "uint", cbSecret, "uint", 0) != 0) 43 | throw Exception("BCryptCreateHash: " NT_STATUS, -1) 44 | 45 | ; hash some data 46 | VarSetCapacity(pbInput, (StrPut(string, encoding) - 1) * ((encoding = "utf-16" || encoding = "cp1200") ? 2 : 1), 0) 47 | cbInput := (StrPut(string, &pbInput, encoding) - 1) * ((encoding = "utf-16" || encoding = "cp1200") ? 2 : 1) 48 | if (NT_STATUS := DllCall("bcrypt\BCryptHashData", "ptr", hHash, "ptr", &pbInput, "uint", cbInput, "uint", 0) != 0) 49 | throw Exception("BCryptHashData: " NT_STATUS, -1) 50 | 51 | ; close the hash 52 | if (NT_STATUS := DllCall("bcrypt\BCryptFinishHash", "ptr", hHash, "ptr", &pbHash, "uint", cbHash, "uint", 0) != 0) 53 | throw Exception("BCryptFinishHash: " NT_STATUS, -1) 54 | 55 | loop % cbHash 56 | hash .= Format("{:02x}", NumGet(pbHash, A_Index - 1, "uchar")) 57 | } 58 | catch exception 59 | { 60 | ; represents errors that occur during application execution 61 | throw Exception 62 | } 63 | finally 64 | { 65 | ; cleaning up resources 66 | if (pbInput) 67 | VarSetCapacity(pbInput, 0) 68 | if (hHash) 69 | DllCall("bcrypt\BCryptDestroyHash", "ptr", hHash) 70 | if (pbHash) 71 | VarSetCapacity(pbHash, 0) 72 | if (pbHashObject) 73 | VarSetCapacity(pbHashObject, 0) 74 | if (hAlg) 75 | DllCall("bcrypt\BCryptCloseAlgorithmProvider", "ptr", hAlg, "uint", 0) 76 | if (hBCRYPT) 77 | DllCall("FreeLibrary", "ptr", hBCRYPT) 78 | } 79 | 80 | return hash 81 | } -------------------------------------------------------------------------------- /src/v1.1_deprecated/functions/bcrypt_md5.ahk: -------------------------------------------------------------------------------- 1 | MsgBox % bcrypt_md5("The quick brown fox jumps over the lazy dog") 2 | ; -> 9e107d9d372bb6826bd81d3542a419d6 3 | 4 | 5 | 6 | bcrypt_md5(string, encoding := "utf-8") 7 | { 8 | static BCRYPT_MD5_ALGORITHM := "MD5" 9 | static BCRYPT_OBJECT_LENGTH := "ObjectLength" 10 | static BCRYPT_HASH_LENGTH := "HashDigestLength" 11 | 12 | try 13 | { 14 | ; loads the specified module into the address space of the calling process 15 | if !(hBCRYPT := DllCall("LoadLibrary", "str", "bcrypt.dll", "ptr")) 16 | throw Exception("Failed to load bcrypt.dll", -1) 17 | 18 | ; open an algorithm handle 19 | if (NT_STATUS := DllCall("bcrypt\BCryptOpenAlgorithmProvider", "ptr*", hAlg, "ptr", &BCRYPT_MD5_ALGORITHM, "ptr", 0, "uint", 0) != 0) 20 | throw Exception("BCryptOpenAlgorithmProvider: " NT_STATUS, -1) 21 | 22 | ; calculate the size of the buffer to hold the hash object 23 | if (NT_STATUS := DllCall("bcrypt\BCryptGetProperty", "ptr", hAlg, "ptr", &BCRYPT_OBJECT_LENGTH, "uint*", cbHashObject, "uint", 4, "uint*", cbData, "uint", 0) != 0) 24 | throw Exception("BCryptGetProperty: " NT_STATUS, -1) 25 | 26 | ; allocate the hash object 27 | VarSetCapacity(pbHashObject, cbHashObject, 0) 28 | ; throw Exception("Memory allocation failed", -1) 29 | 30 | ; calculate the length of the hash 31 | if (NT_STATUS := DllCall("bcrypt\BCryptGetProperty", "ptr", hAlg, "ptr", &BCRYPT_HASH_LENGTH, "uint*", cbHash, "uint", 4, "uint*", cbData, "uint", 0) != 0) 32 | throw Exception("BCryptGetProperty: " NT_STATUS, -1) 33 | 34 | ; allocate the hash buffer 35 | VarSetCapacity(pbHash, cbHash, 0) 36 | ; throw Exception("Memory allocation failed", -1) 37 | 38 | ; create a hash 39 | if (NT_STATUS := DllCall("bcrypt\BCryptCreateHash", "ptr", hAlg, "ptr*", hHash, "ptr", &pbHashObject, "uint", cbHashObject, "ptr", 0, "uint", 0, "uint", 0) != 0) 40 | throw Exception("BCryptCreateHash: " NT_STATUS, -1) 41 | 42 | ; hash some data 43 | VarSetCapacity(pbInput, (StrPut(string, encoding) - 1) * ((encoding = "utf-16" || encoding = "cp1200") ? 2 : 1), 0) 44 | cbInput := (StrPut(string, &pbInput, encoding) - 1) * ((encoding = "utf-16" || encoding = "cp1200") ? 2 : 1) 45 | if (NT_STATUS := DllCall("bcrypt\BCryptHashData", "ptr", hHash, "ptr", &pbInput, "uint", cbInput, "uint", 0) != 0) 46 | throw Exception("BCryptHashData: " NT_STATUS, -1) 47 | 48 | ; close the hash 49 | if (NT_STATUS := DllCall("bcrypt\BCryptFinishHash", "ptr", hHash, "ptr", &pbHash, "uint", cbHash, "uint", 0) != 0) 50 | throw Exception("BCryptFinishHash: " NT_STATUS, -1) 51 | 52 | loop % cbHash 53 | hash .= Format("{:02x}", NumGet(pbHash, A_Index - 1, "uchar")) 54 | } 55 | catch exception 56 | { 57 | ; represents errors that occur during application execution 58 | throw Exception 59 | } 60 | finally 61 | { 62 | ; cleaning up resources 63 | if (pbInput) 64 | VarSetCapacity(pbInput, 0) 65 | if (hHash) 66 | DllCall("bcrypt\BCryptDestroyHash", "ptr", hHash) 67 | if (pbHash) 68 | VarSetCapacity(pbHash, 0) 69 | if (pbHashObject) 70 | VarSetCapacity(pbHashObject, 0) 71 | if (hAlg) 72 | DllCall("bcrypt\BCryptCloseAlgorithmProvider", "ptr", hAlg, "uint", 0) 73 | if (hBCRYPT) 74 | DllCall("FreeLibrary", "ptr", hBCRYPT) 75 | } 76 | 77 | return hash 78 | } -------------------------------------------------------------------------------- /src/v1.1_deprecated/functions/bcrypt_md5_file.ahk: -------------------------------------------------------------------------------- 1 | v := 0x0 2 | file := FileOpen("TestFile", "rw") 3 | loop % 256 4 | file.WriteUChar(v++) 5 | file.Close() 6 | 7 | MsgBox % bcrypt_md5_file("TestFile") 8 | ; -> e2c865db4162bed963bfaa9ef6ac18f0 9 | 10 | FileDelete, TestFile 11 | 12 | 13 | 14 | bcrypt_md5_file(filename, bytes := 1048576 , offset := 0, length := -1, encoding := "utf-8") 15 | { 16 | static BCRYPT_MD5_ALGORITHM := "MD5" 17 | static BCRYPT_OBJECT_LENGTH := "ObjectLength" 18 | static BCRYPT_HASH_LENGTH := "HashDigestLength" 19 | 20 | try 21 | { 22 | ; loads the specified module into the address space of the calling process 23 | if !(hBCRYPT := DllCall("LoadLibrary", "str", "bcrypt.dll", "ptr")) 24 | throw Exception("Failed to load bcrypt.dll", -1) 25 | 26 | ; open an algorithm handle 27 | if (NT_STATUS := DllCall("bcrypt\BCryptOpenAlgorithmProvider", "ptr*", hAlg, "ptr", &BCRYPT_MD5_ALGORITHM, "ptr", 0, "uint", 0) != 0) 28 | throw Exception("BCryptOpenAlgorithmProvider: " NT_STATUS, -1) 29 | 30 | ; calculate the size of the buffer to hold the hash object 31 | if (NT_STATUS := DllCall("bcrypt\BCryptGetProperty", "ptr", hAlg, "ptr", &BCRYPT_OBJECT_LENGTH, "uint*", cbHashObject, "uint", 4, "uint*", cbData, "uint", 0) != 0) 32 | throw Exception("BCryptGetProperty: " NT_STATUS, -1) 33 | 34 | ; allocate the hash object 35 | VarSetCapacity(pbHashObject, cbHashObject, 0) 36 | ; throw Exception("Memory allocation failed", -1) 37 | 38 | ; calculate the length of the hash 39 | if (NT_STATUS := DllCall("bcrypt\BCryptGetProperty", "ptr", hAlg, "ptr", &BCRYPT_HASH_LENGTH, "uint*", cbHash, "uint", 4, "uint*", cbData, "uint", 0) != 0) 40 | throw Exception("BCryptGetProperty: " NT_STATUS, -1) 41 | 42 | ; allocate the hash buffer 43 | VarSetCapacity(pbHash, cbHash, 0) 44 | ; throw Exception("Memory allocation failed", -1) 45 | 46 | ; create a hash 47 | if (NT_STATUS := DllCall("bcrypt\BCryptCreateHash", "ptr", hAlg, "ptr*", hHash, "ptr", &pbHashObject, "uint", cbHashObject, "ptr", 0, "uint", 0, "uint", 0) != 0) 48 | throw Exception("BCryptCreateHash: " NT_STATUS, -1) 49 | 50 | ; create a hash 51 | if !(IsObject(f := FileOpen(filename, "r", encoding))) 52 | throw Exception("Failed to open file: " filename, -1) 53 | length := length < 0 ? f.length - offset : length 54 | if ((offset + length) > f.length) 55 | throw Exception("Invalid parameters offset / length!", -1) 56 | f.Pos(offset) 57 | while (length > bytes) && (dataread := f.RawRead(data, bytes)) 58 | { 59 | if (NT_STATUS := DllCall("bcrypt\BCryptHashData", "ptr", hHash, "ptr", &data, "uint", dataread, "uint", 0) != 0) 60 | throw Exception("BCryptHashData: " NT_STATUS, -1) 61 | length -= dataread 62 | } 63 | if (length > 0) 64 | { 65 | if (dataread := f.RawRead(data, length)) 66 | if (NT_STATUS := DllCall("bcrypt\BCryptHashData", "ptr", hHash, "ptr", &data, "uint", dataread, "uint", 0) != 0) 67 | throw Exception("BCryptHashData: " NT_STATUS, -1) 68 | } 69 | 70 | ; close the hash 71 | if (NT_STATUS := DllCall("bcrypt\BCryptFinishHash", "ptr", hHash, "ptr", &pbHash, "uint", cbHash, "uint", 0) != 0) 72 | throw Exception("BCryptFinishHash: " NT_STATUS, -1) 73 | 74 | loop % cbHash 75 | hash .= Format("{:02x}", NumGet(pbHash, A_Index - 1, "uchar")) 76 | } 77 | catch exception 78 | { 79 | ; represents errors that occur during application execution 80 | throw Exception 81 | } 82 | finally 83 | { 84 | ; cleaning up resources 85 | if (f) 86 | f.Close() 87 | if (pbInput) 88 | VarSetCapacity(pbInput, 0) 89 | if (hHash) 90 | DllCall("bcrypt\BCryptDestroyHash", "ptr", hHash) 91 | if (pbHash) 92 | VarSetCapacity(pbHash, 0) 93 | if (pbHashObject) 94 | VarSetCapacity(pbHashObject, 0) 95 | if (hAlg) 96 | DllCall("bcrypt\BCryptCloseAlgorithmProvider", "ptr", hAlg, "uint", 0) 97 | if (hBCRYPT) 98 | DllCall("FreeLibrary", "ptr", hBCRYPT) 99 | } 100 | 101 | return hash 102 | } -------------------------------------------------------------------------------- /src/v1.1_deprecated/functions/bcrypt_md5_hmac.ahk: -------------------------------------------------------------------------------- 1 | MsgBox % bcrypt_md5_hmac("The quick brown fox jumps over the lazy dog", "Secret Salt") 2 | ; -> ad8af8953b9f7f880887ab3bd7a7674a 3 | 4 | 5 | 6 | bcrypt_md5_hmac(string, hmac, encoding := "utf-8") 7 | { 8 | static BCRYPT_MD5_ALGORITHM := "MD5" 9 | static BCRYPT_ALG_HANDLE_HMAC_FLAG := 0x00000008 10 | static BCRYPT_OBJECT_LENGTH := "ObjectLength" 11 | static BCRYPT_HASH_LENGTH := "HashDigestLength" 12 | 13 | try 14 | { 15 | ; loads the specified module into the address space of the calling process 16 | if !(hBCRYPT := DllCall("LoadLibrary", "str", "bcrypt.dll", "ptr")) 17 | throw Exception("Failed to load bcrypt.dll", -1) 18 | 19 | ; open an algorithm handle 20 | if (NT_STATUS := DllCall("bcrypt\BCryptOpenAlgorithmProvider", "ptr*", hAlg, "ptr", &BCRYPT_MD5_ALGORITHM, "ptr", 0, "uint", BCRYPT_ALG_HANDLE_HMAC_FLAG) != 0) 21 | throw Exception("BCryptOpenAlgorithmProvider: " NT_STATUS, -1) 22 | 23 | ; calculate the size of the buffer to hold the hash object 24 | if (NT_STATUS := DllCall("bcrypt\BCryptGetProperty", "ptr", hAlg, "ptr", &BCRYPT_OBJECT_LENGTH, "uint*", cbHashObject, "uint", 4, "uint*", cbData, "uint", 0) != 0) 25 | throw Exception("BCryptGetProperty: " NT_STATUS, -1) 26 | 27 | ; allocate the hash object 28 | VarSetCapacity(pbHashObject, cbHashObject, 0) 29 | ; throw Exception("Memory allocation failed", -1) 30 | 31 | ; calculate the length of the hash 32 | if (NT_STATUS := DllCall("bcrypt\BCryptGetProperty", "ptr", hAlg, "ptr", &BCRYPT_HASH_LENGTH, "uint*", cbHash, "uint", 4, "uint*", cbData, "uint", 0) != 0) 33 | throw Exception("BCryptGetProperty: " NT_STATUS, -1) 34 | 35 | ; allocate the hash buffer 36 | VarSetCapacity(pbHash, cbHash, 0) 37 | ; throw Exception("Memory allocation failed", -1) 38 | 39 | ; create a hash 40 | VarSetCapacity(pbSecret, (StrPut(hmac, encoding) - 1) * ((encoding = "utf-16" || encoding = "cp1200") ? 2 : 1), 0) 41 | cbSecret := (StrPut(hmac, &pbSecret, encoding) - 1) * ((encoding = "utf-16" || encoding = "cp1200") ? 2 : 1) 42 | if (NT_STATUS := DllCall("bcrypt\BCryptCreateHash", "ptr", hAlg, "ptr*", hHash, "ptr", &pbHashObject, "uint", cbHashObject, "ptr", &pbSecret, "uint", cbSecret, "uint", 0) != 0) 43 | throw Exception("BCryptCreateHash: " NT_STATUS, -1) 44 | 45 | ; hash some data 46 | VarSetCapacity(pbInput, (StrPut(string, encoding) - 1) * ((encoding = "utf-16" || encoding = "cp1200") ? 2 : 1), 0) 47 | cbInput := (StrPut(string, &pbInput, encoding) - 1) * ((encoding = "utf-16" || encoding = "cp1200") ? 2 : 1) 48 | if (NT_STATUS := DllCall("bcrypt\BCryptHashData", "ptr", hHash, "ptr", &pbInput, "uint", cbInput, "uint", 0) != 0) 49 | throw Exception("BCryptHashData: " NT_STATUS, -1) 50 | 51 | ; close the hash 52 | if (NT_STATUS := DllCall("bcrypt\BCryptFinishHash", "ptr", hHash, "ptr", &pbHash, "uint", cbHash, "uint", 0) != 0) 53 | throw Exception("BCryptFinishHash: " NT_STATUS, -1) 54 | 55 | loop % cbHash 56 | hash .= Format("{:02x}", NumGet(pbHash, A_Index - 1, "uchar")) 57 | } 58 | catch exception 59 | { 60 | ; represents errors that occur during application execution 61 | throw Exception 62 | } 63 | finally 64 | { 65 | ; cleaning up resources 66 | if (pbInput) 67 | VarSetCapacity(pbInput, 0) 68 | if (hHash) 69 | DllCall("bcrypt\BCryptDestroyHash", "ptr", hHash) 70 | if (pbHash) 71 | VarSetCapacity(pbHash, 0) 72 | if (pbHashObject) 73 | VarSetCapacity(pbHashObject, 0) 74 | if (hAlg) 75 | DllCall("bcrypt\BCryptCloseAlgorithmProvider", "ptr", hAlg, "uint", 0) 76 | if (hBCRYPT) 77 | DllCall("FreeLibrary", "ptr", hBCRYPT) 78 | } 79 | 80 | return hash 81 | } -------------------------------------------------------------------------------- /src/v1.1_deprecated/functions/bcrypt_pbkdf2_md2.ahk: -------------------------------------------------------------------------------- 1 | MsgBox % bcrypt_pbkdf2_md2("The quick brown fox jumps over the lazy dog", "Secret Salt") 2 | ; -> 5ba040dee9ecab7259884b50af25ef83 3 | 4 | 5 | 6 | bcrypt_pbkdf2_md2(password, salt, iterations := 4096, keysize := 16, encoding := "utf-8") 7 | { 8 | static BCRYPT_MD2_ALGORITHM := "MD2" 9 | static BCRYPT_ALG_HANDLE_HMAC_FLAG := 0x00000008 10 | 11 | try 12 | { 13 | ; loads the specified module into the address space of the calling process 14 | if !(hBCRYPT := DllCall("LoadLibrary", "str", "bcrypt.dll", "ptr")) 15 | throw Exception("Failed to load bcrypt.dll", -1) 16 | 17 | ; open an algorithm handle 18 | if (NT_STATUS := DllCall("bcrypt\BCryptOpenAlgorithmProvider", "ptr*", hAlg, "ptr", &BCRYPT_MD2_ALGORITHM, "ptr", 0, "uint", BCRYPT_ALG_HANDLE_HMAC_FLAG) != 0) 19 | throw Exception("BCryptOpenAlgorithmProvider: " NT_STATUS, -1) 20 | 21 | ; allocate the derived key buffer 22 | VarSetCapacity(pbDKey, keysize, 0) 23 | ; throw Exception("Memory allocation failed", -1) 24 | 25 | ; derives a key from a hash value 26 | VarSetCapacity(pbPass, (StrPut(password, encoding) - 1) * ((encoding = "utf-16" || encoding = "cp1200") ? 2 : 1), 0) 27 | cbPass := (StrPut(password, &pbPass, encoding) - 1) * ((encoding = "utf-16" || encoding = "cp1200") ? 2 : 1) 28 | VarSetCapacity(pbSalt, (StrPut(salt, encoding) - 1) * ((encoding = "utf-16" || encoding = "cp1200") ? 2 : 1), 0) 29 | cbSalt := (StrPut(salt, &pbSalt, encoding) - 1) * ((encoding = "utf-16" || encoding = "cp1200") ? 2 : 1) 30 | if (NT_STATUS := DllCall("bcrypt\BCryptDeriveKeyPBKDF2", "ptr", hAlg, "ptr", &pbPass, "uint", cbPass, "ptr", &pbSalt, "uint", cbSalt, "int64", iterations, "ptr", &pbDKey, "uint", keysize, "uint", 0) != 0) 31 | throw Exception("BCryptDeriveKeyPBKDF2: " NT_STATUS, -1) 32 | 33 | loop % keysize 34 | pbkdf2 .= Format("{:02x}", NumGet(pbDKey, A_Index - 1, "uchar")) 35 | } 36 | catch exception 37 | { 38 | ; represents errors that occur during application execution 39 | throw Exception 40 | } 41 | finally 42 | { 43 | ; cleaning up resources 44 | if (pbSalt) 45 | VarSetCapacity(pbSalt, 0) 46 | if (pbPass) 47 | VarSetCapacity(pbPass, 0) 48 | if (pbDKey) 49 | VarSetCapacity(pbDKey, 0) 50 | if (hAlg) 51 | DllCall("bcrypt\BCryptCloseAlgorithmProvider", "ptr", hAlg, "uint", 0) 52 | if (hBCRYPT) 53 | DllCall("FreeLibrary", "ptr", hBCRYPT) 54 | } 55 | 56 | return pbkdf2 57 | } -------------------------------------------------------------------------------- /src/v1.1_deprecated/functions/bcrypt_pbkdf2_md4.ahk: -------------------------------------------------------------------------------- 1 | MsgBox % bcrypt_pbkdf2_md4("The quick brown fox jumps over the lazy dog", "Secret Salt") 2 | ; -> 76bbd07a1c8d6f15c7e50525b634d6dd 3 | 4 | 5 | 6 | bcrypt_pbkdf2_md4(password, salt, iterations := 4096, keysize := 16, encoding := "utf-8") 7 | { 8 | static BCRYPT_MD4_ALGORITHM := "MD4" 9 | static BCRYPT_ALG_HANDLE_HMAC_FLAG := 0x00000008 10 | 11 | try 12 | { 13 | ; loads the specified module into the address space of the calling process 14 | if !(hBCRYPT := DllCall("LoadLibrary", "str", "bcrypt.dll", "ptr")) 15 | throw Exception("Failed to load bcrypt.dll", -1) 16 | 17 | ; open an algorithm handle 18 | if (NT_STATUS := DllCall("bcrypt\BCryptOpenAlgorithmProvider", "ptr*", hAlg, "ptr", &BCRYPT_MD4_ALGORITHM, "ptr", 0, "uint", BCRYPT_ALG_HANDLE_HMAC_FLAG) != 0) 19 | throw Exception("BCryptOpenAlgorithmProvider: " NT_STATUS, -1) 20 | 21 | ; allocate the derived key buffer 22 | VarSetCapacity(pbDKey, keysize, 0) 23 | ; throw Exception("Memory allocation failed", -1) 24 | 25 | ; derives a key from a hash value 26 | VarSetCapacity(pbPass, (StrPut(password, encoding) - 1) * ((encoding = "utf-16" || encoding = "cp1200") ? 2 : 1), 0) 27 | cbPass := (StrPut(password, &pbPass, encoding) - 1) * ((encoding = "utf-16" || encoding = "cp1200") ? 2 : 1) 28 | VarSetCapacity(pbSalt, (StrPut(salt, encoding) - 1) * ((encoding = "utf-16" || encoding = "cp1200") ? 2 : 1), 0) 29 | cbSalt := (StrPut(salt, &pbSalt, encoding) - 1) * ((encoding = "utf-16" || encoding = "cp1200") ? 2 : 1) 30 | if (NT_STATUS := DllCall("bcrypt\BCryptDeriveKeyPBKDF2", "ptr", hAlg, "ptr", &pbPass, "uint", cbPass, "ptr", &pbSalt, "uint", cbSalt, "int64", iterations, "ptr", &pbDKey, "uint", keysize, "uint", 0) != 0) 31 | throw Exception("BCryptDeriveKeyPBKDF2: " NT_STATUS, -1) 32 | 33 | loop % keysize 34 | pbkdf2 .= Format("{:02x}", NumGet(pbDKey, A_Index - 1, "uchar")) 35 | } 36 | catch exception 37 | { 38 | ; represents errors that occur during application execution 39 | throw Exception 40 | } 41 | finally 42 | { 43 | ; cleaning up resources 44 | if (pbSalt) 45 | VarSetCapacity(pbSalt, 0) 46 | if (pbPass) 47 | VarSetCapacity(pbPass, 0) 48 | if (pbDKey) 49 | VarSetCapacity(pbDKey, 0) 50 | if (hAlg) 51 | DllCall("bcrypt\BCryptCloseAlgorithmProvider", "ptr", hAlg, "uint", 0) 52 | if (hBCRYPT) 53 | DllCall("FreeLibrary", "ptr", hBCRYPT) 54 | } 55 | 56 | return pbkdf2 57 | } -------------------------------------------------------------------------------- /src/v1.1_deprecated/functions/bcrypt_pbkdf2_md5.ahk: -------------------------------------------------------------------------------- 1 | MsgBox % bcrypt_pbkdf2_md5("The quick brown fox jumps over the lazy dog", "Secret Salt") 2 | ; -> 8191e192ab4fed8c1918f51eed10f81c 3 | 4 | 5 | 6 | bcrypt_pbkdf2_md5(password, salt, iterations := 4096, keysize := 16, encoding := "utf-8") 7 | { 8 | static BCRYPT_MD5_ALGORITHM := "MD5" 9 | static BCRYPT_ALG_HANDLE_HMAC_FLAG := 0x00000008 10 | 11 | try 12 | { 13 | ; loads the specified module into the address space of the calling process 14 | if !(hBCRYPT := DllCall("LoadLibrary", "str", "bcrypt.dll", "ptr")) 15 | throw Exception("Failed to load bcrypt.dll", -1) 16 | 17 | ; open an algorithm handle 18 | if (NT_STATUS := DllCall("bcrypt\BCryptOpenAlgorithmProvider", "ptr*", hAlg, "ptr", &BCRYPT_MD5_ALGORITHM, "ptr", 0, "uint", BCRYPT_ALG_HANDLE_HMAC_FLAG) != 0) 19 | throw Exception("BCryptOpenAlgorithmProvider: " NT_STATUS, -1) 20 | 21 | ; allocate the derived key buffer 22 | VarSetCapacity(pbDKey, keysize, 0) 23 | ; throw Exception("Memory allocation failed", -1) 24 | 25 | ; derives a key from a hash value 26 | VarSetCapacity(pbPass, (StrPut(password, encoding) - 1) * ((encoding = "utf-16" || encoding = "cp1200") ? 2 : 1), 0) 27 | cbPass := (StrPut(password, &pbPass, encoding) - 1) * ((encoding = "utf-16" || encoding = "cp1200") ? 2 : 1) 28 | VarSetCapacity(pbSalt, (StrPut(salt, encoding) - 1) * ((encoding = "utf-16" || encoding = "cp1200") ? 2 : 1), 0) 29 | cbSalt := (StrPut(salt, &pbSalt, encoding) - 1) * ((encoding = "utf-16" || encoding = "cp1200") ? 2 : 1) 30 | if (NT_STATUS := DllCall("bcrypt\BCryptDeriveKeyPBKDF2", "ptr", hAlg, "ptr", &pbPass, "uint", cbPass, "ptr", &pbSalt, "uint", cbSalt, "int64", iterations, "ptr", &pbDKey, "uint", keysize, "uint", 0) != 0) 31 | throw Exception("BCryptDeriveKeyPBKDF2: " NT_STATUS, -1) 32 | 33 | loop % keysize 34 | pbkdf2 .= Format("{:02x}", NumGet(pbDKey, A_Index - 1, "uchar")) 35 | } 36 | catch exception 37 | { 38 | ; represents errors that occur during application execution 39 | throw Exception 40 | } 41 | finally 42 | { 43 | ; cleaning up resources 44 | if (pbSalt) 45 | VarSetCapacity(pbSalt, 0) 46 | if (pbPass) 47 | VarSetCapacity(pbPass, 0) 48 | if (pbDKey) 49 | VarSetCapacity(pbDKey, 0) 50 | if (hAlg) 51 | DllCall("bcrypt\BCryptCloseAlgorithmProvider", "ptr", hAlg, "uint", 0) 52 | if (hBCRYPT) 53 | DllCall("FreeLibrary", "ptr", hBCRYPT) 54 | } 55 | 56 | return pbkdf2 57 | } -------------------------------------------------------------------------------- /src/v1.1_deprecated/functions/bcrypt_pbkdf2_sha1.ahk: -------------------------------------------------------------------------------- 1 | MsgBox % bcrypt_pbkdf2_sha1("The quick brown fox jumps over the lazy dog", "Secret Salt") 2 | ; -> 275607744d6aa8c633512e6fd44cec860f1b9f18 3 | 4 | 5 | 6 | bcrypt_pbkdf2_sha1(password, salt, iterations := 4096, keysize := 20, encoding := "utf-8") 7 | { 8 | static BCRYPT_SHA1_ALGORITHM := "SHA1" 9 | static BCRYPT_ALG_HANDLE_HMAC_FLAG := 0x00000008 10 | 11 | try 12 | { 13 | ; loads the specified module into the address space of the calling process 14 | if !(hBCRYPT := DllCall("LoadLibrary", "str", "bcrypt.dll", "ptr")) 15 | throw Exception("Failed to load bcrypt.dll", -1) 16 | 17 | ; open an algorithm handle 18 | if (NT_STATUS := DllCall("bcrypt\BCryptOpenAlgorithmProvider", "ptr*", hAlg, "ptr", &BCRYPT_SHA1_ALGORITHM, "ptr", 0, "uint", BCRYPT_ALG_HANDLE_HMAC_FLAG) != 0) 19 | throw Exception("BCryptOpenAlgorithmProvider: " NT_STATUS, -1) 20 | 21 | ; allocate the derived key buffer 22 | VarSetCapacity(pbDKey, keysize, 0) 23 | ; throw Exception("Memory allocation failed", -1) 24 | 25 | ; derives a key from a hash value 26 | VarSetCapacity(pbPass, (StrPut(password, encoding) - 1) * ((encoding = "utf-16" || encoding = "cp1200") ? 2 : 1), 0) 27 | cbPass := (StrPut(password, &pbPass, encoding) - 1) * ((encoding = "utf-16" || encoding = "cp1200") ? 2 : 1) 28 | VarSetCapacity(pbSalt, (StrPut(salt, encoding) - 1) * ((encoding = "utf-16" || encoding = "cp1200") ? 2 : 1), 0) 29 | cbSalt := (StrPut(salt, &pbSalt, encoding) - 1) * ((encoding = "utf-16" || encoding = "cp1200") ? 2 : 1) 30 | if (NT_STATUS := DllCall("bcrypt\BCryptDeriveKeyPBKDF2", "ptr", hAlg, "ptr", &pbPass, "uint", cbPass, "ptr", &pbSalt, "uint", cbSalt, "int64", iterations, "ptr", &pbDKey, "uint", keysize, "uint", 0) != 0) 31 | throw Exception("BCryptDeriveKeyPBKDF2: " NT_STATUS, -1) 32 | 33 | loop % keysize 34 | pbkdf2 .= Format("{:02x}", NumGet(pbDKey, A_Index - 1, "uchar")) 35 | } 36 | catch exception 37 | { 38 | ; represents errors that occur during application execution 39 | throw Exception 40 | } 41 | finally 42 | { 43 | ; cleaning up resources 44 | if (pbSalt) 45 | VarSetCapacity(pbSalt, 0) 46 | if (pbPass) 47 | VarSetCapacity(pbPass, 0) 48 | if (pbDKey) 49 | VarSetCapacity(pbDKey, 0) 50 | if (hAlg) 51 | DllCall("bcrypt\BCryptCloseAlgorithmProvider", "ptr", hAlg, "uint", 0) 52 | if (hBCRYPT) 53 | DllCall("FreeLibrary", "ptr", hBCRYPT) 54 | } 55 | 56 | return pbkdf2 57 | } -------------------------------------------------------------------------------- /src/v1.1_deprecated/functions/bcrypt_pbkdf2_sha256.ahk: -------------------------------------------------------------------------------- 1 | MsgBox % bcrypt_pbkdf2_sha256("The quick brown fox jumps over the lazy dog", "Secret Salt") 2 | ; -> 70497e570c8cbe1c486e7f6ce755df4f5535dbe16e84337eb04946b1267b0d9d 3 | 4 | 5 | 6 | bcrypt_pbkdf2_sha256(password, salt, iterations := 4096, keysize := 32, encoding := "utf-8") 7 | { 8 | static BCRYPT_SHA256_ALGORITHM := "SHA256" 9 | static BCRYPT_ALG_HANDLE_HMAC_FLAG := 0x00000008 10 | 11 | try 12 | { 13 | ; loads the specified module into the address space of the calling process 14 | if !(hBCRYPT := DllCall("LoadLibrary", "str", "bcrypt.dll", "ptr")) 15 | throw Exception("Failed to load bcrypt.dll", -1) 16 | 17 | ; open an algorithm handle 18 | if (NT_STATUS := DllCall("bcrypt\BCryptOpenAlgorithmProvider", "ptr*", hAlg, "ptr", &BCRYPT_SHA256_ALGORITHM, "ptr", 0, "uint", BCRYPT_ALG_HANDLE_HMAC_FLAG) != 0) 19 | throw Exception("BCryptOpenAlgorithmProvider: " NT_STATUS, -1) 20 | 21 | ; allocate the derived key buffer 22 | VarSetCapacity(pbDKey, keysize, 0) 23 | ; throw Exception("Memory allocation failed", -1) 24 | 25 | ; derives a key from a hash value 26 | VarSetCapacity(pbPass, (StrPut(password, encoding) - 1) * ((encoding = "utf-16" || encoding = "cp1200") ? 2 : 1), 0) 27 | cbPass := (StrPut(password, &pbPass, encoding) - 1) * ((encoding = "utf-16" || encoding = "cp1200") ? 2 : 1) 28 | VarSetCapacity(pbSalt, (StrPut(salt, encoding) - 1) * ((encoding = "utf-16" || encoding = "cp1200") ? 2 : 1), 0) 29 | cbSalt := (StrPut(salt, &pbSalt, encoding) - 1) * ((encoding = "utf-16" || encoding = "cp1200") ? 2 : 1) 30 | if (NT_STATUS := DllCall("bcrypt\BCryptDeriveKeyPBKDF2", "ptr", hAlg, "ptr", &pbPass, "uint", cbPass, "ptr", &pbSalt, "uint", cbSalt, "int64", iterations, "ptr", &pbDKey, "uint", keysize, "uint", 0) != 0) 31 | throw Exception("BCryptDeriveKeyPBKDF2: " NT_STATUS, -1) 32 | 33 | loop % keysize 34 | pbkdf2 .= Format("{:02x}", NumGet(pbDKey, A_Index - 1, "uchar")) 35 | } 36 | catch exception 37 | { 38 | ; represents errors that occur during application execution 39 | throw Exception 40 | } 41 | finally 42 | { 43 | ; cleaning up resources 44 | if (pbSalt) 45 | VarSetCapacity(pbSalt, 0) 46 | if (pbPass) 47 | VarSetCapacity(pbPass, 0) 48 | if (pbDKey) 49 | VarSetCapacity(pbDKey, 0) 50 | if (hAlg) 51 | DllCall("bcrypt\BCryptCloseAlgorithmProvider", "ptr", hAlg, "uint", 0) 52 | if (hBCRYPT) 53 | DllCall("FreeLibrary", "ptr", hBCRYPT) 54 | } 55 | 56 | return pbkdf2 57 | } -------------------------------------------------------------------------------- /src/v1.1_deprecated/functions/bcrypt_pbkdf2_sha384.ahk: -------------------------------------------------------------------------------- 1 | MsgBox % bcrypt_pbkdf2_sha384("The quick brown fox jumps over the lazy dog", "Secret Salt") 2 | ; -> baad91313b631b698db4110bd28aa3b35e0cd19709ebf665e68da7f2afd89003037e162204eb91a1af7ef01e9327c7d4 3 | 4 | 5 | 6 | bcrypt_pbkdf2_sha384(password, salt, iterations := 4096, keysize := 48, encoding := "utf-8") 7 | { 8 | static BCRYPT_SHA384_ALGORITHM := "SHA384" 9 | static BCRYPT_ALG_HANDLE_HMAC_FLAG := 0x00000008 10 | 11 | try 12 | { 13 | ; loads the specified module into the address space of the calling process 14 | if !(hBCRYPT := DllCall("LoadLibrary", "str", "bcrypt.dll", "ptr")) 15 | throw Exception("Failed to load bcrypt.dll", -1) 16 | 17 | ; open an algorithm handle 18 | if (NT_STATUS := DllCall("bcrypt\BCryptOpenAlgorithmProvider", "ptr*", hAlg, "ptr", &BCRYPT_SHA384_ALGORITHM, "ptr", 0, "uint", BCRYPT_ALG_HANDLE_HMAC_FLAG) != 0) 19 | throw Exception("BCryptOpenAlgorithmProvider: " NT_STATUS, -1) 20 | 21 | ; allocate the derived key buffer 22 | VarSetCapacity(pbDKey, keysize, 0) 23 | ; throw Exception("Memory allocation failed", -1) 24 | 25 | ; derives a key from a hash value 26 | VarSetCapacity(pbPass, (StrPut(password, encoding) - 1) * ((encoding = "utf-16" || encoding = "cp1200") ? 2 : 1), 0) 27 | cbPass := (StrPut(password, &pbPass, encoding) - 1) * ((encoding = "utf-16" || encoding = "cp1200") ? 2 : 1) 28 | VarSetCapacity(pbSalt, (StrPut(salt, encoding) - 1) * ((encoding = "utf-16" || encoding = "cp1200") ? 2 : 1), 0) 29 | cbSalt := (StrPut(salt, &pbSalt, encoding) - 1) * ((encoding = "utf-16" || encoding = "cp1200") ? 2 : 1) 30 | if (NT_STATUS := DllCall("bcrypt\BCryptDeriveKeyPBKDF2", "ptr", hAlg, "ptr", &pbPass, "uint", cbPass, "ptr", &pbSalt, "uint", cbSalt, "int64", iterations, "ptr", &pbDKey, "uint", keysize, "uint", 0) != 0) 31 | throw Exception("BCryptDeriveKeyPBKDF2: " NT_STATUS, -1) 32 | 33 | loop % keysize 34 | pbkdf2 .= Format("{:02x}", NumGet(pbDKey, A_Index - 1, "uchar")) 35 | } 36 | catch exception 37 | { 38 | ; represents errors that occur during application execution 39 | throw Exception 40 | } 41 | finally 42 | { 43 | ; cleaning up resources 44 | if (pbSalt) 45 | VarSetCapacity(pbSalt, 0) 46 | if (pbPass) 47 | VarSetCapacity(pbPass, 0) 48 | if (pbDKey) 49 | VarSetCapacity(pbDKey, 0) 50 | if (hAlg) 51 | DllCall("bcrypt\BCryptCloseAlgorithmProvider", "ptr", hAlg, "uint", 0) 52 | if (hBCRYPT) 53 | DllCall("FreeLibrary", "ptr", hBCRYPT) 54 | } 55 | 56 | return pbkdf2 57 | } -------------------------------------------------------------------------------- /src/v1.1_deprecated/functions/bcrypt_pbkdf2_sha512.ahk: -------------------------------------------------------------------------------- 1 | MsgBox % bcrypt_pbkdf2_sha512("The quick brown fox jumps over the lazy dog", "Secret Salt") 2 | ; -> c69571bb7ac960be902e9ec59062e2a6b3b1827c98c2e725797cdaff15cc8714411527fc39f4967c9bf07b8f46182add813ac6f0e3bbda5ffdccdc4b334540c0 3 | 4 | 5 | 6 | bcrypt_pbkdf2_sha512(password, salt, iterations := 4096, keysize := 64, encoding := "utf-8") 7 | { 8 | static BCRYPT_SHA512_ALGORITHM := "SHA512" 9 | static BCRYPT_ALG_HANDLE_HMAC_FLAG := 0x00000008 10 | 11 | try 12 | { 13 | ; loads the specified module into the address space of the calling process 14 | if !(hBCRYPT := DllCall("LoadLibrary", "str", "bcrypt.dll", "ptr")) 15 | throw Exception("Failed to load bcrypt.dll", -1) 16 | 17 | ; open an algorithm handle 18 | if (NT_STATUS := DllCall("bcrypt\BCryptOpenAlgorithmProvider", "ptr*", hAlg, "ptr", &BCRYPT_SHA512_ALGORITHM, "ptr", 0, "uint", BCRYPT_ALG_HANDLE_HMAC_FLAG) != 0) 19 | throw Exception("BCryptOpenAlgorithmProvider: " NT_STATUS, -1) 20 | 21 | ; allocate the derived key buffer 22 | VarSetCapacity(pbDKey, keysize, 0) 23 | ; throw Exception("Memory allocation failed", -1) 24 | 25 | ; derives a key from a hash value 26 | VarSetCapacity(pbPass, (StrPut(password, encoding) - 1) * ((encoding = "utf-16" || encoding = "cp1200") ? 2 : 1), 0) 27 | cbPass := (StrPut(password, &pbPass, encoding) - 1) * ((encoding = "utf-16" || encoding = "cp1200") ? 2 : 1) 28 | VarSetCapacity(pbSalt, (StrPut(salt, encoding) - 1) * ((encoding = "utf-16" || encoding = "cp1200") ? 2 : 1), 0) 29 | cbSalt := (StrPut(salt, &pbSalt, encoding) - 1) * ((encoding = "utf-16" || encoding = "cp1200") ? 2 : 1) 30 | if (NT_STATUS := DllCall("bcrypt\BCryptDeriveKeyPBKDF2", "ptr", hAlg, "ptr", &pbPass, "uint", cbPass, "ptr", &pbSalt, "uint", cbSalt, "int64", iterations, "ptr", &pbDKey, "uint", keysize, "uint", 0) != 0) 31 | throw Exception("BCryptDeriveKeyPBKDF2: " NT_STATUS, -1) 32 | 33 | loop % keysize 34 | pbkdf2 .= Format("{:02x}", NumGet(pbDKey, A_Index - 1, "uchar")) 35 | } 36 | catch exception 37 | { 38 | ; represents errors that occur during application execution 39 | throw Exception 40 | } 41 | finally 42 | { 43 | ; cleaning up resources 44 | if (pbSalt) 45 | VarSetCapacity(pbSalt, 0) 46 | if (pbPass) 47 | VarSetCapacity(pbPass, 0) 48 | if (pbDKey) 49 | VarSetCapacity(pbDKey, 0) 50 | if (hAlg) 51 | DllCall("bcrypt\BCryptCloseAlgorithmProvider", "ptr", hAlg, "uint", 0) 52 | if (hBCRYPT) 53 | DllCall("FreeLibrary", "ptr", hBCRYPT) 54 | } 55 | 56 | return pbkdf2 57 | } -------------------------------------------------------------------------------- /src/v1.1_deprecated/functions/bcrypt_sha1.ahk: -------------------------------------------------------------------------------- 1 | MsgBox % bcrypt_sha1("The quick brown fox jumps over the lazy dog") 2 | ; -> 2fd4e1c67a2d28fced849ee1bb76e7391b93eb12 3 | 4 | 5 | 6 | bcrypt_sha1(string, encoding := "utf-8") 7 | { 8 | static BCRYPT_SHA1_ALGORITHM := "SHA1" 9 | static BCRYPT_OBJECT_LENGTH := "ObjectLength" 10 | static BCRYPT_HASH_LENGTH := "HashDigestLength" 11 | 12 | try 13 | { 14 | ; loads the specified module into the address space of the calling process 15 | if !(hBCRYPT := DllCall("LoadLibrary", "str", "bcrypt.dll", "ptr")) 16 | throw Exception("Failed to load bcrypt.dll", -1) 17 | 18 | ; open an algorithm handle 19 | if (NT_STATUS := DllCall("bcrypt\BCryptOpenAlgorithmProvider", "ptr*", hAlg, "ptr", &BCRYPT_SHA1_ALGORITHM, "ptr", 0, "uint", 0) != 0) 20 | throw Exception("BCryptOpenAlgorithmProvider: " NT_STATUS, -1) 21 | 22 | ; calculate the size of the buffer to hold the hash object 23 | if (NT_STATUS := DllCall("bcrypt\BCryptGetProperty", "ptr", hAlg, "ptr", &BCRYPT_OBJECT_LENGTH, "uint*", cbHashObject, "uint", 4, "uint*", cbData, "uint", 0) != 0) 24 | throw Exception("BCryptGetProperty: " NT_STATUS, -1) 25 | 26 | ; allocate the hash object 27 | VarSetCapacity(pbHashObject, cbHashObject, 0) 28 | ; throw Exception("Memory allocation failed", -1) 29 | 30 | ; calculate the length of the hash 31 | if (NT_STATUS := DllCall("bcrypt\BCryptGetProperty", "ptr", hAlg, "ptr", &BCRYPT_HASH_LENGTH, "uint*", cbHash, "uint", 4, "uint*", cbData, "uint", 0) != 0) 32 | throw Exception("BCryptGetProperty: " NT_STATUS, -1) 33 | 34 | ; allocate the hash buffer 35 | VarSetCapacity(pbHash, cbHash, 0) 36 | ; throw Exception("Memory allocation failed", -1) 37 | 38 | ; create a hash 39 | if (NT_STATUS := DllCall("bcrypt\BCryptCreateHash", "ptr", hAlg, "ptr*", hHash, "ptr", &pbHashObject, "uint", cbHashObject, "ptr", 0, "uint", 0, "uint", 0) != 0) 40 | throw Exception("BCryptCreateHash: " NT_STATUS, -1) 41 | 42 | ; hash some data 43 | VarSetCapacity(pbInput, (StrPut(string, encoding) - 1) * ((encoding = "utf-16" || encoding = "cp1200") ? 2 : 1), 0) 44 | cbInput := (StrPut(string, &pbInput, encoding) - 1) * ((encoding = "utf-16" || encoding = "cp1200") ? 2 : 1) 45 | if (NT_STATUS := DllCall("bcrypt\BCryptHashData", "ptr", hHash, "ptr", &pbInput, "uint", cbInput, "uint", 0) != 0) 46 | throw Exception("BCryptHashData: " NT_STATUS, -1) 47 | 48 | ; close the hash 49 | if (NT_STATUS := DllCall("bcrypt\BCryptFinishHash", "ptr", hHash, "ptr", &pbHash, "uint", cbHash, "uint", 0) != 0) 50 | throw Exception("BCryptFinishHash: " NT_STATUS, -1) 51 | 52 | loop % cbHash 53 | hash .= Format("{:02x}", NumGet(pbHash, A_Index - 1, "uchar")) 54 | } 55 | catch exception 56 | { 57 | ; represents errors that occur during application execution 58 | throw Exception 59 | } 60 | finally 61 | { 62 | ; cleaning up resources 63 | if (pbInput) 64 | VarSetCapacity(pbInput, 0) 65 | if (hHash) 66 | DllCall("bcrypt\BCryptDestroyHash", "ptr", hHash) 67 | if (pbHash) 68 | VarSetCapacity(pbHash, 0) 69 | if (pbHashObject) 70 | VarSetCapacity(pbHashObject, 0) 71 | if (hAlg) 72 | DllCall("bcrypt\BCryptCloseAlgorithmProvider", "ptr", hAlg, "uint", 0) 73 | if (hBCRYPT) 74 | DllCall("FreeLibrary", "ptr", hBCRYPT) 75 | } 76 | 77 | return hash 78 | } -------------------------------------------------------------------------------- /src/v1.1_deprecated/functions/bcrypt_sha1_file.ahk: -------------------------------------------------------------------------------- 1 | v := 0x0 2 | file := FileOpen("TestFile", "rw") 3 | loop % 256 4 | file.WriteUChar(v++) 5 | file.Close() 6 | 7 | MsgBox % bcrypt_sha1_file("TestFile") 8 | ; -> 4916d6bdb7f78e6803698cab32d1586ea457dfc8 9 | 10 | FileDelete, TestFile 11 | 12 | 13 | 14 | bcrypt_sha1_file(filename, bytes := 1048576 , offset := 0, length := -1, encoding := "utf-8") 15 | { 16 | static BCRYPT_SHA1_ALGORITHM := "SHA1" 17 | static BCRYPT_OBJECT_LENGTH := "ObjectLength" 18 | static BCRYPT_HASH_LENGTH := "HashDigestLength" 19 | 20 | try 21 | { 22 | ; loads the specified module into the address space of the calling process 23 | if !(hBCRYPT := DllCall("LoadLibrary", "str", "bcrypt.dll", "ptr")) 24 | throw Exception("Failed to load bcrypt.dll", -1) 25 | 26 | ; open an algorithm handle 27 | if (NT_STATUS := DllCall("bcrypt\BCryptOpenAlgorithmProvider", "ptr*", hAlg, "ptr", &BCRYPT_SHA1_ALGORITHM, "ptr", 0, "uint", 0) != 0) 28 | throw Exception("BCryptOpenAlgorithmProvider: " NT_STATUS, -1) 29 | 30 | ; calculate the size of the buffer to hold the hash object 31 | if (NT_STATUS := DllCall("bcrypt\BCryptGetProperty", "ptr", hAlg, "ptr", &BCRYPT_OBJECT_LENGTH, "uint*", cbHashObject, "uint", 4, "uint*", cbData, "uint", 0) != 0) 32 | throw Exception("BCryptGetProperty: " NT_STATUS, -1) 33 | 34 | ; allocate the hash object 35 | VarSetCapacity(pbHashObject, cbHashObject, 0) 36 | ; throw Exception("Memory allocation failed", -1) 37 | 38 | ; calculate the length of the hash 39 | if (NT_STATUS := DllCall("bcrypt\BCryptGetProperty", "ptr", hAlg, "ptr", &BCRYPT_HASH_LENGTH, "uint*", cbHash, "uint", 4, "uint*", cbData, "uint", 0) != 0) 40 | throw Exception("BCryptGetProperty: " NT_STATUS, -1) 41 | 42 | ; allocate the hash buffer 43 | VarSetCapacity(pbHash, cbHash, 0) 44 | ; throw Exception("Memory allocation failed", -1) 45 | 46 | ; create a hash 47 | if (NT_STATUS := DllCall("bcrypt\BCryptCreateHash", "ptr", hAlg, "ptr*", hHash, "ptr", &pbHashObject, "uint", cbHashObject, "ptr", 0, "uint", 0, "uint", 0) != 0) 48 | throw Exception("BCryptCreateHash: " NT_STATUS, -1) 49 | 50 | ; create a hash 51 | if !(IsObject(f := FileOpen(filename, "r", encoding))) 52 | throw Exception("Failed to open file: " filename, -1) 53 | length := length < 0 ? f.length - offset : length 54 | if ((offset + length) > f.length) 55 | throw Exception("Invalid parameters offset / length!", -1) 56 | f.Pos(offset) 57 | while (length > bytes) && (dataread := f.RawRead(data, bytes)) 58 | { 59 | if (NT_STATUS := DllCall("bcrypt\BCryptHashData", "ptr", hHash, "ptr", &data, "uint", dataread, "uint", 0) != 0) 60 | throw Exception("BCryptHashData: " NT_STATUS, -1) 61 | length -= dataread 62 | } 63 | if (length > 0) 64 | { 65 | if (dataread := f.RawRead(data, length)) 66 | if (NT_STATUS := DllCall("bcrypt\BCryptHashData", "ptr", hHash, "ptr", &data, "uint", dataread, "uint", 0) != 0) 67 | throw Exception("BCryptHashData: " NT_STATUS, -1) 68 | } 69 | 70 | ; close the hash 71 | if (NT_STATUS := DllCall("bcrypt\BCryptFinishHash", "ptr", hHash, "ptr", &pbHash, "uint", cbHash, "uint", 0) != 0) 72 | throw Exception("BCryptFinishHash: " NT_STATUS, -1) 73 | 74 | loop % cbHash 75 | hash .= Format("{:02x}", NumGet(pbHash, A_Index - 1, "uchar")) 76 | } 77 | catch exception 78 | { 79 | ; represents errors that occur during application execution 80 | throw Exception 81 | } 82 | finally 83 | { 84 | ; cleaning up resources 85 | if (f) 86 | f.Close() 87 | if (pbInput) 88 | VarSetCapacity(pbInput, 0) 89 | if (hHash) 90 | DllCall("bcrypt\BCryptDestroyHash", "ptr", hHash) 91 | if (pbHash) 92 | VarSetCapacity(pbHash, 0) 93 | if (pbHashObject) 94 | VarSetCapacity(pbHashObject, 0) 95 | if (hAlg) 96 | DllCall("bcrypt\BCryptCloseAlgorithmProvider", "ptr", hAlg, "uint", 0) 97 | if (hBCRYPT) 98 | DllCall("FreeLibrary", "ptr", hBCRYPT) 99 | } 100 | 101 | return hash 102 | } -------------------------------------------------------------------------------- /src/v1.1_deprecated/functions/bcrypt_sha1_hmac.ahk: -------------------------------------------------------------------------------- 1 | MsgBox % bcrypt_sha1_hmac("The quick brown fox jumps over the lazy dog", "Secret Salt") 2 | ; -> d736602b0b10855afb5b0699232200a2284d9661 3 | 4 | 5 | 6 | bcrypt_sha1_hmac(string, hmac, encoding := "utf-8") 7 | { 8 | static BCRYPT_SHA1_ALGORITHM := "SHA1" 9 | static BCRYPT_ALG_HANDLE_HMAC_FLAG := 0x00000008 10 | static BCRYPT_OBJECT_LENGTH := "ObjectLength" 11 | static BCRYPT_HASH_LENGTH := "HashDigestLength" 12 | 13 | try 14 | { 15 | ; loads the specified module into the address space of the calling process 16 | if !(hBCRYPT := DllCall("LoadLibrary", "str", "bcrypt.dll", "ptr")) 17 | throw Exception("Failed to load bcrypt.dll", -1) 18 | 19 | ; open an algorithm handle 20 | if (NT_STATUS := DllCall("bcrypt\BCryptOpenAlgorithmProvider", "ptr*", hAlg, "ptr", &BCRYPT_SHA1_ALGORITHM, "ptr", 0, "uint", BCRYPT_ALG_HANDLE_HMAC_FLAG) != 0) 21 | throw Exception("BCryptOpenAlgorithmProvider: " NT_STATUS, -1) 22 | 23 | ; calculate the size of the buffer to hold the hash object 24 | if (NT_STATUS := DllCall("bcrypt\BCryptGetProperty", "ptr", hAlg, "ptr", &BCRYPT_OBJECT_LENGTH, "uint*", cbHashObject, "uint", 4, "uint*", cbData, "uint", 0) != 0) 25 | throw Exception("BCryptGetProperty: " NT_STATUS, -1) 26 | 27 | ; allocate the hash object 28 | VarSetCapacity(pbHashObject, cbHashObject, 0) 29 | ; throw Exception("Memory allocation failed", -1) 30 | 31 | ; calculate the length of the hash 32 | if (NT_STATUS := DllCall("bcrypt\BCryptGetProperty", "ptr", hAlg, "ptr", &BCRYPT_HASH_LENGTH, "uint*", cbHash, "uint", 4, "uint*", cbData, "uint", 0) != 0) 33 | throw Exception("BCryptGetProperty: " NT_STATUS, -1) 34 | 35 | ; allocate the hash buffer 36 | VarSetCapacity(pbHash, cbHash, 0) 37 | ; throw Exception("Memory allocation failed", -1) 38 | 39 | ; create a hash 40 | VarSetCapacity(pbSecret, (StrPut(hmac, encoding) - 1) * ((encoding = "utf-16" || encoding = "cp1200") ? 2 : 1), 0) 41 | cbSecret := (StrPut(hmac, &pbSecret, encoding) - 1) * ((encoding = "utf-16" || encoding = "cp1200") ? 2 : 1) 42 | if (NT_STATUS := DllCall("bcrypt\BCryptCreateHash", "ptr", hAlg, "ptr*", hHash, "ptr", &pbHashObject, "uint", cbHashObject, "ptr", &pbSecret, "uint", cbSecret, "uint", 0) != 0) 43 | throw Exception("BCryptCreateHash: " NT_STATUS, -1) 44 | 45 | ; hash some data 46 | VarSetCapacity(pbInput, (StrPut(string, encoding) - 1) * ((encoding = "utf-16" || encoding = "cp1200") ? 2 : 1), 0) 47 | cbInput := (StrPut(string, &pbInput, encoding) - 1) * ((encoding = "utf-16" || encoding = "cp1200") ? 2 : 1) 48 | if (NT_STATUS := DllCall("bcrypt\BCryptHashData", "ptr", hHash, "ptr", &pbInput, "uint", cbInput, "uint", 0) != 0) 49 | throw Exception("BCryptHashData: " NT_STATUS, -1) 50 | 51 | ; close the hash 52 | if (NT_STATUS := DllCall("bcrypt\BCryptFinishHash", "ptr", hHash, "ptr", &pbHash, "uint", cbHash, "uint", 0) != 0) 53 | throw Exception("BCryptFinishHash: " NT_STATUS, -1) 54 | 55 | loop % cbHash 56 | hash .= Format("{:02x}", NumGet(pbHash, A_Index - 1, "uchar")) 57 | } 58 | catch exception 59 | { 60 | ; represents errors that occur during application execution 61 | throw Exception 62 | } 63 | finally 64 | { 65 | ; cleaning up resources 66 | if (pbInput) 67 | VarSetCapacity(pbInput, 0) 68 | if (hHash) 69 | DllCall("bcrypt\BCryptDestroyHash", "ptr", hHash) 70 | if (pbHash) 71 | VarSetCapacity(pbHash, 0) 72 | if (pbHashObject) 73 | VarSetCapacity(pbHashObject, 0) 74 | if (hAlg) 75 | DllCall("bcrypt\BCryptCloseAlgorithmProvider", "ptr", hAlg, "uint", 0) 76 | if (hBCRYPT) 77 | DllCall("FreeLibrary", "ptr", hBCRYPT) 78 | } 79 | 80 | return hash 81 | } -------------------------------------------------------------------------------- /src/v1.1_deprecated/functions/bcrypt_sha256.ahk: -------------------------------------------------------------------------------- 1 | MsgBox % bcrypt_sha256("The quick brown fox jumps over the lazy dog") 2 | ; -> d7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592 3 | 4 | 5 | 6 | bcrypt_sha256(string, encoding := "utf-8") 7 | { 8 | static BCRYPT_SHA256_ALGORITHM := "SHA256" 9 | static BCRYPT_OBJECT_LENGTH := "ObjectLength" 10 | static BCRYPT_HASH_LENGTH := "HashDigestLength" 11 | 12 | try 13 | { 14 | ; loads the specified module into the address space of the calling process 15 | if !(hBCRYPT := DllCall("LoadLibrary", "str", "bcrypt.dll", "ptr")) 16 | throw Exception("Failed to load bcrypt.dll", -1) 17 | 18 | ; open an algorithm handle 19 | if (NT_STATUS := DllCall("bcrypt\BCryptOpenAlgorithmProvider", "ptr*", hAlg, "ptr", &BCRYPT_SHA256_ALGORITHM, "ptr", 0, "uint", 0) != 0) 20 | throw Exception("BCryptOpenAlgorithmProvider: " NT_STATUS, -1) 21 | 22 | ; calculate the size of the buffer to hold the hash object 23 | if (NT_STATUS := DllCall("bcrypt\BCryptGetProperty", "ptr", hAlg, "ptr", &BCRYPT_OBJECT_LENGTH, "uint*", cbHashObject, "uint", 4, "uint*", cbData, "uint", 0) != 0) 24 | throw Exception("BCryptGetProperty: " NT_STATUS, -1) 25 | 26 | ; allocate the hash object 27 | VarSetCapacity(pbHashObject, cbHashObject, 0) 28 | ; throw Exception("Memory allocation failed", -1) 29 | 30 | ; calculate the length of the hash 31 | if (NT_STATUS := DllCall("bcrypt\BCryptGetProperty", "ptr", hAlg, "ptr", &BCRYPT_HASH_LENGTH, "uint*", cbHash, "uint", 4, "uint*", cbData, "uint", 0) != 0) 32 | throw Exception("BCryptGetProperty: " NT_STATUS, -1) 33 | 34 | ; allocate the hash buffer 35 | VarSetCapacity(pbHash, cbHash, 0) 36 | ; throw Exception("Memory allocation failed", -1) 37 | 38 | ; create a hash 39 | if (NT_STATUS := DllCall("bcrypt\BCryptCreateHash", "ptr", hAlg, "ptr*", hHash, "ptr", &pbHashObject, "uint", cbHashObject, "ptr", 0, "uint", 0, "uint", 0) != 0) 40 | throw Exception("BCryptCreateHash: " NT_STATUS, -1) 41 | 42 | ; hash some data 43 | VarSetCapacity(pbInput, (StrPut(string, encoding) - 1) * ((encoding = "utf-16" || encoding = "cp1200") ? 2 : 1), 0) 44 | cbInput := (StrPut(string, &pbInput, encoding) - 1) * ((encoding = "utf-16" || encoding = "cp1200") ? 2 : 1) 45 | if (NT_STATUS := DllCall("bcrypt\BCryptHashData", "ptr", hHash, "ptr", &pbInput, "uint", cbInput, "uint", 0) != 0) 46 | throw Exception("BCryptHashData: " NT_STATUS, -1) 47 | 48 | ; close the hash 49 | if (NT_STATUS := DllCall("bcrypt\BCryptFinishHash", "ptr", hHash, "ptr", &pbHash, "uint", cbHash, "uint", 0) != 0) 50 | throw Exception("BCryptFinishHash: " NT_STATUS, -1) 51 | 52 | loop % cbHash 53 | hash .= Format("{:02x}", NumGet(pbHash, A_Index - 1, "uchar")) 54 | } 55 | catch exception 56 | { 57 | ; represents errors that occur during application execution 58 | throw Exception 59 | } 60 | finally 61 | { 62 | ; cleaning up resources 63 | if (pbInput) 64 | VarSetCapacity(pbInput, 0) 65 | if (hHash) 66 | DllCall("bcrypt\BCryptDestroyHash", "ptr", hHash) 67 | if (pbHash) 68 | VarSetCapacity(pbHash, 0) 69 | if (pbHashObject) 70 | VarSetCapacity(pbHashObject, 0) 71 | if (hAlg) 72 | DllCall("bcrypt\BCryptCloseAlgorithmProvider", "ptr", hAlg, "uint", 0) 73 | if (hBCRYPT) 74 | DllCall("FreeLibrary", "ptr", hBCRYPT) 75 | } 76 | 77 | return hash 78 | } -------------------------------------------------------------------------------- /src/v1.1_deprecated/functions/bcrypt_sha256_file.ahk: -------------------------------------------------------------------------------- 1 | v := 0x0 2 | file := FileOpen("TestFile", "rw") 3 | loop % 256 4 | file.WriteUChar(v++) 5 | file.Close() 6 | 7 | MsgBox % bcrypt_sha256_file("TestFile") 8 | ; -> 40aff2e9d2d8922e47afd4648e6967497158785fbd1da870e7110266bf944880 9 | 10 | FileDelete, TestFile 11 | 12 | 13 | 14 | bcrypt_sha256_file(filename, bytes := 1048576 , offset := 0, length := -1, encoding := "utf-8") 15 | { 16 | static BCRYPT_SHA256_ALGORITHM := "SHA256" 17 | static BCRYPT_OBJECT_LENGTH := "ObjectLength" 18 | static BCRYPT_HASH_LENGTH := "HashDigestLength" 19 | 20 | try 21 | { 22 | ; loads the specified module into the address space of the calling process 23 | if !(hBCRYPT := DllCall("LoadLibrary", "str", "bcrypt.dll", "ptr")) 24 | throw Exception("Failed to load bcrypt.dll", -1) 25 | 26 | ; open an algorithm handle 27 | if (NT_STATUS := DllCall("bcrypt\BCryptOpenAlgorithmProvider", "ptr*", hAlg, "ptr", &BCRYPT_SHA256_ALGORITHM, "ptr", 0, "uint", 0) != 0) 28 | throw Exception("BCryptOpenAlgorithmProvider: " NT_STATUS, -1) 29 | 30 | ; calculate the size of the buffer to hold the hash object 31 | if (NT_STATUS := DllCall("bcrypt\BCryptGetProperty", "ptr", hAlg, "ptr", &BCRYPT_OBJECT_LENGTH, "uint*", cbHashObject, "uint", 4, "uint*", cbData, "uint", 0) != 0) 32 | throw Exception("BCryptGetProperty: " NT_STATUS, -1) 33 | 34 | ; allocate the hash object 35 | VarSetCapacity(pbHashObject, cbHashObject, 0) 36 | ; throw Exception("Memory allocation failed", -1) 37 | 38 | ; calculate the length of the hash 39 | if (NT_STATUS := DllCall("bcrypt\BCryptGetProperty", "ptr", hAlg, "ptr", &BCRYPT_HASH_LENGTH, "uint*", cbHash, "uint", 4, "uint*", cbData, "uint", 0) != 0) 40 | throw Exception("BCryptGetProperty: " NT_STATUS, -1) 41 | 42 | ; allocate the hash buffer 43 | VarSetCapacity(pbHash, cbHash, 0) 44 | ; throw Exception("Memory allocation failed", -1) 45 | 46 | ; create a hash 47 | if (NT_STATUS := DllCall("bcrypt\BCryptCreateHash", "ptr", hAlg, "ptr*", hHash, "ptr", &pbHashObject, "uint", cbHashObject, "ptr", 0, "uint", 0, "uint", 0) != 0) 48 | throw Exception("BCryptCreateHash: " NT_STATUS, -1) 49 | 50 | ; create a hash 51 | if !(IsObject(f := FileOpen(filename, "r", encoding))) 52 | throw Exception("Failed to open file: " filename, -1) 53 | length := length < 0 ? f.length - offset : length 54 | if ((offset + length) > f.length) 55 | throw Exception("Invalid parameters offset / length!", -1) 56 | f.Pos(offset) 57 | while (length > bytes) && (dataread := f.RawRead(data, bytes)) 58 | { 59 | if (NT_STATUS := DllCall("bcrypt\BCryptHashData", "ptr", hHash, "ptr", &data, "uint", dataread, "uint", 0) != 0) 60 | throw Exception("BCryptHashData: " NT_STATUS, -1) 61 | length -= dataread 62 | } 63 | if (length > 0) 64 | { 65 | if (dataread := f.RawRead(data, length)) 66 | if (NT_STATUS := DllCall("bcrypt\BCryptHashData", "ptr", hHash, "ptr", &data, "uint", dataread, "uint", 0) != 0) 67 | throw Exception("BCryptHashData: " NT_STATUS, -1) 68 | } 69 | 70 | ; close the hash 71 | if (NT_STATUS := DllCall("bcrypt\BCryptFinishHash", "ptr", hHash, "ptr", &pbHash, "uint", cbHash, "uint", 0) != 0) 72 | throw Exception("BCryptFinishHash: " NT_STATUS, -1) 73 | 74 | loop % cbHash 75 | hash .= Format("{:02x}", NumGet(pbHash, A_Index - 1, "uchar")) 76 | } 77 | catch exception 78 | { 79 | ; represents errors that occur during application execution 80 | throw Exception 81 | } 82 | finally 83 | { 84 | ; cleaning up resources 85 | if (f) 86 | f.Close() 87 | if (pbInput) 88 | VarSetCapacity(pbInput, 0) 89 | if (hHash) 90 | DllCall("bcrypt\BCryptDestroyHash", "ptr", hHash) 91 | if (pbHash) 92 | VarSetCapacity(pbHash, 0) 93 | if (pbHashObject) 94 | VarSetCapacity(pbHashObject, 0) 95 | if (hAlg) 96 | DllCall("bcrypt\BCryptCloseAlgorithmProvider", "ptr", hAlg, "uint", 0) 97 | if (hBCRYPT) 98 | DllCall("FreeLibrary", "ptr", hBCRYPT) 99 | } 100 | 101 | return hash 102 | } -------------------------------------------------------------------------------- /src/v1.1_deprecated/functions/bcrypt_sha256_hmac.ahk: -------------------------------------------------------------------------------- 1 | MsgBox % bcrypt_sha256_hmac("The quick brown fox jumps over the lazy dog", "Secret Salt") 2 | ; -> 68dba4b3a6d5c36b6e3567e1a925fe87c7386162e8fb6e2e9f17ade4aa7dc262 3 | 4 | 5 | 6 | bcrypt_sha256_hmac(string, hmac, encoding := "utf-8") 7 | { 8 | static BCRYPT_SHA256_ALGORITHM := "SHA256" 9 | static BCRYPT_ALG_HANDLE_HMAC_FLAG := 0x00000008 10 | static BCRYPT_OBJECT_LENGTH := "ObjectLength" 11 | static BCRYPT_HASH_LENGTH := "HashDigestLength" 12 | 13 | try 14 | { 15 | ; loads the specified module into the address space of the calling process 16 | if !(hBCRYPT := DllCall("LoadLibrary", "str", "bcrypt.dll", "ptr")) 17 | throw Exception("Failed to load bcrypt.dll", -1) 18 | 19 | ; open an algorithm handle 20 | if (NT_STATUS := DllCall("bcrypt\BCryptOpenAlgorithmProvider", "ptr*", hAlg, "ptr", &BCRYPT_SHA256_ALGORITHM, "ptr", 0, "uint", BCRYPT_ALG_HANDLE_HMAC_FLAG) != 0) 21 | throw Exception("BCryptOpenAlgorithmProvider: " NT_STATUS, -1) 22 | 23 | ; calculate the size of the buffer to hold the hash object 24 | if (NT_STATUS := DllCall("bcrypt\BCryptGetProperty", "ptr", hAlg, "ptr", &BCRYPT_OBJECT_LENGTH, "uint*", cbHashObject, "uint", 4, "uint*", cbData, "uint", 0) != 0) 25 | throw Exception("BCryptGetProperty: " NT_STATUS, -1) 26 | 27 | ; allocate the hash object 28 | VarSetCapacity(pbHashObject, cbHashObject, 0) 29 | ; throw Exception("Memory allocation failed", -1) 30 | 31 | ; calculate the length of the hash 32 | if (NT_STATUS := DllCall("bcrypt\BCryptGetProperty", "ptr", hAlg, "ptr", &BCRYPT_HASH_LENGTH, "uint*", cbHash, "uint", 4, "uint*", cbData, "uint", 0) != 0) 33 | throw Exception("BCryptGetProperty: " NT_STATUS, -1) 34 | 35 | ; allocate the hash buffer 36 | VarSetCapacity(pbHash, cbHash, 0) 37 | ; throw Exception("Memory allocation failed", -1) 38 | 39 | ; create a hash 40 | VarSetCapacity(pbSecret, (StrPut(hmac, encoding) - 1) * ((encoding = "utf-16" || encoding = "cp1200") ? 2 : 1), 0) 41 | cbSecret := (StrPut(hmac, &pbSecret, encoding) - 1) * ((encoding = "utf-16" || encoding = "cp1200") ? 2 : 1) 42 | if (NT_STATUS := DllCall("bcrypt\BCryptCreateHash", "ptr", hAlg, "ptr*", hHash, "ptr", &pbHashObject, "uint", cbHashObject, "ptr", &pbSecret, "uint", cbSecret, "uint", 0) != 0) 43 | throw Exception("BCryptCreateHash: " NT_STATUS, -1) 44 | 45 | ; hash some data 46 | VarSetCapacity(pbInput, (StrPut(string, encoding) - 1) * ((encoding = "utf-16" || encoding = "cp1200") ? 2 : 1), 0) 47 | cbInput := (StrPut(string, &pbInput, encoding) - 1) * ((encoding = "utf-16" || encoding = "cp1200") ? 2 : 1) 48 | if (NT_STATUS := DllCall("bcrypt\BCryptHashData", "ptr", hHash, "ptr", &pbInput, "uint", cbInput, "uint", 0) != 0) 49 | throw Exception("BCryptHashData: " NT_STATUS, -1) 50 | 51 | ; close the hash 52 | if (NT_STATUS := DllCall("bcrypt\BCryptFinishHash", "ptr", hHash, "ptr", &pbHash, "uint", cbHash, "uint", 0) != 0) 53 | throw Exception("BCryptFinishHash: " NT_STATUS, -1) 54 | 55 | loop % cbHash 56 | hash .= Format("{:02x}", NumGet(pbHash, A_Index - 1, "uchar")) 57 | } 58 | catch exception 59 | { 60 | ; represents errors that occur during application execution 61 | throw Exception 62 | } 63 | finally 64 | { 65 | ; cleaning up resources 66 | if (pbInput) 67 | VarSetCapacity(pbInput, 0) 68 | if (hHash) 69 | DllCall("bcrypt\BCryptDestroyHash", "ptr", hHash) 70 | if (pbHash) 71 | VarSetCapacity(pbHash, 0) 72 | if (pbHashObject) 73 | VarSetCapacity(pbHashObject, 0) 74 | if (hAlg) 75 | DllCall("bcrypt\BCryptCloseAlgorithmProvider", "ptr", hAlg, "uint", 0) 76 | if (hBCRYPT) 77 | DllCall("FreeLibrary", "ptr", hBCRYPT) 78 | } 79 | 80 | return hash 81 | } -------------------------------------------------------------------------------- /src/v1.1_deprecated/functions/bcrypt_sha384.ahk: -------------------------------------------------------------------------------- 1 | MsgBox % bcrypt_sha384("The quick brown fox jumps over the lazy dog") 2 | ; -> ca737f1014a48f4c0b6dd43cb177b0afd9e5169367544c494011e3317dbf9a509cb1e5dc1e85a941bbee3d7f2afbc9b1 3 | 4 | 5 | 6 | bcrypt_sha384(string, encoding := "utf-8") 7 | { 8 | static BCRYPT_SHA384_ALGORITHM := "SHA384" 9 | static BCRYPT_OBJECT_LENGTH := "ObjectLength" 10 | static BCRYPT_HASH_LENGTH := "HashDigestLength" 11 | 12 | try 13 | { 14 | ; loads the specified module into the address space of the calling process 15 | if !(hBCRYPT := DllCall("LoadLibrary", "str", "bcrypt.dll", "ptr")) 16 | throw Exception("Failed to load bcrypt.dll", -1) 17 | 18 | ; open an algorithm handle 19 | if (NT_STATUS := DllCall("bcrypt\BCryptOpenAlgorithmProvider", "ptr*", hAlg, "ptr", &BCRYPT_SHA384_ALGORITHM, "ptr", 0, "uint", 0) != 0) 20 | throw Exception("BCryptOpenAlgorithmProvider: " NT_STATUS, -1) 21 | 22 | ; calculate the size of the buffer to hold the hash object 23 | if (NT_STATUS := DllCall("bcrypt\BCryptGetProperty", "ptr", hAlg, "ptr", &BCRYPT_OBJECT_LENGTH, "uint*", cbHashObject, "uint", 4, "uint*", cbData, "uint", 0) != 0) 24 | throw Exception("BCryptGetProperty: " NT_STATUS, -1) 25 | 26 | ; allocate the hash object 27 | VarSetCapacity(pbHashObject, cbHashObject, 0) 28 | ; throw Exception("Memory allocation failed", -1) 29 | 30 | ; calculate the length of the hash 31 | if (NT_STATUS := DllCall("bcrypt\BCryptGetProperty", "ptr", hAlg, "ptr", &BCRYPT_HASH_LENGTH, "uint*", cbHash, "uint", 4, "uint*", cbData, "uint", 0) != 0) 32 | throw Exception("BCryptGetProperty: " NT_STATUS, -1) 33 | 34 | ; allocate the hash buffer 35 | VarSetCapacity(pbHash, cbHash, 0) 36 | ; throw Exception("Memory allocation failed", -1) 37 | 38 | ; create a hash 39 | if (NT_STATUS := DllCall("bcrypt\BCryptCreateHash", "ptr", hAlg, "ptr*", hHash, "ptr", &pbHashObject, "uint", cbHashObject, "ptr", 0, "uint", 0, "uint", 0) != 0) 40 | throw Exception("BCryptCreateHash: " NT_STATUS, -1) 41 | 42 | ; hash some data 43 | VarSetCapacity(pbInput, (StrPut(string, encoding) - 1) * ((encoding = "utf-16" || encoding = "cp1200") ? 2 : 1), 0) 44 | cbInput := (StrPut(string, &pbInput, encoding) - 1) * ((encoding = "utf-16" || encoding = "cp1200") ? 2 : 1) 45 | if (NT_STATUS := DllCall("bcrypt\BCryptHashData", "ptr", hHash, "ptr", &pbInput, "uint", cbInput, "uint", 0) != 0) 46 | throw Exception("BCryptHashData: " NT_STATUS, -1) 47 | 48 | ; close the hash 49 | if (NT_STATUS := DllCall("bcrypt\BCryptFinishHash", "ptr", hHash, "ptr", &pbHash, "uint", cbHash, "uint", 0) != 0) 50 | throw Exception("BCryptFinishHash: " NT_STATUS, -1) 51 | 52 | loop % cbHash 53 | hash .= Format("{:02x}", NumGet(pbHash, A_Index - 1, "uchar")) 54 | } 55 | catch exception 56 | { 57 | ; represents errors that occur during application execution 58 | throw Exception 59 | } 60 | finally 61 | { 62 | ; cleaning up resources 63 | if (pbInput) 64 | VarSetCapacity(pbInput, 0) 65 | if (hHash) 66 | DllCall("bcrypt\BCryptDestroyHash", "ptr", hHash) 67 | if (pbHash) 68 | VarSetCapacity(pbHash, 0) 69 | if (pbHashObject) 70 | VarSetCapacity(pbHashObject, 0) 71 | if (hAlg) 72 | DllCall("bcrypt\BCryptCloseAlgorithmProvider", "ptr", hAlg, "uint", 0) 73 | if (hBCRYPT) 74 | DllCall("FreeLibrary", "ptr", hBCRYPT) 75 | } 76 | 77 | return hash 78 | } -------------------------------------------------------------------------------- /src/v1.1_deprecated/functions/bcrypt_sha384_file.ahk: -------------------------------------------------------------------------------- 1 | v := 0x0 2 | file := FileOpen("TestFile", "rw") 3 | loop % 256 4 | file.WriteUChar(v++) 5 | file.Close() 6 | 7 | MsgBox % bcrypt_sha384_file("TestFile") 8 | ; -> ffdaebff65ed05cf400f0221c4ccfb4b2104fb6a51f87e40be6c4309386bfdec2892e9179b34632331a59592737db5c5 9 | 10 | FileDelete, TestFile 11 | 12 | 13 | 14 | bcrypt_sha384_file(filename, bytes := 1048576 , offset := 0, length := -1, encoding := "utf-8") 15 | { 16 | static BCRYPT_SHA384_ALGORITHM := "SHA384" 17 | static BCRYPT_OBJECT_LENGTH := "ObjectLength" 18 | static BCRYPT_HASH_LENGTH := "HashDigestLength" 19 | 20 | try 21 | { 22 | ; loads the specified module into the address space of the calling process 23 | if !(hBCRYPT := DllCall("LoadLibrary", "str", "bcrypt.dll", "ptr")) 24 | throw Exception("Failed to load bcrypt.dll", -1) 25 | 26 | ; open an algorithm handle 27 | if (NT_STATUS := DllCall("bcrypt\BCryptOpenAlgorithmProvider", "ptr*", hAlg, "ptr", &BCRYPT_SHA384_ALGORITHM, "ptr", 0, "uint", 0) != 0) 28 | throw Exception("BCryptOpenAlgorithmProvider: " NT_STATUS, -1) 29 | 30 | ; calculate the size of the buffer to hold the hash object 31 | if (NT_STATUS := DllCall("bcrypt\BCryptGetProperty", "ptr", hAlg, "ptr", &BCRYPT_OBJECT_LENGTH, "uint*", cbHashObject, "uint", 4, "uint*", cbData, "uint", 0) != 0) 32 | throw Exception("BCryptGetProperty: " NT_STATUS, -1) 33 | 34 | ; allocate the hash object 35 | VarSetCapacity(pbHashObject, cbHashObject, 0) 36 | ; throw Exception("Memory allocation failed", -1) 37 | 38 | ; calculate the length of the hash 39 | if (NT_STATUS := DllCall("bcrypt\BCryptGetProperty", "ptr", hAlg, "ptr", &BCRYPT_HASH_LENGTH, "uint*", cbHash, "uint", 4, "uint*", cbData, "uint", 0) != 0) 40 | throw Exception("BCryptGetProperty: " NT_STATUS, -1) 41 | 42 | ; allocate the hash buffer 43 | VarSetCapacity(pbHash, cbHash, 0) 44 | ; throw Exception("Memory allocation failed", -1) 45 | 46 | ; create a hash 47 | if (NT_STATUS := DllCall("bcrypt\BCryptCreateHash", "ptr", hAlg, "ptr*", hHash, "ptr", &pbHashObject, "uint", cbHashObject, "ptr", 0, "uint", 0, "uint", 0) != 0) 48 | throw Exception("BCryptCreateHash: " NT_STATUS, -1) 49 | 50 | ; create a hash 51 | if !(IsObject(f := FileOpen(filename, "r", encoding))) 52 | throw Exception("Failed to open file: " filename, -1) 53 | length := length < 0 ? f.length - offset : length 54 | if ((offset + length) > f.length) 55 | throw Exception("Invalid parameters offset / length!", -1) 56 | f.Pos(offset) 57 | while (length > bytes) && (dataread := f.RawRead(data, bytes)) 58 | { 59 | if (NT_STATUS := DllCall("bcrypt\BCryptHashData", "ptr", hHash, "ptr", &data, "uint", dataread, "uint", 0) != 0) 60 | throw Exception("BCryptHashData: " NT_STATUS, -1) 61 | length -= dataread 62 | } 63 | if (length > 0) 64 | { 65 | if (dataread := f.RawRead(data, length)) 66 | if (NT_STATUS := DllCall("bcrypt\BCryptHashData", "ptr", hHash, "ptr", &data, "uint", dataread, "uint", 0) != 0) 67 | throw Exception("BCryptHashData: " NT_STATUS, -1) 68 | } 69 | 70 | ; close the hash 71 | if (NT_STATUS := DllCall("bcrypt\BCryptFinishHash", "ptr", hHash, "ptr", &pbHash, "uint", cbHash, "uint", 0) != 0) 72 | throw Exception("BCryptFinishHash: " NT_STATUS, -1) 73 | 74 | loop % cbHash 75 | hash .= Format("{:02x}", NumGet(pbHash, A_Index - 1, "uchar")) 76 | } 77 | catch exception 78 | { 79 | ; represents errors that occur during application execution 80 | throw Exception 81 | } 82 | finally 83 | { 84 | ; cleaning up resources 85 | if (f) 86 | f.Close() 87 | if (pbInput) 88 | VarSetCapacity(pbInput, 0) 89 | if (hHash) 90 | DllCall("bcrypt\BCryptDestroyHash", "ptr", hHash) 91 | if (pbHash) 92 | VarSetCapacity(pbHash, 0) 93 | if (pbHashObject) 94 | VarSetCapacity(pbHashObject, 0) 95 | if (hAlg) 96 | DllCall("bcrypt\BCryptCloseAlgorithmProvider", "ptr", hAlg, "uint", 0) 97 | if (hBCRYPT) 98 | DllCall("FreeLibrary", "ptr", hBCRYPT) 99 | } 100 | 101 | return hash 102 | } -------------------------------------------------------------------------------- /src/v1.1_deprecated/functions/bcrypt_sha384_hmac.ahk: -------------------------------------------------------------------------------- 1 | MsgBox % bcrypt_sha384_hmac("The quick brown fox jumps over the lazy dog", "Secret Salt") 2 | ; -> d91c0d4c3a6c50239354340a89eee6688e1e8f7d760d619bac0f53dd5b5e9ec0cac437d10f7e143e3bba183970850fae 3 | 4 | 5 | 6 | bcrypt_sha384_hmac(string, hmac, encoding := "utf-8") 7 | { 8 | static BCRYPT_SHA384_ALGORITHM := "SHA384" 9 | static BCRYPT_ALG_HANDLE_HMAC_FLAG := 0x00000008 10 | static BCRYPT_OBJECT_LENGTH := "ObjectLength" 11 | static BCRYPT_HASH_LENGTH := "HashDigestLength" 12 | 13 | try 14 | { 15 | ; loads the specified module into the address space of the calling process 16 | if !(hBCRYPT := DllCall("LoadLibrary", "str", "bcrypt.dll", "ptr")) 17 | throw Exception("Failed to load bcrypt.dll", -1) 18 | 19 | ; open an algorithm handle 20 | if (NT_STATUS := DllCall("bcrypt\BCryptOpenAlgorithmProvider", "ptr*", hAlg, "ptr", &BCRYPT_SHA384_ALGORITHM, "ptr", 0, "uint", BCRYPT_ALG_HANDLE_HMAC_FLAG) != 0) 21 | throw Exception("BCryptOpenAlgorithmProvider: " NT_STATUS, -1) 22 | 23 | ; calculate the size of the buffer to hold the hash object 24 | if (NT_STATUS := DllCall("bcrypt\BCryptGetProperty", "ptr", hAlg, "ptr", &BCRYPT_OBJECT_LENGTH, "uint*", cbHashObject, "uint", 4, "uint*", cbData, "uint", 0) != 0) 25 | throw Exception("BCryptGetProperty: " NT_STATUS, -1) 26 | 27 | ; allocate the hash object 28 | VarSetCapacity(pbHashObject, cbHashObject, 0) 29 | ; throw Exception("Memory allocation failed", -1) 30 | 31 | ; calculate the length of the hash 32 | if (NT_STATUS := DllCall("bcrypt\BCryptGetProperty", "ptr", hAlg, "ptr", &BCRYPT_HASH_LENGTH, "uint*", cbHash, "uint", 4, "uint*", cbData, "uint", 0) != 0) 33 | throw Exception("BCryptGetProperty: " NT_STATUS, -1) 34 | 35 | ; allocate the hash buffer 36 | VarSetCapacity(pbHash, cbHash, 0) 37 | ; throw Exception("Memory allocation failed", -1) 38 | 39 | ; create a hash 40 | VarSetCapacity(pbSecret, (StrPut(hmac, encoding) - 1) * ((encoding = "utf-16" || encoding = "cp1200") ? 2 : 1), 0) 41 | cbSecret := (StrPut(hmac, &pbSecret, encoding) - 1) * ((encoding = "utf-16" || encoding = "cp1200") ? 2 : 1) 42 | if (NT_STATUS := DllCall("bcrypt\BCryptCreateHash", "ptr", hAlg, "ptr*", hHash, "ptr", &pbHashObject, "uint", cbHashObject, "ptr", &pbSecret, "uint", cbSecret, "uint", 0) != 0) 43 | throw Exception("BCryptCreateHash: " NT_STATUS, -1) 44 | 45 | ; hash some data 46 | VarSetCapacity(pbInput, (StrPut(string, encoding) - 1) * ((encoding = "utf-16" || encoding = "cp1200") ? 2 : 1), 0) 47 | cbInput := (StrPut(string, &pbInput, encoding) - 1) * ((encoding = "utf-16" || encoding = "cp1200") ? 2 : 1) 48 | if (NT_STATUS := DllCall("bcrypt\BCryptHashData", "ptr", hHash, "ptr", &pbInput, "uint", cbInput, "uint", 0) != 0) 49 | throw Exception("BCryptHashData: " NT_STATUS, -1) 50 | 51 | ; close the hash 52 | if (NT_STATUS := DllCall("bcrypt\BCryptFinishHash", "ptr", hHash, "ptr", &pbHash, "uint", cbHash, "uint", 0) != 0) 53 | throw Exception("BCryptFinishHash: " NT_STATUS, -1) 54 | 55 | loop % cbHash 56 | hash .= Format("{:02x}", NumGet(pbHash, A_Index - 1, "uchar")) 57 | } 58 | catch exception 59 | { 60 | ; represents errors that occur during application execution 61 | throw Exception 62 | } 63 | finally 64 | { 65 | ; cleaning up resources 66 | if (pbInput) 67 | VarSetCapacity(pbInput, 0) 68 | if (hHash) 69 | DllCall("bcrypt\BCryptDestroyHash", "ptr", hHash) 70 | if (pbHash) 71 | VarSetCapacity(pbHash, 0) 72 | if (pbHashObject) 73 | VarSetCapacity(pbHashObject, 0) 74 | if (hAlg) 75 | DllCall("bcrypt\BCryptCloseAlgorithmProvider", "ptr", hAlg, "uint", 0) 76 | if (hBCRYPT) 77 | DllCall("FreeLibrary", "ptr", hBCRYPT) 78 | } 79 | 80 | return hash 81 | } -------------------------------------------------------------------------------- /src/v1.1_deprecated/functions/bcrypt_sha512.ahk: -------------------------------------------------------------------------------- 1 | MsgBox % bcrypt_sha512("The quick brown fox jumps over the lazy dog") 2 | ; -> 07e547d9586f6a73f73fbac0435ed76951218fb7d0c8d788a309d785436bbb642e93a252a954f23912547d1e8a3b5ed6e1bfd7097821233fa0538f3db854fee6 3 | 4 | 5 | 6 | bcrypt_sha512(string, encoding := "utf-8") 7 | { 8 | static BCRYPT_SHA512_ALGORITHM := "SHA512" 9 | static BCRYPT_OBJECT_LENGTH := "ObjectLength" 10 | static BCRYPT_HASH_LENGTH := "HashDigestLength" 11 | 12 | try 13 | { 14 | ; loads the specified module into the address space of the calling process 15 | if !(hBCRYPT := DllCall("LoadLibrary", "str", "bcrypt.dll", "ptr")) 16 | throw Exception("Failed to load bcrypt.dll", -1) 17 | 18 | ; open an algorithm handle 19 | if (NT_STATUS := DllCall("bcrypt\BCryptOpenAlgorithmProvider", "ptr*", hAlg, "ptr", &BCRYPT_SHA512_ALGORITHM, "ptr", 0, "uint", 0) != 0) 20 | throw Exception("BCryptOpenAlgorithmProvider: " NT_STATUS, -1) 21 | 22 | ; calculate the size of the buffer to hold the hash object 23 | if (NT_STATUS := DllCall("bcrypt\BCryptGetProperty", "ptr", hAlg, "ptr", &BCRYPT_OBJECT_LENGTH, "uint*", cbHashObject, "uint", 4, "uint*", cbData, "uint", 0) != 0) 24 | throw Exception("BCryptGetProperty: " NT_STATUS, -1) 25 | 26 | ; allocate the hash object 27 | VarSetCapacity(pbHashObject, cbHashObject, 0) 28 | ; throw Exception("Memory allocation failed", -1) 29 | 30 | ; calculate the length of the hash 31 | if (NT_STATUS := DllCall("bcrypt\BCryptGetProperty", "ptr", hAlg, "ptr", &BCRYPT_HASH_LENGTH, "uint*", cbHash, "uint", 4, "uint*", cbData, "uint", 0) != 0) 32 | throw Exception("BCryptGetProperty: " NT_STATUS, -1) 33 | 34 | ; allocate the hash buffer 35 | VarSetCapacity(pbHash, cbHash, 0) 36 | ; throw Exception("Memory allocation failed", -1) 37 | 38 | ; create a hash 39 | if (NT_STATUS := DllCall("bcrypt\BCryptCreateHash", "ptr", hAlg, "ptr*", hHash, "ptr", &pbHashObject, "uint", cbHashObject, "ptr", 0, "uint", 0, "uint", 0) != 0) 40 | throw Exception("BCryptCreateHash: " NT_STATUS, -1) 41 | 42 | ; hash some data 43 | VarSetCapacity(pbInput, (StrPut(string, encoding) - 1) * ((encoding = "utf-16" || encoding = "cp1200") ? 2 : 1), 0) 44 | cbInput := (StrPut(string, &pbInput, encoding) - 1) * ((encoding = "utf-16" || encoding = "cp1200") ? 2 : 1) 45 | if (NT_STATUS := DllCall("bcrypt\BCryptHashData", "ptr", hHash, "ptr", &pbInput, "uint", cbInput, "uint", 0) != 0) 46 | throw Exception("BCryptHashData: " NT_STATUS, -1) 47 | 48 | ; close the hash 49 | if (NT_STATUS := DllCall("bcrypt\BCryptFinishHash", "ptr", hHash, "ptr", &pbHash, "uint", cbHash, "uint", 0) != 0) 50 | throw Exception("BCryptFinishHash: " NT_STATUS, -1) 51 | 52 | loop % cbHash 53 | hash .= Format("{:02x}", NumGet(pbHash, A_Index - 1, "uchar")) 54 | } 55 | catch exception 56 | { 57 | ; represents errors that occur during application execution 58 | throw Exception 59 | } 60 | finally 61 | { 62 | ; cleaning up resources 63 | if (pbInput) 64 | VarSetCapacity(pbInput, 0) 65 | if (hHash) 66 | DllCall("bcrypt\BCryptDestroyHash", "ptr", hHash) 67 | if (pbHash) 68 | VarSetCapacity(pbHash, 0) 69 | if (pbHashObject) 70 | VarSetCapacity(pbHashObject, 0) 71 | if (hAlg) 72 | DllCall("bcrypt\BCryptCloseAlgorithmProvider", "ptr", hAlg, "uint", 0) 73 | if (hBCRYPT) 74 | DllCall("FreeLibrary", "ptr", hBCRYPT) 75 | } 76 | 77 | return hash 78 | } -------------------------------------------------------------------------------- /src/v1.1_deprecated/functions/bcrypt_sha512_file.ahk: -------------------------------------------------------------------------------- 1 | v := 0x0 2 | file := FileOpen("TestFile", "rw") 3 | loop % 256 4 | file.WriteUChar(v++) 5 | file.Close() 6 | 7 | MsgBox % bcrypt_sha512_file("TestFile") 8 | ; -> 1e7b80bc8edc552c8feeb2780e111477e5bc70465fac1a77b29b35980c3f0ce4a036a6c9462036824bd56801e62af7e9feba5c22ed8a5af877bf7de117dcac6d 9 | 10 | FileDelete, TestFile 11 | 12 | 13 | 14 | bcrypt_sha512_file(filename, bytes := 1048576 , offset := 0, length := -1, encoding := "utf-8") 15 | { 16 | static BCRYPT_SHA512_ALGORITHM := "SHA512" 17 | static BCRYPT_OBJECT_LENGTH := "ObjectLength" 18 | static BCRYPT_HASH_LENGTH := "HashDigestLength" 19 | 20 | try 21 | { 22 | ; loads the specified module into the address space of the calling process 23 | if !(hBCRYPT := DllCall("LoadLibrary", "str", "bcrypt.dll", "ptr")) 24 | throw Exception("Failed to load bcrypt.dll", -1) 25 | 26 | ; open an algorithm handle 27 | if (NT_STATUS := DllCall("bcrypt\BCryptOpenAlgorithmProvider", "ptr*", hAlg, "ptr", &BCRYPT_SHA512_ALGORITHM, "ptr", 0, "uint", 0) != 0) 28 | throw Exception("BCryptOpenAlgorithmProvider: " NT_STATUS, -1) 29 | 30 | ; calculate the size of the buffer to hold the hash object 31 | if (NT_STATUS := DllCall("bcrypt\BCryptGetProperty", "ptr", hAlg, "ptr", &BCRYPT_OBJECT_LENGTH, "uint*", cbHashObject, "uint", 4, "uint*", cbData, "uint", 0) != 0) 32 | throw Exception("BCryptGetProperty: " NT_STATUS, -1) 33 | 34 | ; allocate the hash object 35 | VarSetCapacity(pbHashObject, cbHashObject, 0) 36 | ; throw Exception("Memory allocation failed", -1) 37 | 38 | ; calculate the length of the hash 39 | if (NT_STATUS := DllCall("bcrypt\BCryptGetProperty", "ptr", hAlg, "ptr", &BCRYPT_HASH_LENGTH, "uint*", cbHash, "uint", 4, "uint*", cbData, "uint", 0) != 0) 40 | throw Exception("BCryptGetProperty: " NT_STATUS, -1) 41 | 42 | ; allocate the hash buffer 43 | VarSetCapacity(pbHash, cbHash, 0) 44 | ; throw Exception("Memory allocation failed", -1) 45 | 46 | ; create a hash 47 | if (NT_STATUS := DllCall("bcrypt\BCryptCreateHash", "ptr", hAlg, "ptr*", hHash, "ptr", &pbHashObject, "uint", cbHashObject, "ptr", 0, "uint", 0, "uint", 0) != 0) 48 | throw Exception("BCryptCreateHash: " NT_STATUS, -1) 49 | 50 | ; create a hash 51 | if !(IsObject(f := FileOpen(filename, "r", encoding))) 52 | throw Exception("Failed to open file: " filename, -1) 53 | length := length < 0 ? f.length - offset : length 54 | if ((offset + length) > f.length) 55 | throw Exception("Invalid parameters offset / length!", -1) 56 | f.Pos(offset) 57 | while (length > bytes) && (dataread := f.RawRead(data, bytes)) 58 | { 59 | if (NT_STATUS := DllCall("bcrypt\BCryptHashData", "ptr", hHash, "ptr", &data, "uint", dataread, "uint", 0) != 0) 60 | throw Exception("BCryptHashData: " NT_STATUS, -1) 61 | length -= dataread 62 | } 63 | if (length > 0) 64 | { 65 | if (dataread := f.RawRead(data, length)) 66 | if (NT_STATUS := DllCall("bcrypt\BCryptHashData", "ptr", hHash, "ptr", &data, "uint", dataread, "uint", 0) != 0) 67 | throw Exception("BCryptHashData: " NT_STATUS, -1) 68 | } 69 | 70 | ; close the hash 71 | if (NT_STATUS := DllCall("bcrypt\BCryptFinishHash", "ptr", hHash, "ptr", &pbHash, "uint", cbHash, "uint", 0) != 0) 72 | throw Exception("BCryptFinishHash: " NT_STATUS, -1) 73 | 74 | loop % cbHash 75 | hash .= Format("{:02x}", NumGet(pbHash, A_Index - 1, "uchar")) 76 | } 77 | catch exception 78 | { 79 | ; represents errors that occur during application execution 80 | throw Exception 81 | } 82 | finally 83 | { 84 | ; cleaning up resources 85 | if (f) 86 | f.Close() 87 | if (pbInput) 88 | VarSetCapacity(pbInput, 0) 89 | if (hHash) 90 | DllCall("bcrypt\BCryptDestroyHash", "ptr", hHash) 91 | if (pbHash) 92 | VarSetCapacity(pbHash, 0) 93 | if (pbHashObject) 94 | VarSetCapacity(pbHashObject, 0) 95 | if (hAlg) 96 | DllCall("bcrypt\BCryptCloseAlgorithmProvider", "ptr", hAlg, "uint", 0) 97 | if (hBCRYPT) 98 | DllCall("FreeLibrary", "ptr", hBCRYPT) 99 | } 100 | 101 | return hash 102 | } -------------------------------------------------------------------------------- /src/v1.1_deprecated/functions/bcrypt_sha512_hmac.ahk: -------------------------------------------------------------------------------- 1 | MsgBox % bcrypt_sha512_hmac("The quick brown fox jumps over the lazy dog", "Secret Salt") 2 | ; -> 8ba0777b278b406b07df08150b98d2c57c68f83a980088e3011f76ea8e6d26b84244a678218408e97066d8dfe8aee20569044d214131327b016ea69a487ef471 3 | 4 | 5 | 6 | bcrypt_sha512_hmac(string, hmac, encoding := "utf-8") 7 | { 8 | static BCRYPT_SHA512_ALGORITHM := "SHA512" 9 | static BCRYPT_ALG_HANDLE_HMAC_FLAG := 0x00000008 10 | static BCRYPT_OBJECT_LENGTH := "ObjectLength" 11 | static BCRYPT_HASH_LENGTH := "HashDigestLength" 12 | 13 | try 14 | { 15 | ; loads the specified module into the address space of the calling process 16 | if !(hBCRYPT := DllCall("LoadLibrary", "str", "bcrypt.dll", "ptr")) 17 | throw Exception("Failed to load bcrypt.dll", -1) 18 | 19 | ; open an algorithm handle 20 | if (NT_STATUS := DllCall("bcrypt\BCryptOpenAlgorithmProvider", "ptr*", hAlg, "ptr", &BCRYPT_SHA512_ALGORITHM, "ptr", 0, "uint", BCRYPT_ALG_HANDLE_HMAC_FLAG) != 0) 21 | throw Exception("BCryptOpenAlgorithmProvider: " NT_STATUS, -1) 22 | 23 | ; calculate the size of the buffer to hold the hash object 24 | if (NT_STATUS := DllCall("bcrypt\BCryptGetProperty", "ptr", hAlg, "ptr", &BCRYPT_OBJECT_LENGTH, "uint*", cbHashObject, "uint", 4, "uint*", cbData, "uint", 0) != 0) 25 | throw Exception("BCryptGetProperty: " NT_STATUS, -1) 26 | 27 | ; allocate the hash object 28 | VarSetCapacity(pbHashObject, cbHashObject, 0) 29 | ; throw Exception("Memory allocation failed", -1) 30 | 31 | ; calculate the length of the hash 32 | if (NT_STATUS := DllCall("bcrypt\BCryptGetProperty", "ptr", hAlg, "ptr", &BCRYPT_HASH_LENGTH, "uint*", cbHash, "uint", 4, "uint*", cbData, "uint", 0) != 0) 33 | throw Exception("BCryptGetProperty: " NT_STATUS, -1) 34 | 35 | ; allocate the hash buffer 36 | VarSetCapacity(pbHash, cbHash, 0) 37 | ; throw Exception("Memory allocation failed", -1) 38 | 39 | ; create a hash 40 | VarSetCapacity(pbSecret, (StrPut(hmac, encoding) - 1) * ((encoding = "utf-16" || encoding = "cp1200") ? 2 : 1), 0) 41 | cbSecret := (StrPut(hmac, &pbSecret, encoding) - 1) * ((encoding = "utf-16" || encoding = "cp1200") ? 2 : 1) 42 | if (NT_STATUS := DllCall("bcrypt\BCryptCreateHash", "ptr", hAlg, "ptr*", hHash, "ptr", &pbHashObject, "uint", cbHashObject, "ptr", &pbSecret, "uint", cbSecret, "uint", 0) != 0) 43 | throw Exception("BCryptCreateHash: " NT_STATUS, -1) 44 | 45 | ; hash some data 46 | VarSetCapacity(pbInput, (StrPut(string, encoding) - 1) * ((encoding = "utf-16" || encoding = "cp1200") ? 2 : 1), 0) 47 | cbInput := (StrPut(string, &pbInput, encoding) - 1) * ((encoding = "utf-16" || encoding = "cp1200") ? 2 : 1) 48 | if (NT_STATUS := DllCall("bcrypt\BCryptHashData", "ptr", hHash, "ptr", &pbInput, "uint", cbInput, "uint", 0) != 0) 49 | throw Exception("BCryptHashData: " NT_STATUS, -1) 50 | 51 | ; close the hash 52 | if (NT_STATUS := DllCall("bcrypt\BCryptFinishHash", "ptr", hHash, "ptr", &pbHash, "uint", cbHash, "uint", 0) != 0) 53 | throw Exception("BCryptFinishHash: " NT_STATUS, -1) 54 | 55 | loop % cbHash 56 | hash .= Format("{:02x}", NumGet(pbHash, A_Index - 1, "uchar")) 57 | } 58 | catch exception 59 | { 60 | ; represents errors that occur during application execution 61 | throw Exception 62 | } 63 | finally 64 | { 65 | ; cleaning up resources 66 | if (pbInput) 67 | VarSetCapacity(pbInput, 0) 68 | if (hHash) 69 | DllCall("bcrypt\BCryptDestroyHash", "ptr", hHash) 70 | if (pbHash) 71 | VarSetCapacity(pbHash, 0) 72 | if (pbHashObject) 73 | VarSetCapacity(pbHashObject, 0) 74 | if (hAlg) 75 | DllCall("bcrypt\BCryptCloseAlgorithmProvider", "ptr", hAlg, "uint", 0) 76 | if (hBCRYPT) 77 | DllCall("FreeLibrary", "ptr", hBCRYPT) 78 | } 79 | 80 | return hash 81 | } --------------------------------------------------------------------------------