- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hi @Vaibhav Chouhan ,
Its working correctly, i modified the code and its working properly, thanks for the help.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
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.
