//Copyright (c) 2022 Bruno van Dooren
//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files (the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions:
//The above copyright notice and this permission notice shall be included in all
//copies or substantial portions of the Software.
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
//SOFTWARE.
#include "pch.h"
#include "CBCryptHashObject.h"
#include <sstream>
#include <SubAuth.h>
namespace w32
{
//Initialization of the hash object.
//This is the biggest part of the boilerplate code.
//The algorithm can be user supplied but defaults to BCRYPT_SHA256_ALGORITHM
//The API allows for the secret to be NULL. This is useful for cases where
//the hash is created solely for the point of data integrity instead of
//security / signing purposes.
//The algorithm provider is optional. If none is provided, the first provider
//That implements the specified algorithm is used
CBCryptHashObject::CBCryptHashObject(
CBCryptProvider & provider,
PUCHAR secret,
ULONG sizeofSecret) {
DWORD cbData = 0;
//calculate the size of the buffer to hold the hash object
DWORD sizeofHashObject = 0;
provider.GetProperty(BCRYPT_OBJECT_LENGTH, (PBYTE)&sizeofHashObject, sizeof(DWORD), &cbData);
//allocate the hash object on the heap
m_pbHashObject.Resize(sizeofHashObject);
//calculate the length of the hash
DWORD sizeofHash = 0;
provider.GetProperty(BCRYPT_HASH_LENGTH, (PBYTE)&sizeofHash, sizeof(DWORD), &cbData);
//allocate the hash buffer on the heap
m_pbHash.Resize(sizeofHash);
//Validate the arguments for the secret if they were supplied
if ((secret != NULL && sizeofSecret == 0) ||
(secret == NULL && sizeofSecret != 0)) {
throw ExWin32Error(ERROR_INVALID_PARAMETER);
}
//Initialize the hash object
NTSTATUS status;
if (!NT_SUCCESS(status = BCryptCreateHash(
provider, &m_hHash, m_pbHashObject, sizeofHashObject, secret, sizeofSecret, 0)))
throw ExNtStatus(status);
}
//Release the various resources
CBCryptHashObject::~CBCryptHashObject() {
if (m_hHash != NULL)
BCryptDestroyHash(m_hHash);
}
//Add new data to the hash. Large datasets can be broken in pieces and supplied
//piece by piece if needed in cases where the buffer size is limited for some reason.
//Data is only added if the internal state is error free.
void CBCryptHashObject::AddData(PBYTE data, ULONG dataSize) {
if (dataSize <= 0)
throw range_error("index out of bounds");
NTSTATUS status;
if(!NT_SUCCESS(status = BCryptHashData(m_hHash, data, dataSize, 0)))
throw ExNtStatus(status);
}
//Finish the hash if the internal state is error free.
void CBCryptHashObject::Finish(void) {
NTSTATUS status;
if (!NT_SUCCESS(status = BCryptFinishHash(
m_hHash, m_pbHash, (DWORD)m_pbHash.Size(), 0)))
throw ExNtStatus(status);
}
//Get the size of the hash buffer.
//The caller needs this in order to allocate a buffer for the hash
//The caller doesn't get access to the hash itself
DWORD CBCryptHashObject::GetHashSize(void) {
return (DWORD)m_pbHash.Size();
}
//Retrieve the hash data
void CBCryptHashObject::GetHash(PBYTE buffer, ULONG bufferSize) {
if (bufferSize >= m_pbHash.Size()) {
memcpy_s(buffer, bufferSize, m_pbHash, m_pbHash.Size());
}
else {
throw ExWin32Error(ERROR_INVALID_PARAMETER);
}
}
wstring CBCryptHashObject::GetHashHexString(){
wstringstream wsstream;
wsstream << L"0x" << hex;
for (ULONG i = 0; i < GetHashSize(); i++)
wsstream << m_pbHash[i];
return wsstream.str();
}
//Add the data of a string object to the hash.
//Note that the data is added without the 0 terminator.
void AddDataToHash(CBCryptHashObject& hash, const string& data) {
if (data.size() <= 0)
throw ExWin32Error();
PUCHAR input = (PUCHAR)(data.c_str());
ULONG numBytes = (ULONG)(data.length() * sizeof(data[0]));
hash.AddData(input, numBytes);
}
//Add the data of a wstring object to the hash.
//Note that the data is added without the 0 terminator.
void AddDataToHash(CBCryptHashObject& hash, const wstring& data) {
if (data.size() <= 0)
throw ExWin32Error();
PUCHAR input = (PUCHAR)(data.c_str());
ULONG numBytes = (ULONG)(data.length() * (ULONG)sizeof(data[0]));
hash.AddData(input, numBytes);
}
CHashDataProvider::CHashDataProvider() : m_buffer() {
}
CHashDataProvider::~CHashDataProvider() {
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源详情
资源评论
资源推荐
收起资源包目录
AmIFirst_-_src.zip (22个子文件)
Test_AmIFirst
framework.h 1KB
CBCryptProvider.cpp 1019B
Test_AmIFirst.vcxproj.user 168B
LinkerDirectives.cpp 505B
pch.h 2KB
CBuffer.h 5KB
CHandle.h 807B
Exception.cpp 4KB
CBCryptProvider.h 2KB
CBCryptHashObject.cpp 6KB
Test_AmIFirst.sln 1KB
CAppInstance.h 3KB
Test_AmIFirst.cpp 1KB
Test_AmIFirst.vcxproj 8KB
CBCryptHashObject.h 6KB
Test_AmIFirst.vcxproj.filters 3KB
StringHandling.h 2KB
CAppInstance.cpp 4KB
PlatformIncludes.h 2KB
String.cpp 1KB
Exception.h 3KB
CHandle.cpp 940B
共 22 条
- 1
StubbornZorro
- 粉丝: 3
- 资源: 19
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0