---
sourceDocument: Yokohama Platform security
sourceDocumentLink: https://servicenow-prod.fluidtopics.net/r/yokohama/platform-security

 Release :

    - yokohama

ft:locale :

    - en-US

ft:publication_title :

    - Yokohama Platform security

ft:clusterId :

    - psec

bundleId :

    - psec

workflow :

    - Platform


---

# Sample Java digest algorithm for encryption

# Sample Java digest algorithm for encryption {#ariaid-title1}

Release version: Yokohama  
Updated January 30, 2025  
![](https://www.servicenow.com/docs/portal-asset/ico-clock) 1 minute to read  
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
{#c_SamJavaDigAlgEncrypt__ul_fhj_tb4_1p}  
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)));
        }
    }


