I am currently working on integrating with third-party organizations that require a digital signature to be added to the data in the request header.
After some research, I decided to utilize the jsrsasign.js
library for handling the digital signature process. However, I have been encountering issues with getting the correct result.
Here is the code snippet I have been using:
import { RSAKey, KEYUTIL, KJUR, hex2b64 } from 'jsrsasign'
export function createSignature (url) {
// Create RSAKey object
var rsa = new RSAKey()
let privateKey = '-----BEGIN PRIVATE KEY-----x-----END PRIVATE KEY-----'
// Convert key to proper format
rsa = KEYUTIL.getKey(privateKey)
// Create Signature object and set encryption algorithm
var sig = new KJUR.crypto.Signature({'alg': 'SHA256withRSA'})
// Initialize signature
sig.init(rsa)
console.log('***url***', url)
// Update string to be encrypted
sig.updateString(url)
// Generate ciphertext
var signature = hex2b64(sig.sign())
console.log('**signature**', signature)
return signature
}