Sample code for 30+ languages & platforms
Java

Workaround for the deprecated Crypt2.MacBytesENC method

Shows how to replace the deprecated MacBytesENC method. (Chilkat is moving away from the use of CkByteData.)

Chilkat Java Downloads

Java
import com.chilkatsoft.*;

public class ChilkatExample {

  static {
    try {
        System.loadLibrary("chilkat");
    } catch (UnsatisfiedLinkError e) {
      System.err.println("Native code library failed to load.\n" + e);
      System.exit(1);
    }
  }

  public static void main(String argv[])
  {
    boolean success = false;

    CkCrypt2 crypt = new CkCrypt2();

    //  Do HMAC using SHA-256.
    crypt.put_MacAlgorithm("hmac");
    crypt.put_HashAlgorithm("sha256");

    String keyHex = "000102030405060708090A0B0C0D0E0F";
    success = crypt.SetMacKeyEncoded(keyHex,"hex");

    //  Return the HMAC using the lowercase hex encoding.
    crypt.put_EncodingMode("hex_lower");

    String path = "c:/someDir/example.dat";

    //  ------------------------------------------------------------------------
    //  The MacBytesENC method is deprecated:

    CkByteData inData = new CkByteData();
    inData.loadFile(path);

    String hmac_hex = crypt.macBytesENC(inData);
    System.out.println(hmac_hex);

    //  ------------------------------------------------------------------------
    //  Workaround.
    //  (Chilkat is moving away from using CkByteData)

    CkBinData bdIn = new CkBinData();
    bdIn.LoadFile(path);

    hmac_hex = crypt.macBdENC(bdIn);
    System.out.println(hmac_hex);
  }
}