Sample code for 30+ languages & platforms
Android™

Select the Hash Algorithm for Signed Email

See more Email Object Examples

Demonstrates the Chilkat Email.SigningHashAlg property, which selects the underlying hash algorithm used when sending signed (PKCS#7) email. Possible values are sha1, sha256, sha384, sha512, md5, and md2; the default is sha1. It controls the message-digest algorithm placed in the signature and is used only when a signed message is generated. This example selects SHA-256.

Background: A digital signature does not sign the whole message directly — it signs a hash, a short fixed-length fingerprint computed from the content. The hash algorithm's job is to make it infeasible to find two different messages with the same fingerprint. Older algorithms like md5 and sha1 are now considered weak (collisions have been demonstrated), so modern signing should prefer sha256 or stronger even though sha1 remains the historical default.

Chilkat Android™ Downloads

Android™
// Important: Don't forget to include the call to System.loadLibrary
// as shown at the bottom of this code sample.
package com.test;

import android.app.Activity;
import com.chilkatsoft.*;

import android.widget.TextView;
import android.os.Bundle;

public class SimpleActivity extends Activity {

  private static final String TAG = "Chilkat";

  // Called when the activity is first created.
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //  Demonstrates the Email.SigningHashAlg property, which selects the hash (message-digest)
    //  algorithm used when sending signed (PKCS#7) email.  Possible values: sha1, sha256,
    //  sha384, sha512, md5, md2.  The default is sha1.

    CkEmail email = new CkEmail();

    //  Select SHA-256 for the signature's message digest.
    email.put_SigningHashAlg("sha256");

    Log.i(TAG, "SigningHashAlg = " + email.signingHashAlg());

  }

  static {
      System.loadLibrary("chilkat");

      // Note: If the incorrect library name is passed to System.loadLibrary,
      // then you will see the following error message at application startup:
      //"The application <your-application-name> has stopped unexpectedly. Please try again."
  }
}