Skip to content

Instantly share code, notes, and snippets.

@kennykerr
Created July 11, 2018 13:35
Show Gist options
  • Save kennykerr/1d535afa8eccbabf058c1236f1e1af41 to your computer and use it in GitHub Desktop.
Save kennykerr/1d535afa8eccbabf058c1236f1e1af41 to your computer and use it in GitHub Desktop.
#include <unknwn.h>
#include "winrt/Windows.Storage.Streams.h"
using namespace winrt;
using namespace Windows::Foundation;
using namespace Windows::Storage::Streams;
struct __declspec(uuid("905a0fef-bc53-11df-8c49-001e4fc686da")) IBufferByteAccess : ::IUnknown
{
virtual HRESULT __stdcall Buffer(uint8_t** value) noexcept = 0;
};
struct SecureBuffer : implements<SecureBuffer, IBuffer, IBufferByteAccess, IClosable>
{
std::vector<uint8_t> m_buffer;
uint32_t m_length{};
SecureBuffer(uint32_t size) :
m_buffer(size)
{
}
~SecureBuffer() noexcept
{
Close();
}
void Close() noexcept
{
SecureZeroMemory(m_buffer.data(), m_buffer.size());
}
uint32_t Capacity() const noexcept
{
return static_cast<uint32_t>(m_buffer.size());
}
uint32_t Length() const noexcept
{
return m_length;
}
void Length(uint32_t value)
{
if (value > m_buffer.size())
{
throw hresult_invalid_argument();
}
m_length = value;
}
HRESULT __stdcall Buffer(uint8_t** value) noexcept final
{
*value = m_buffer.data();
return S_OK;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment