The Now Platform® Washington DC release is live. Watch now!

Help
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Converting a base64 string to Hex

kclark1
Kilo Expert

Hey all,

I have a question which I feel should be relatively easy, but I haven't been able to get it running.

I would like a scripted method to turn a string into a Hexadecimal SHA1 encoded string.

We're using a scripted REST API to receive 3 incoming parameters - Timestamp, Nonce and Signature, then use a shared/known token value which we've captured in ServiceNow to generate a SHA1 hash.

So we do the following:

1. Receive the values

2. Construct the timestamp+nonce+token string

3. run it through a SHA1 function in a Script Include (taken from this thread Generating a proper WS-Security UsernameToken/PasswordDigest for Outgoing SOAP Calls? )

This returns a Base64 string which is the correctly encrypted value, but we need it to be output in Hex instead of Base64 (the Script Include is great, but doesn't have the Hex output option available).

I've been trying to use GlideStringUtil, and in particular the function toHexString() to try to convert the Base64 to Hex, but I feel that's probably not going to work either.

My first question is: is there an OOB function that we can call to generate the SHA1 hash in Hex?

If not, how do you think we might go about converting the Base64 to Hex?

Cheers,

Kevin

1 ACCEPTED SOLUTION

kclark1
Kilo Expert

And lo we have figured it out!  



The trick was to introduce the "hex" case into the SHA1 function that we were using in our Script Include...



Replacing these lines:



  1. case "":  
  2.   return "";  


With these lines:


  1. case "hex":  
  2. return this.rstr2hex(this._SHA1_rstr(input, utf8), '');  


We were then able to tell our scripted API to create the hash using the Hex case instead of the Base64 default.



Very grateful for Oliver Döserick's Script Include!!   (ping od)


Kevin


View solution in original post

1 REPLY 1

kclark1
Kilo Expert

And lo we have figured it out!  



The trick was to introduce the "hex" case into the SHA1 function that we were using in our Script Include...



Replacing these lines:



  1. case "":  
  2.   return "";  


With these lines:


  1. case "hex":  
  2. return this.rstr2hex(this._SHA1_rstr(input, utf8), '');  


We were then able to tell our scripted API to create the hash using the Hex case instead of the Base64 default.



Very grateful for Oliver Döserick's Script Include!!   (ping od)


Kevin