--- title: Sample Java digest algorithm for encryption description: This Java algorithm illustrates creating a digest token from an HTTP header. locale: en-US canonical_url: https://www.servicenow.com/docs/r/australia/platform-security/authentication/c\_SamJavaDigAlgEncrypt.html release: australia product: Authentication classification: authentication topic_type: concept last_updated: "2026-03-12" reading_time_minutes: 1 breadcrumb: [Digest token authentication, Token based authentication \(User logins\), Authentication, Access Management] --- # Sample Java digest algorithm for encryption This Java algorithm illustrates creating a digest token from an HTTP header. This sample assumes: - The web server supports Java - The hash computation method is SHA1 - The secret key value is abc123 - The unencrypted HTTP header name is user\_name Change the Java code to use another hash computation mechanism \(such as MD5\), change the secret key value, or HTTP header name. ``` public class DigestTest { private static final String MAC_ALG = "HmacSHA256"; fKey = {32 or more characters}; public byte[] getDigest(String acct) { try { byte[] bkey = fKey.getBytes(); byte[] data = acct.getBytes(); Mac mac = null; try { mac = Mac.getInstance(MAC_ALG); mac.init(new SecretKeySpec(bkey, MAC_ALG)); } catch (Exception e) { e.printStackTrace(); } byte[] sig = mac.doFinal(data); String signature = new String(sig); System.out.println("value:" + acct); System.out.println("digested value: " + signature); return sig; } catch (IllegalStateException e) { e.printStackTrace(); } return null; } public static void main(String[] args) { BASE64Encoder encoder = new BASE64Encoder(); DigestTest test = new DigestTest(); String userName = "user_name"; System.out.println("base 64 digest username: " + encoder.encode(test.getDigest(userName))); } } ```