Chilkat Examples

ChilkatHOME.NET Core C#Android™AutoItCC#C++Chilkat2-PythonCkPythonClassic ASPDataFlexDelphi ActiveXDelphi DLLGoJavaLianjaMono C#Node.jsObjective-CPHP ActiveXPHP ExtensionPerlPowerBuilderPowerShellPureBasicRubySQL ServerSwift 2Swift 3,4,5...TclUnicode CUnicode C++VB.NETVBScriptVisual Basic 6.0Visual FoxProXojo Plugin

SQL Server Examples

Web API Categories

ASN.1
AWS KMS
AWS Misc
Amazon EC2
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Azure Cloud Storage
Azure Key Vault
Azure Service Bus
Azure Table Service
Base64
Bounced Email
Box
CAdES
CSR
CSV
Certificates
Cloud Signature CSC
Code Signing
Compression
DKIM / DomainKey
DNS
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
EBICS
ECC
Ed25519
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
GMail SMTP/IMAP/POP
Geolocation
Google APIs
Google Calendar
Google Cloud SQL
Google Cloud Storage
Google Drive
Google Photos
Google Sheets
Google Tasks
Gzip
HTML-to-XML/Text
HTTP

HTTP Misc
IMAP
JSON
JSON Web Encryption (JWE)
JSON Web Signatures (JWS)
JSON Web Token (JWT)
Java KeyStore (JKS)
MHT / HTML Email
MIME
MS Storage Providers
Microsoft Graph
Misc
NTLM
OAuth1
OAuth2
OIDC
Office365
OneDrive
OpenSSL
Outlook
Outlook Calendar
Outlook Contact
PDF Signatures
PEM
PFX/P12
PKCS11
POP3
PRNG
REST
REST Misc
RSA
SCP
SCard
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
ScMinidriver
SharePoint
SharePoint Online
Signing in the Cloud
Socket/SSL/TLS
Spider
Stream
Tar Archive
ULID/UUID
Upload
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Zip
curl
uncategorized

 

 

 

(SQL Server) Duplicate Java HMAC-SHA1 using Chilkat

This example uses Chilkat to produce the same results as this Java code:

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.binary.Hex;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;

public void hmacSignatureExample() throws NoSuchAlgorithmException,
InvalidKeyException {

final String method = "POST";
final Long epoch = 1456765639015L; // Hardcoding for this example
final String uri = "/api/v5/policy/1234567890";
final String newline = "\n";

final String privateKey = "qwfvUeVRWAwyjlAzGivefFPTg+m6QtBPmDVv7Ra
/u7K3UuVVRhrZ/qc8EPh8IGJatuxsWD4EX+D9qE/eVvLTpw==";
final String publicKey = "16baedbe244b6c063968850716afb319a";

// Prepare the signature
final String plainText = method + newline + epoch + newline + uri + newline;

// Hash the plaintext with the private key
final byte[] keyBytes = privateKey.getBytes();
final Key key = new SecretKeySpec(keyBytes, 0, keyBytes.length, "HmacSHA1");
final Mac mac = Mac.getInstance("HmacSHA1");
mac.init(key);
String signatureHash = new String(Hex.encodeHex(mac.doFinal(plainText.
getBytes())));

// Prefix with public key
signatureHash = publicKey + ":" + signatureHash;

// Base64 encode to produce AUTHORIZATION
final String authorization = new String(Base64.encodeBase64(signatureHash.
getBytes()));
}

Chilkat ActiveX Downloads

ActiveX for 32-bit and 64-bit Windows

-- 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 @success int

    -- This is clearly in base64 encoding.
    DECLARE @privateKey nvarchar(4000)
    SELECT @privateKey = 'qwfvUeVRWAwyjlAzGivefFPTg+m6QtBPmDVv7Ra/u7K3UuVVRhrZ/qc8EPh8IGJatuxsWD4EX+D9qE/eVvLTpw=='

    -- This is clearly in the hex encoding.
    DECLARE @publicKey nvarchar(4000)
    SELECT @publicKey = '16baedbe244b6c063968850716afb319a'

    DECLARE @plainText nvarchar(4000)
    SELECT @plainText = 'POST' + CHAR(10) + '1456765639015' + CHAR(10) + '/api/v5/policy/1234567890' + CHAR(10)

    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

    -- We want HMAC-SHA1.
    EXEC sp_OASetProperty @crypt, 'MacAlgorithm', 'HMAC'
    EXEC sp_OASetProperty @crypt, 'HashAlgorithm', 'SHA1'

    -- The Java code (above) is literally using the us-ascii chars of the base64 string as the HMAC key.
    -- (It is NOT using the base64 decoded bytes.)
    EXEC sp_OAMethod @crypt, 'SetMacKeyEncoded', @success OUT, @privateKey, 'us-ascii'

    -- We want our HMAC-SHA1 output to be a hex string.
    EXEC sp_OASetProperty @crypt, 'EncodingMode', 'hex_lower'

    DECLARE @hmacHex nvarchar(4000)
    EXEC sp_OAMethod @crypt, 'MacStringENC', @hmacHex OUT, @plainText


    PRINT @hmacHex

    -- The expected result is: dd3e8440f6b550f152156ea5e12d3e20b262adae

    DECLARE @sbSignatureHash int
    -- Use "Chilkat_9_5_0.StringBuilder" for versions of Chilkat < 10.0.0
    EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sbSignatureHash OUT

    EXEC sp_OAMethod @sbSignatureHash, 'Append', @success OUT, @publicKey
    EXEC sp_OAMethod @sbSignatureHash, 'Append', @success OUT, ':'
    EXEC sp_OAMethod @sbSignatureHash, 'Append', @success OUT, @hmacHex

    DECLARE @authorization nvarchar(4000)
    EXEC sp_OAMethod @sbSignatureHash, 'GetEncoded', @authorization OUT, 'base64', 'utf-8'

    PRINT 'Authorization: ' + @authorization

    -- The expected result is:  MTZiYWVkYmUyNDRiNmMwNjM5Njg4NTA3MTZhZmIzMTlhOmRkM2U4NDQwZjZiNTUwZjE1MjE1NmVhNWUxMmQzZTIwYjI2MmFkYWU=

    EXEC @hr = sp_OADestroy @crypt
    EXEC @hr = sp_OADestroy @sbSignatureHash


END
GO

 

© 2000-2024 Chilkat Software, Inc. All Rights Reserved.