Create SHA -256 Hash for File / String in PeopleCode - Example

In this tutorial, we will explain how to create SHA-256 hash for a File / String in PeopleSoft with a PeopleCode example program. We will make use of java.security.MessageDigest class to generate SHA-256 hash. With minimum customization effort, you should be able to stick the code provided in this blog into a page / record field / Application Engine PeopleCode depending on your needs. The code is fully commented so you would be able to understand it better - you can always post us a question back if you are stuck somewhere.

1. Create SHA-256 Hash for a File - PeopleCode Example


The Peoplecode snippet that you can use to create SHA-256 hash for a file is provided below:

/* Calculate SHA-256 Checksum for a File in PeopleCode */
/* Use Java Object to retrive input File as FileInputStream object */
Local JavaObject &InputSHA256Reader = CreateJavaObject("java.io.FileInputStream", "c:\temp\TESTER.zip");
/* Create Message Digest Java Object with an instance of SHA-256 */
Local JavaObject &messageDigest = GetJavaClass("java.security.MessageDigest").getInstance("SHA-256");
/* Create Java Array to assist in Hashing */
Local JavaObject &ByteBufferArray = CreateJavaArray("byte[]", 1024);
/* Read Input File Byte by Byte */
Local number &NumberOfBytes = &InputSHA256Reader.read(&ByteBufferArray);
/* Keep adding the bytes to SHA Digest calculator until all the bytes are read from the file */
While &NumberOfBytes > 0
   &messageDigest.update(&ByteBufferArray, 0, &NumberOfBytes);
   &NumberOfBytes = &InputSHA256Reader.read(&ByteBufferArray);
End-While;
/* Calculate SHA Digest */
Local JavaObject &bytearray = &messageDigest.digest();
/* Convert the SHA Digest to Hex String using Apache Commons Codec Library */
Local string &SHAHex = GetJavaClass("org.apache.commons.codec.binary.Hex").encodeHexString(&bytearray);
/* Output the SHA -256 encoded value */
MessageBox(0, "", 0, 0, "" | &SHAHex);


2. PeopleCode program - Generate SHA-256 Checksum for a String


With minor modifications, you can change this code to read an input string and provide SHA hash for the same. This code is provided in this section.

/* Calculate SHA-256 Checksum for an Input String in PeopleCode*/
/* Create Message Digest Java Object with an instance of SHA-256 */
Local JavaObject &messageDigest = GetJavaClass("java.security.MessageDigest").getInstance("SHA-256");
/* Create Java Array to assist in Hashing */
Local JavaObject &ByteBufferArray = CreateJavaArray("byte[]", 1024);
/* Accept input string as an object of type java.lang.string */
Local JavaObject &inputString = CreateJavaObject("java.lang.String", "input");
/* Get the bytes from the string into a byte array in PeopleCode */
&ByteBufferArray = &inputString.getBytes();
/* Call Digest.update to compute the digest information */
&messageDigest.update(&ByteBufferArray);
/* Calculate SHA Digest for the generated output */
Local JavaObject &bytearray = &messageDigest.digest();
/* Convert the SHA Digest to Hex String using Apache Commons Codec Library */
Local string &SHAHex = GetJavaClass("org.apache.commons.codec.binary.Hex").encodeHexString(&bytearray);
/* Output the SHA -256 encoded value */
MessageBox(0, "", 0, 0, "" | &SHAHex);

3. Using Java Stored Procedures through PeopleCode


This is an alternative option to generate SHA-256 hash from PeopleCode. It assumes that you can write some Java and create a class file in your Oracle Database that you can wrap into a function - you can then invoke the function straight from PeopleCode using SQLEXEC which would give the same result. You may want to read our earlier blog post that explained how to create SHA-256 hash in Oracle using PL/SQL before starting this section.
SQLExec("select HR.SHAVAL('input') from dual", &SHAHex);
MessageBox(0, "", 0, 0, "" | &SHAHex);

Try these PeopleCode techniques to create SHA checksum for your input and post in the comments section - if you are stuck.

Note: You need Apache Commons Codec JAR files to run the program presented in this post, you can get this from Apache foundation. Please make sure this file is available in your classpath.


1 comment:

  1. Hi,

    I am trying these code but encountered the following error
    Java Exception: java.lang.NoClassDefFoundError: org/apache/commons/codec/binary/Hex: finding class org.apache.commons.codec.binary.Hex

    I am purely a PS developer and not knowledgeable with Java, how can I call the missing java class mentioned? Thanks

    ReplyDelete