SHA 256 value calculation for a file in servicenow

AkshataP30
Tera Contributor

I want to calculate sha 256 value for a file in servicenow the below i got from command prompt teh sha 256 value the same i need to get in servicenow, the value should not change on each run it should be fixed

 

Algorithm Hash Path
--------- ---- ----
SHA256 BDD1A33DE78618D16EE4CE148B849932C05D0015491C34887846D431D29F308E C:\Users

2 ACCEPTED SOLUTIONS

Vaibhav Chouhan
Tera Guru

Yes, you can achieve this in ServiceNow, and the correct approach for your requirement is to use GlideDigest with getSHA256HexFromInputStream.

Since you want the SHA-256 value of a file and it must match what you get from the command prompt, you should hash the raw file content, not a string version of it. The best way to do that is to read the attachment as a content stream and pass it into GlideDigest.

Example:

var inputStream = new GlideSysAttachment().getContentStream(attachmentSysID);
var digest = new GlideDigest();
var hash = digest.getSHA256HexFromInputStream(inputStream);
gs.info(hash);

This method generates the SHA 256 hash in hexadecimal format and will produce the same fixed value every time for the same file, just like the OS level SHA 256 calculation.

Official Documentation: GlideDigest | ServiceNow Developers

View solution in original post

Hi @AkshataP30 ,

The hash E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855 is actually the standard SHA-256 value of an empty file. I recognized it because that’s the well-known digest you always get when no data is passed into the hashing function.

So, in the instance where you’re seeing that value, it most likely means the input stream returned by getContentStream() is empty or not actually reading the attachment content.

View solution in original post

6 REPLIES 6

Vaibhav Chouhan
Tera Guru

Yes, you can achieve this in ServiceNow, and the correct approach for your requirement is to use GlideDigest with getSHA256HexFromInputStream.

Since you want the SHA-256 value of a file and it must match what you get from the command prompt, you should hash the raw file content, not a string version of it. The best way to do that is to read the attachment as a content stream and pass it into GlideDigest.

Example:

var inputStream = new GlideSysAttachment().getContentStream(attachmentSysID);
var digest = new GlideDigest();
var hash = digest.getSHA256HexFromInputStream(inputStream);
gs.info(hash);

This method generates the SHA 256 hash in hexadecimal format and will produce the same fixed value every time for the same file, just like the OS level SHA 256 calculation.

Official Documentation: GlideDigest | ServiceNow Developers

Hi Vaibhav, 

 

I tried in 2 different instances, in one instance it was same like OS level SHA 256 calculation. but in another instance it is different for same file

Hi @Vaibhav Chouhan 

 

 

In one instance E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855

 

another instance

correct hash -  BDD1A33DE78618D16EE4CE148B849932C05D0015491C34887846D431D29F308E

Hi @AkshataP30 ,

The hash E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855 is actually the standard SHA-256 value of an empty file. I recognized it because that’s the well-known digest you always get when no data is passed into the hashing function.

So, in the instance where you’re seeing that value, it most likely means the input stream returned by getContentStream() is empty or not actually reading the attachment content.