Re: CNG (Cryptography API: Next Generation)
Posted: 05 Oct 2016, 05:37
Update:
Added class for all hash functions
Added class for all hash functions
Let's help each other out
https://www.autohotkey.com/boards/
There is a function only present in Windows 10 that encapsulates several functions. That is, there is no difference, just a convenience.guest3456 wrote:why are there separate implementations for win7/win10 ?
Code: Select all
File := FileOpen("TestFile", "rw")
File.WriteUChar(0x80)
File.Seek(0)
File.RawRead(Data, 1)
File.Close()
MsgBox,
( Join`s LTrim Comments
%
bcrypt_sha256(Data) . "`n" . ; ea0add9a514e94de9abd0ba721eed15b15aa0b3a0f09ebee5e54a9dfae943153 <- Not OK
bcrypt_sha256(StrGet(&Data, "CP1252")) . "`n" . ; c4cc90ed3d26f12d4b08a75140970a7904035c31cbb4515a83f19b9003c00d1d <- Not OK
bcrypt_sha256(StrGet(&Data, "CP1250")) . "`n" . ; c4cc90ed3d26f12d4b08a75140970a7904035c31cbb4515a83f19b9003c00d1d <- Not OK
bcrypt_sha256(StrGet(&Data, "CP0")) . "`n`n" . ; c4cc90ed3d26f12d4b08a75140970a7904035c31cbb4515a83f19b9003c00d1d <- Not OK
bcrypt_sha256_file("TestFile") ; 76be8b528d0075f7aae98d6fa57a6d3c83ae480a8469e668d7b0af968995ac71 <- OK, but uses the whole file instead of just a part
)
Code: Select all
VarSetCapacity(pbInput, StrPut(string, "UTF-8"), 0) && cbInput := StrPut(string, &pbInput, "UTF-8") - 1
Code: Select all
VarSetCapacity(pbInput, StrPut(string, "CP0"), 0) && cbInput := StrPut(string, &pbInput, "CP0") - 1
Just to give an example to anyone who'd like to confirm / reproduce:The Windows 10 versions of the file hash functions give incorrect results if the file size is 262145 bytes or larger.
Code: Select all
#Include class_bcrypt.ahk
#Include bcrypt_sha256_file.ahk
SetWorkingDir, %A_ScriptDir%
Loop, 262145
TestString .= "a"
FileAppend, %TestString%, test.txt
MsgBox, % bcrypt.file("test.txt", "SHA256") ; ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb - INCORRECT, matches SHA256 of "a"
MsgBox, % bcrypt_sha256_file("test.txt") ; ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb - INCORRECT, matches SHA256 of "a"
; Correct SHA256:
; c592f4a6b099700b5050ce8bc67367f0c8f44810203124e86405c3e7b6f1a2ba
FileDelete, test.txt
Code: Select all
#NoEnv
MsgBox % bcrypt_sha256_filepart("C:\Windows\notepad.exe")
ExitApp
bcrypt_sha256_filepart(filename, offset := 0, length := -1)
{
static BCRYPT_SHA256_ALGORITHM := "SHA256"
static BCRYPT_OBJECT_LENGTH := "ObjectLength"
static BCRYPT_HASH_LENGTH := "HashDigestLength"
if !(hBCRYPT := DllCall("LoadLibrary", "str", "bcrypt.dll", "ptr"))
throw Exception("Failed to load bcrypt.dll", -1)
if (NT_STATUS := DllCall("bcrypt\BCryptOpenAlgorithmProvider", "ptr*", hAlgo, "ptr", &BCRYPT_SHA256_ALGORITHM, "ptr", 0, "uint", 0) != 0)
throw Exception("BCryptOpenAlgorithmProvider: " NT_STATUS, -1)
if (NT_STATUS := DllCall("bcrypt\BCryptGetProperty", "ptr", hAlgo, "ptr", &BCRYPT_OBJECT_LENGTH, "uint*", cbHashObject, "uint", 4, "uint*", cbResult, "uint", 0) != 0)
throw Exception("BCryptGetProperty: " NT_STATUS, -1)
if (NT_STATUS := DllCall("bcrypt\BCryptGetProperty", "ptr", hAlgo, "ptr", &BCRYPT_HASH_LENGTH, "uint*", cbHash, "uint", 4, "uint*", cbResult, "uint", 0) != 0)
throw Exception("BCryptGetProperty: " NT_STATUS, -1)
VarSetCapacity(pbHashObject, cbHashObject, 0)
if (NT_STATUS := DllCall("bcrypt\BCryptCreateHash", "ptr", hAlgo, "ptr*", hHash, "ptr", &pbHashObject, "uint", cbHashObject, "ptr", 0, "uint", 0, "uint", 0) != 0)
throw Exception("BCryptCreateHash: " NT_STATUS, -1)
if !(f := FileOpen(filename, "r", "UTF-8"))
throw Exception("Failed to open file: " filename, -1)
length := length < 0 ? f.length - offset : length
if ((offset + length) > f.length)
throw Exception("Invalid parameters offset / length!", -1)
f.Pos(offset)
while (length > 262144) && (dataread := f.RawRead(data, 262144))
{
if (NT_STATUS := DllCall("bcrypt\BCryptHashData", "ptr", hHash, "ptr", &data, "uint", dataread, "uint", 0) != 0)
throw Exception("BCryptHashData: " NT_STATUS, -1)
length -= dataread
}
if (length > 0)
{
if (dataread := f.RawRead(data, length))
if (NT_STATUS := DllCall("bcrypt\BCryptHashData", "ptr", hHash, "ptr", &data, "uint", dataread, "uint", 0) != 0)
throw Exception("BCryptHashData: " NT_STATUS, -1)
}
f.Close()
VarSetCapacity(pbHash, cbHash, 0)
if (NT_STATUS := DllCall("bcrypt\BCryptFinishHash", "ptr", hHash, "ptr", &pbHash, "uint", cbHash, "uint", 0) != 0)
throw Exception("BCryptFinishHash: " NT_STATUS, -1)
loop % cbHash
hash .= Format("{:02x}", NumGet(pbHash, A_Index - 1, "uchar"))
DllCall("bcrypt\BCryptDestroyHash", "ptr", hHash)
DllCall("bcrypt\BCryptCloseAlgorithmProvider", "ptr", hAlgo, "uint", 0)
DllCall("FreeLibrary", "ptr", hBCRYPT)
return hash
}