Message Digests

Updated: August 9, 2017

SmartKey can calculate standard message digests such as SHA-1 and SHA-256. However, since computing these hashes do not involve any cryptographic secrets, it is generally more convenient and more efficient to calculate non-authenticated message digests on the client. In java, the java.security.MessageDigest class can be used to compute message digests in code. Or programs such as sha1sum or sha256sum can be used to calculate message digests on the command line.

Prerequisites

Computing message digests requires an SmartKey account, a group, and an application configured in that group. See the SmartKey Developer’s Guide for more details.

Authorization and Configuration

You must first authenticate and optionally configure a default API client as described in Configure API Client and Client Authentication. Computing a message digest requires authenticating as an app with an API key or a client certificate. (User accounts cannot compute message digests).

Create a DigestApi Client Object

Message digests are computed using a DigestApi object.

import com.fortanix.sdkms.v1.api.DigestApi;

DigestApi digestApi = new DigestApi();

Create a DigestRequest

The DigestRequest object contains the requested message digest algorithm and the data to be hashed. The data should be binary data passed as an array of bytes.

import com.fortanix.sdkms.v1.model.DigestAlgorithm;
import com.fortanix.sdkms.v1.model.DigestRequest;

DigestRequest digestRequest = new DigestRequest().alg(DigestAlgorithm.<Algorithm name>).data(<input data as bytes[]>);

Make the Digest Call

The digest is computed by calling the computeDigest() method of a DigestApi() object with the DigestRequest. This method will return a DigestResponse containing the message digest. The message digest is returned in binary as an array of bytes. Note that command line tools typically return message digests as strings of hexidecimal digits. You will need to convert the output to this format if you want hexadecimal.

import com.fortanix.sdkms.v1.model.DigestResponse;

DigestResponse digestResponse = digestApi.computeDigest(digestRequest);
byte[] digest = digestResponse.getDigest();