Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(SQL Server) AES-CMACDemonstrates using the AES-CMAC algorithm, which is a keyed hash function similar to HMAC and Poly1305. Note: Chilkat added AES-CMAC in version 9.5.0.95.
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls. -- CREATE PROCEDURE ChilkatSample AS BEGIN DECLARE @hr int -- This example assumes the Chilkat API to have been previously unlocked. -- See Global Unlock Sample for sample code. DECLARE @crypt int -- Use "Chilkat_9_5_0.Crypt2" for versions of Chilkat < 10.0.0 EXEC @hr = sp_OACreate 'Chilkat.Crypt2', @crypt OUT IF @hr <> 0 BEGIN PRINT 'Failed to create ActiveX component' RETURN END -- Set the MAC algorithm to AES-CMAC EXEC sp_OASetProperty @crypt, 'MacAlgorithm', 'aes-cmac' -- AES-CMAC always uses a 16-byte (128-bit) MAC key. DECLARE @keyHex nvarchar(4000) SELECT @keyHex = '2b7e151628aed2a6abf7158809cf4f3c' DECLARE @success int EXEC sp_OAMethod @crypt, 'SetMacKeyEncoded', @success OUT, @keyHex, 'hex' -- Let's compute the AES-CMAC for the test vector at https://www.rfc-editor.org/rfc/rfc4493#appendix-A -- Here we have 64 bytes in hex representation. DECLARE @messageBytes nvarchar(4000) SELECT @messageBytes = '6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e5130c81c46a35ce411e5fbc1191a0a52eff69f2445df4f9b17ad2b417be66c3710' DECLARE @bd int -- Use "Chilkat_9_5_0.BinData" for versions of Chilkat < 10.0.0 EXEC @hr = sp_OACreate 'Chilkat.BinData', @bd OUT EXEC sp_OAMethod @bd, 'AppendEncoded', @success OUT, @messageBytes, 'hex' -- Compute the AES-CMAC for the bytes contained in bd and return the AES-CMAC in hex representation. EXEC sp_OASetProperty @crypt, 'EncodingMode', 'hex_lower' DECLARE @cmac nvarchar(4000) EXEC sp_OAMethod @crypt, 'MacBdENC', @cmac OUT, @bd PRINT @cmac -- Output should be: 51f0bebf7e3b9d92fc49741779363cfe -- Now do the same for a string: DECLARE @plainText nvarchar(4000) SELECT @plainText = '''Twas brillig, and the slithy toves' + CHAR(10) + 'Did gyre and gimble in the wabe:' + CHAR(10) + 'All mimsy were the borogoves,' + CHAR(10) + 'And the mome raths outgrabe.' DECLARE @encTag nvarchar(4000) EXEC sp_OAMethod @crypt, 'MacStringENC', @encTag OUT, @plainText PRINT @encTag -- Output should be: 4fca1fcbd265048d247f99ab57fa3ceb EXEC @hr = sp_OADestroy @crypt EXEC @hr = sp_OADestroy @bd END GO |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.