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
qBut 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
qI came from NodeJS development, and I am a newbie to the intersystems. Thanks so much if you can give some enlightenment to me.
Comments
Try working directly with the private key file, for example:
#include %msql s f=##class(%Stream.FileBinary).%New() s f.Filename="С:\your_private_key.pem" s privateKey=f.Read($$$MaxLocalLength) s myString = "text to sign" s signedTxt = ##class(%SYSTEM.Encryption).RSASHASign(256, $zcvt(myString,"O","UTF8"), privateKey) zw signedTxt
This code works for me.
You are a life savor indeed, it works. Thank you so much. ![]()