Question
· Feb 16, 2023

How to Sign a String with RSA SHA256?

So I have a base string that I want to sign using RSA-SHA256. I have a .p12 file and passphrase to get the RSA Private key using NodeJS (pem.readPkcs12 library), which I don't know how to do that in intersystems as well. (would appreciate if you can include a solution for that too)

The main problem here is I am trying to sign a string and print the result to terminal, using the code below in a routine (.mac file).

SignTest
    s privateKey = "-----BEGIN RSA PRIVATE KEY-----\r\nsomeKey\r\nsomeKey\r\n-----END RSA PRIVATE KEY-----"
    s myString  = "text to sign"
    s signedTxt = ##class(%SYSTEM.Encryption).RSASHASign(256, myString, privateKey)
    zw signedTxt
    q

But when I run it in the terminal, the output is an empty string. What is wrong here?

I have tried %SYS.X509Credentials class, and RSASHASign method as well, but still cannot get around to the expected result. The code is below.

signTest
    s signer = ##class(%SYS.X509Credentials).%New()
    s privateKey = "-----BEGIN RSA PRIVATE KEY-----\r\nsomeKey\r\nsomeKey\r\n-----END RSA PRIVATE KEY-----"
    s myString  = "text to sign"
    s signer.PrivateKey = privateKey
    s signedText = signer.RSASHASign(256, txt)
    zw signedText
    q

I came from NodeJS development, and I am a newbie to the intersystems. Thanks so much if you can give some enlightenment to me.

Product version: IRIS 2022.2
$ZV: IRIS for Windows (x86-64) 2022.2 (Build 368U) Fri Oct 21 2022 16:44:33 EDT
Discussion (2)2
Log in or sign up to continue

Try working directly with the private key file, for example:

#include %msql

f=##class(%Stream.FileBinary).%New()
f.Filename="С:\your_private_key.pem"
privateKey=f.Read($$$MaxLocalLength)
 
myString  "text to sign"
signedTxt ##class(%SYSTEM.Encryption).RSASHASign(256, $zcvt(myString,"O","UTF8"), privateKey)
zw signedTxt

This code works for me.