SHA256WithRSA signatute in k6

Hello experts,
Here is a requirement to sign a private key and text with “SHA256WithRSA” algo. But after doing many search over the documentation of k6 I didn’t find any algo “SHA256WithRSA”

Link: k6/crypto

Hi there,

sorry, this scheme is not supported natively in k6. A workaround might be to do the signing outside of k6 and then load the file with open() in the script.

1 Like

Hi @sanjay.bansal57,

One way to include external modules is by bundling modules. Apart from that, have a look at these JavaScript libraries and try to see if you can make it happen:

  1. sha.js
  2. hash.js
  3. crypto-js

Hope it helps you achieve your goals.

1 Like

Just to elaborate a bit more on @mostafa’s suggestion:

Those 3 libraries are pure javascript, which is a requirement to be able to bundle them (without external dependencies at runtime) using for instance webpack. As such, they might have serious performance implications, due to the fact that cryptographic work isn’t that well-suited for single-threaded, interpreted languages.

Happy testing! :tada:

1 Like

Hi, I have this problem also and find to using modules but when running the test it’s stuck. Did you found the solution?

Hi @muhsin,

Can you provide more information, so that I can help you?

Hi @mostafa,

I have an issue about signing with RS256. Currently k6 crypto modules only support HMAC. Then I’m trying to import modules node-rsa that support this functionality and bundle the js with a webpack. But, when I run k6 run with the bundle it’s stuck and do nothing.

If you have any idea how to implement RS256 in k6 with another way, I’m happy to listen also.

note: I need the RS256 signing result will be use as one of the parameter in the header for the http request.

k6 does not support NodeJS API, therefore this package won’t work on k6.

@mostafa any alternative way in k6 if I want to use RS256 for signing?

Try this one:

The last resort would be to wait for the plug-in support PR to be merged.

@mostafa thx for the advice. With some small modification in jsencrypt (take out window and navigator stuff), now I’m able to execute rs256.

Thanks for the guidance.

1 Like

This is also worth being mentioned here if you want a complete example, although it uses crypto-js and not jsencrypt:

done to post the code there :slight_smile:

1 Like

Hi @muhsin

Seems like the code you’ve posted in github is not fully JWT compatible - for example, it seems like you’re signing only the payload without an header. is it intentional?

thanks :pray: