![]() |
Chilkat HOME Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi DLL Go Java Node.js Objective-C PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(PowerBuilder) 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()));
}
integer li_rc integer li_Success string ls_PrivateKey string ls_PublicKey string ls_PlainText oleobject loo_Crypt string ls_HmacHex oleobject loo_SbSignatureHash string ls_Authorization // This example assumes the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. // This is clearly in base64 encoding. ls_PrivateKey = "qwfvUeVRWAwyjlAzGivefFPTg+m6QtBPmDVv7Ra/u7K3UuVVRhrZ/qc8EPh8IGJatuxsWD4EX+D9qE/eVvLTpw==" // This is clearly in the hex encoding. ls_PublicKey = "16baedbe244b6c063968850716afb319a" ls_PlainText = "POST~n1456765639015~n/api/v5/policy/1234567890~n" loo_Crypt = create oleobject // Use "Chilkat_9_5_0.Crypt2" for versions of Chilkat < 10.0.0 li_rc = loo_Crypt.ConnectToNewObject("Chilkat.Crypt2") if li_rc < 0 then destroy loo_Crypt MessageBox("Error","Connecting to COM object failed") return end if // We want HMAC-SHA1. loo_Crypt.MacAlgorithm = "HMAC" loo_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.) loo_Crypt.SetMacKeyEncoded(ls_PrivateKey,"us-ascii") // We want our HMAC-SHA1 output to be a hex string. loo_Crypt.EncodingMode = "hex_lower" ls_HmacHex = loo_Crypt.MacStringENC(ls_PlainText) Write-Debug ls_HmacHex // The expected result is: dd3e8440f6b550f152156ea5e12d3e20b262adae loo_SbSignatureHash = create oleobject // Use "Chilkat_9_5_0.StringBuilder" for versions of Chilkat < 10.0.0 li_rc = loo_SbSignatureHash.ConnectToNewObject("Chilkat.StringBuilder") loo_SbSignatureHash.Append(ls_PublicKey) loo_SbSignatureHash.Append(":") loo_SbSignatureHash.Append(ls_HmacHex) ls_Authorization = loo_SbSignatureHash.GetEncoded("base64","utf-8") Write-Debug "Authorization: " + ls_Authorization // The expected result is: MTZiYWVkYmUyNDRiNmMwNjM5Njg4NTA3MTZhZmIzMTlhOmRkM2U4NDQwZjZiNTUwZjE1MjE1NmVhNWUxMmQzZTIwYjI2MmFkYWU= destroy loo_Crypt destroy loo_SbSignatureHash |
||||
© 2000-2025 Chilkat Software, Inc. All Rights Reserved.