Sample code for 30+ languages & platforms
Java

HMAC SHA-1 Matching FIPS Examples

See more Encryption Examples

Provides an example of computing an HMAC-SHA1 digest to duplicate the FIPS examples at http://csrc.nist.gov/publications/fips/fips198/fips-198a.pdf

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[])
  {
    // This example assumes the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    CkCrypt2 crypt = new CkCrypt2();

    String strToSign = "Sample #1";

    // 64-byte key
    String key = "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f";

    // The expected result:
    // (from Appendix A.1 at http://csrc.nist.gov/publications/fips/fips198/fips-198a.pdf )
    String expectedResult = "4f4ca3d5d68ba7cc0a1208c9c61e9c5da0403c0a";

    // Here is the code to duplicate the results:
    crypt.put_HashAlgorithm("sha-1");
    crypt.put_EncodingMode("hex");
    crypt.put_MacAlgorithm("hmac");

    crypt.SetMacKeyEncoded(key,"hex");

    String mac;
    mac = crypt.macStringENC(strToSign);

    System.out.println("Computed: " + mac);
    System.out.println("Expected: " + expectedResult);
  }
}