How to convert a hexadecimal string? To make it a hash.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-16-2024 07:43 PM
I am creating a script in servicenow to hash from input with SHA-256.
I want to implement it in script include so that the result is the same as the hashed result in node.js, but the results do not match.
The hashing result in node.js is correct.
(It should be 027211735A51D527097D519D498EDFA037A1BF3D835934D0ED6D4D341D19D999)
I believe that input is a hexadecimal string and I need to do a binary conversion.
I do not know how to do the conversion in servicenow.
Is there a binary conversion method in servicenow javascript to get the same hash result as node.js?
â– ServiceNow(Script Include)
checkMessageDigest: function() {
var input = "6E581AD1299B32AE6AE9F81614C63F81";
var input_binary = "";
for (var n = 0; n < salt.length; n += 2) {
// Extract two characters from the hexadecimal string and convert them to their ASCII equivalent
input_binary += parseInt(salt.substring(n, 2), 16);
//input_binary =11088262094115550174106233248222019863129
}
//Execution of hashing
var digest = new GlideDigest();
var messageDigest = digest.getSHA256Hex(input_binary);
  //messageDigest = C17F32991D204D006280A68D2B787621ED97F9B82F53BD828E9DD2112EFDB8F0
}
â– NodeJS
var input = "6E581AD1299B32AE6AE9F81614C63F81";
const input_arr = Uint8Array.from(Buffer.from(input, 'hex'));
console.log("input_arr " , input_arr);
//input_arr Uint8Array(16) [
110, 88, 26, 209, 41,
155, 50, 174, 106, 233,
248, 22, 20, 198, 63,
129
]
//Execution of hashing
var messageDigest = crypto.createHash('sha256').update(input_arr).digest('hex');
console.log("messageDigest " , messageDigest.toUpperCase());
//messageDigest = 027211735A51D527097D519D498EDFA037A1BF3D835934D0ED6D4D341D19D999
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-16-2024 08:48 PM
Try this
var HexToSHA256 = Class.create();
HexToSHA256.prototype = {
initialize: function() {
},
checkMessageDigest: function() {
var input = "6E581AD1299B32AE6AE9F81614C63F81";
var input_binary = this.hexToBinary(input);
// Execution of hashing
var digest = new GlideDigest();
var messageDigest = digest.getSHA256(input_binary); // Using getSHA256 for binary input
return messageDigest.toUpperCase(); // Ensure the result is in uppercase
},
hexToBinary: function(hex) {
var binary = "";
for (var i = 0; i < hex.length; i += 2) {
var byte = parseInt(hex.substr(i, 2), 16);
binary += String.fromCharCode(byte);
}
return binary;
},
type: 'HexToSHA256'
};
Regards,
Pratiksha
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-16-2024 09:39 PM
Hi,Pratiksha !
Thanks for the reply.
I tried running it as per your reply.
But messageDigest is 2958EA8DB805990147AAD95740FDED8C9A901FF8188B6BF5BA7345A7704091E3.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-21-2024 11:58 PM
I put the binaryized value with String.fromCharCode into a Uint8Array array used in NodeJS.
As a result, we get the same value results as node.js up to 110, 88, and 26, but 209 and after do not match.
var input = "6E581AD1299B32AE6AE9F81614C63F81";
var binary = "";
for (var i = 0; i < input.length; i += 2) {
var byte = parseInt(input.substr(i, 2), 16);
binary += String.fromCharCode(byte);
}
console.log('binary ',binary); //nXÑ)›2®jéøÆ?
const input_arr = Uint8Array.from(Buffer.from(binary));
console.log('binary ',input_arr ); // Uint8Array(23) [
110, 88, 26, 195, 145, 41,
194, 155, 50, 194, 174, 106,
195, 169, 195, 184, 22, 20,
195, 134, 63, 194, 129
]
Does anyone have any idea what it is?