function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
hu yanghu yang 

How to use "Crypto.sign"

When i use the sample code that is provided by salesforce,I got a error "System.SecurityException: Invalid Crypto Key".
Who can tell me how to use "Crypto.sign".
Thanks.

The blow is the sample code:
String algorithmName = 'RSA';
String key = 'pkcs8 format private key';
Blob privateKey = EncodingUtil.base64Decode(key);
Blob input = Blob.valueOf('12345qwerty');
Crypto.sign(algorithmName, input,privateKey);
Hargobind_SinghHargobind_Singh
You need a valid pksc8 format key. You can use OpenSSL to generate one. 

Here is some documentaiton: http://www.openssl.org/docs/apps/openssl.html

And, sample commands: 

openssl genrsa -out mykey.pem 1024
openssl pkcs8 -topk8 -nocrypt -in mykey.pem -outform PEM  > pkcs8.key