Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Decode Base64 to String using Cryptography in SNOW

Rajanmehta
Mega Guru

Hello 

We are on Jakarta version. I have a need to convert native Powershell script to SNOW script. (code snippet below)

I need to create signature and headers as below using Cryptography and then pass it to URL with JSON body.

All other variables are passes into the script. I need to generate Signature and Headers.

It works fine with Powershell command. Is there a way to use the same code in SNOW script? 

If yes, please advise how do I achieve that? 

Thanks,

 

find_real_file.png

 

 

5 REPLIES 5

Brad Tilton
ServiceNow Employee
ServiceNow Employee

I'm not too familiar with powershell, but you should be able to do a gs.base64Decode(encodedStr).

Thanks Brad for your response. 

Yes I can use that function but I believe it wouldn't incorporate same level of security as being used in Powershell script (Cryptography and SHA256) 

But I appreciate your response and time to help me out.

 

Thanks,

Rajan Mehta

 

terrywang
ServiceNow Employee
ServiceNow Employee

Yes you can run this script using Orchestration Custom PowerShell Activity.

1> Make sure plugin Orchestration is activated

2> Navigate to Orchestration > MID Server Script Files > click on New > copy and paste the script in "script" box, and make sure the name of the script file ends with .ps1.  Also leave other options empty / unchecked

3> Create a custom PowerShell activity to run this script file

https://docs.servicenow.com/bundle/london-servicenow-platform/page/administer/orchestration-activity-designer/task/create-custom-activities.html

 

=== Example ===

I tried below on my lab instance:

$accesskey = "asdfasdfasdf"
$StorageAccount = "jkljkljkl"
$resource = "vm"

$GMTTime = (get-date).ToUniversalTime().toString('R')
$stringToSign = "$GMTTime`n/$StorageAccount/$resource"
$hmacsha = new-object system.security.cryptography.HMACSHA256
$hmacsha.key = [convert]::FromBase64String($accesskey)
$signature = $hmacsha.ComputeHash([Text.Encoding]::UTF8.GetBytes($stringToSign))
$Signature = [Convert]::ToBase64String($signature)
$headers = @{
'x-ms-date' = $GMTTime
'x-ms-version' = "2018-03-28"
'Authorization' = "SharedKeyLite " + $storageAccount + ":" + $signature
'Accept' = "application/json;odata=fullmetadata"
}
$headers

 

=== Output ===

Name Value
---- -----
x-ms-version 2018-03-28
x-ms-date Wed, 08 Aug 2018 01:56:15 GMT
Authorization SharedKeyLite jkljkljkl:QvwYYY9Is/cEKWGhpiwAf...
Accept application/json;odata=fullmetadata

 

 

Please Mark as answered, or let me know if you need more information.

Thanks,

T

terrywang
ServiceNow Employee
ServiceNow Employee

Please mark as answer if this helps, or let me know if you need more information.

Thanks,

T