-
I'm writing a program with MD5 and HMAC-SHA1 singature, the MD5 seems work but the HMAC-SHA1 failed with // Generate MD5 hex string.
auto signatureBaseString = "Some string";
auto signatureHashProvider = winrt::Windows::Security::Cryptography::Core::HashAlgorithmProvider::OpenAlgorithm(winrt::Windows::Security::Cryptography::Core::HashAlgorithmNames::Md5());
auto signatureHashBuffer = winrt::Windows::Security::Cryptography::CryptographicBuffer::ConvertStringToBinary(signatureBaseString, winrt::Windows::Security::Cryptography::BinaryStringEncoding::Utf8);
auto signatureHashResult = signatureHashProvider.HashData(signatureHashBuffer);
auto signatureHashString = winrt::Windows::Security::Cryptography::CryptographicBuffer::EncodeToHexString(signatureHashResult);
// MD5 hex string successfully generated here.
// Generate encrypted string using HMAC-SHA1.
auto signatureEncryptProvider = winrt::Windows::Security::Cryptography::Core::MacAlgorithmProvider::OpenAlgorithm(winrt::Windows::Security::Cryptography::Core::MacAlgorithmNames::HmacSha1());
auto signatureEncryptBuffer = winrt::Windows::Security::Cryptography::CryptographicBuffer::ConvertStringToBinary(signatureHashString, winrt::Windows::Security::Cryptography::BinaryStringEncoding::Utf8);
auto signatureEncryptKeyBinary = winrt::Windows::Security::Cryptography::CryptographicBuffer::ConvertStringToBinary(apiKey, winrt::Windows::Security::Cryptography::BinaryStringEncoding::Utf8);
auto signatureEncryptKey = signatureEncryptProvider.CreateKey(signatureEncryptKeyBinary);
// WinRT originate error - 0x80004002 'No such interface supported' error occurred at the follow line but not interrupt the program.
auto singatureEncryptResult = winrt::Windows::Security::Cryptography::Core::CryptographicEngine::Encrypt(signatureEncryptKey, signatureEncryptBuffer, nullptr);
auto singatureEncryptString = winrt::Windows::Security::Cryptography::CryptographicBuffer::EncodeToHexString(singatureEncryptResult); |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Yes. All of these should even have the DualApiPartition attribute in the metadata. While it doesn't mean as much these days, this attribute was used to identify runtime classes that were usable in both desktop and Windows Store applications in the days of Windows 8/8.1 and early Windows 10. This is also an issue that is independent of the Windows App SDK. Anything that is under the Windows root namespace is provided by Windows itself. For C++ applications, this is also provided by the Windows SDK, with it being part of the Universal API contract. |
Beta Was this translation helpful? Give feedback.
-
Well, I finally found I made a stupid mistake. HMACSHA1 is a hash algorithm, and I should call |
Beta Was this translation helpful? Give feedback.
Well, I finally found I made a stupid mistake. HMACSHA1 is a hash algorithm, and I should call
winrt::Windows::Security::Cryptography::Core::CryptographicEngine::Sign
function. Only the encryption algorithm can callwinrt::Windows::Security::Cryptography::Core::CryptographicEngine::Encrypt
.Say one more thing, there is really too little debug information in WinRT. This kind of erroneous call should be reported as
interface call error
or do not provide this interface for hash-like algorithms, rather thanNo such interface supported
.