Update:
Added class for all hash functions
CNG (Cryptography API: Next Generation)
Re: CNG (Cryptography API: Next Generation)
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
- Masonjar13
- Posts: 1555
- Joined: 20 Jul 2014, 10:16
- Location: Не Россия
- Contact:
Re: CNG (Cryptography API: Next Generation)
Any progress on encryption?
Re: CNG (Cryptography API: Next Generation)
Hey Masonjar13...
just AES with CBC is finsihed atm.
just AES with CBC is finsihed atm.
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
- Masonjar13
- Posts: 1555
- Joined: 20 Jul 2014, 10:16
- Location: Не Россия
- Contact:
Re: CNG (Cryptography API: Next Generation)
Great! No pressure or anything, it had just been a while, curious if you were still working on it. I'm very much looking forward to using this in place of Crypt. I appreciate the update!
- Masonjar13
- Posts: 1555
- Joined: 20 Jul 2014, 10:16
- Location: Не Россия
- Contact:
Re: CNG (Cryptography API: Next Generation)
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 ?
Re: CNG (Cryptography API: Next Generation)
Have you quit on this project?
Would love to see data encryption!
Would love to see data encryption!
Re: CNG (Cryptography API: Next Generation)
Since I got no time to finish this project atm I uploaded my first encryption function AES + CBC
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
Re: CNG (Cryptography API: Next Generation)
Update:
- fixed files with bom (thx jeeswg for reporting and wolf_II for the hint)
- fixed files with bom (thx jeeswg for reporting and wolf_II for the hint)
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
Re: CNG (Cryptography API: Next Generation)
Hi,
AHK 1.1.28.00 x64 Unicode, Windows 10 x64 v.1709.
The Windows 10 versions of the file hash functions give incorrect results if the file size is 262145 bytes or larger.
The Windows 7 versions of the file hash functions seem to give correct results regardless of file size.
Tested for MD5, SHA1, SHA256, SHA384, SHA512.
AHK 1.1.28.00 x64 Unicode, Windows 10 x64 v.1709.
The Windows 10 versions of the file hash functions give incorrect results if the file size is 262145 bytes or larger.
The Windows 7 versions of the file hash functions seem to give correct results regardless of file size.
Tested for MD5, SHA1, SHA256, SHA384, SHA512.
Re: CNG (Cryptography API: Next Generation)
Hi again,
AHK 1.1.28.00 x64 Unicode, Windows 10 x64 v.1709, CNG Win7 version.
I would like to create a hash of a part of a binary file. However, if said part contains characters (bytes) >= 0x80 I get incorrect results.
Any pointers on what I am doing wrong and how to fix it would be appreciated.
AHK 1.1.28.00 x64 Unicode, Windows 10 x64 v.1709, CNG Win7 version.
I would like to create a hash of a part of a binary file. However, if said part contains characters (bytes) >= 0x80 I get incorrect results.
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
)
Re: CNG (Cryptography API: Next Generation)
You have to learn about character encoding then. 0x80 has a special meaning for unicode strings.
This library only seems to be able to handle strings unless it is a file.
This library only seems to be able to handle strings unless it is a file.
Recommends AHK Studio
Re: CNG (Cryptography API: Next Generation)
Thanks for bringing me on the right track. It seems replacing
with
helps. Need to do some testing though.
Thanks again.
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
Thanks again.
Re: CNG (Cryptography API: Next Generation)
OK, no luck. Sometimes it works, sometimes it does not. Don't know how to pinpoint it.
Re: CNG (Cryptography API: Next Generation)
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
Re: CNG (Cryptography API: Next Generation)
After fixing the bug (win10 functions), I decided to remove the win10 functions from the lib.
Since BCryptHash performs a single hash computation, it will consume the full memory for string and file hashes.
So if the file is 4 GB, the ram usage will be also 4 gb for the hash calculation. Its also slower than the other one and not compatible on windows 7.
So please use the win7 function.
Since BCryptHash performs a single hash computation, it will consume the full memory for string and file hashes.
So if the file is 4 GB, the ram usage will be also 4 gb for the hash calculation. Its also slower than the other one and not compatible on windows 7.
So please use the win7 function.
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
Re: CNG (Cryptography API: Next Generation)
Thank you very much for having looked into the bug! I will use the Windows 7 functions, then.
Any suggestions on how to create a hash of a part of a binary file, please?
Thank you.
Any suggestions on how to create a hash of a part of a binary file, please?
Thank you.
Re: CNG (Cryptography API: Next Generation)
?
*not tested*
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
}
Re: CNG (Cryptography API: Next Generation)
Will try, thank you!
Re: CNG (Cryptography API: Next Generation)
@just me: works fine