We've updated the ServiceNow Community Code of Conduct, adding guidelines around AI usage, professionalism, and content violations. Read more

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

1 ACCEPTED SOLUTION

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

4 REPLIES 4

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

YashwanthV18760
Giga Guru

Hi @AkshataP30 

 

To calculate the SHA-256 hash of a file in ServiceNow and get a fixed value (same every time for the same file), you can use the built-in GlideDigest API, which supports SHA-256 hashing.


Here is a sample script you can use in a Script Include or Background Script to calculate the SHA-256 hash of a file's content stored in a ServiceNow attachment or any string content:


```javascript
// Function to calculate SHA-256 hash of a string or file content

function getSHA256Hash(input) {
var digest = new GlideDigest();
var hash = digest.getSHA256(input);
return hash.toUpperCase(); // To match your command prompt output format
}

// Example usage:
// If you have the file content as a string, pass it directly
var fileContent = "Your file content here"; // Replace with actual file content

var sha256Hash = getSHA256Hash(fileContent);
gs.info("SHA256 Hash: " + sha256Hash);
```

If you want to hash an attachment file content:



javascript <br />var attachmentSysId = 'your_attachment_sys_id_here'; // Replace with actual attachment sys_id <br />var attachment = new GlideSysAttachment(); <br />var stream = attachment.getContentStream(attachmentSysId); <br /> <br />var reader = new Packages.java.io.InputStreamReader(stream); <br />var bufferedReader = new Packages.java.io.BufferedReader(reader); <br /> <br />var fileContent = ''; <br />var line; <br />while ((line = bufferedReader.readLine()) != null) { <br /> fileContent += line + '\n'; <br />} <br />bufferedReader.close(); <br /> <br />var digest = new GlideDigest(); <br />var sha256Hash = digest.getSHA256(fileContent); <br /><a href="http://gs.info" target="_blank" style="display: inline-block">gs.info</a>("SHA256 Hash: " + sha256Hash.toUpperCase()); <br />

Notes:

  • The hash will be consistent (fixed) for the same file content.
  • Make sure you read the file content exactly the same way each time (no extra spaces or line breaks added).
  • The output is converted to uppercase to match the format you showed from the command prompt.


If you want to hash a file on the server file system (outside ServiceNow), you would need to upload it as an attachment or read it via an integration, since ServiceNow scripts cannot directly access server file systems.