Getting a proper MD5 for a file in the sys_attachment table

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-18-2018 08:03 AM
I am working on a solution to block known images (i.e. signature images/logos) from being added to records when an email comes in. I don't want to use a file size as a basis for blocking an image since that's risky.
I am trying to use this logic from https://community.servicenow.com/community?id=community_question&sys_id=917ffaa9db58dbc01dcaf3231f96... but getting poor results.
var StringUtil = GlideStringUtil;
var gsa = new GlideSysAttachment();
var b64 = StringUtil.base64Encode(gsa.getBytes(current));
var imgHash = (new GlideChecksum(b64)).getMD5();
The issue I'm running into is that the same exact image returns different bytes and therefore a different hashcode every time it is inserted into the attachment table from an email.
Does anyone have any ideas on this?
Thanks,
Todd
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-22-2019 01:06 PM
FYI: As of New York, a SHA256 is calculated and put into sys_attachment.hash field and this is something you can use.
See "Attachments are assigned unique identifiers" in the New York release notes.
Additionally if an email arrives with an attachment (image or otherwise), its hash is compared to other attachments on the target record that have a hash. If the identical content already exists on the target record, the attachment is not moved to the target record, but remains on the sys_email record.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-22-2019 01:19 PM

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-27-2025 07:45 AM - edited ‎05-27-2025 07:57 AM
Hi everbody,
Found out how ServiceNow is calculating the hash-field. The take the content stream for calculating the sha256 hex string.
You can check it / or use it with this script:
var attachmentSysId = '1a87e60d87f96a90b5ae0d040cbb355f';
gs.info(new GlideDigest().getSHA256HexFromInputStream(new GlideSysAttachment().getContentStream(attachmentSysId)).toLowerCase());