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
(DataFlex) Duplicate Java HMAC-SHA1 using ChilkatThis 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())); }
Use ChilkatAx-win32.pkg Procedure Test Boolean iSuccess String sPrivateKey String sPublicKey String sPlainText Handle hoCrypt String sHmacHex Handle hoSbSignatureHash String sAuthorization // This example assumes the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. // This is clearly in base64 encoding. Move "qwfvUeVRWAwyjlAzGivefFPTg+m6QtBPmDVv7Ra/u7K3UuVVRhrZ/qc8EPh8IGJatuxsWD4EX+D9qE/eVvLTpw==" To sPrivateKey // This is clearly in the hex encoding. Move "16baedbe244b6c063968850716afb319a" To sPublicKey Move "POST" + (character(10)) + "1456765639015" + (character(10)) + "/api/v5/policy/1234567890" + (character(10)) To sPlainText Get Create (RefClass(cComChilkatCrypt2)) To hoCrypt If (Not(IsComObjectCreated(hoCrypt))) Begin Send CreateComObject of hoCrypt End // We want HMAC-SHA1. Set ComMacAlgorithm Of hoCrypt To "HMAC" Set ComHashAlgorithm Of hoCrypt To "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.) Get ComSetMacKeyEncoded Of hoCrypt sPrivateKey "us-ascii" To iSuccess // We want our HMAC-SHA1 output to be a hex string. Set ComEncodingMode Of hoCrypt To "hex_lower" Get ComMacStringENC Of hoCrypt sPlainText To sHmacHex Showln sHmacHex // The expected result is: dd3e8440f6b550f152156ea5e12d3e20b262adae Get Create (RefClass(cComChilkatStringBuilder)) To hoSbSignatureHash If (Not(IsComObjectCreated(hoSbSignatureHash))) Begin Send CreateComObject of hoSbSignatureHash End Get ComAppend Of hoSbSignatureHash sPublicKey To iSuccess Get ComAppend Of hoSbSignatureHash ":" To iSuccess Get ComAppend Of hoSbSignatureHash sHmacHex To iSuccess Get ComGetEncoded Of hoSbSignatureHash "base64" "utf-8" To sAuthorization Showln "Authorization: " sAuthorization // The expected result is: MTZiYWVkYmUyNDRiNmMwNjM5Njg4NTA3MTZhZmIzMTlhOmRkM2U4NDQwZjZiNTUwZjE1MjE1NmVhNWUxMmQzZTIwYjI2MmFkYWU= End_Procedure |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.