| 318 | } |
| 319 | |
| 320 | private static byte[] rsaSign(String data, String pemPrivateKey, String hashAlgo) { |
| 321 | String jce = switch (hashAlgo) { |
| 322 | case "sha1" -> "SHA1withRSA"; |
| 323 | case "sha256" -> "SHA256withRSA"; |
| 324 | case "sha384" -> "SHA384withRSA"; |
| 325 | case "sha512" -> "SHA512withRSA"; |
| 326 | case "md5" -> "MD5withRSA"; |
| 327 | default -> throw new IllegalArgumentException("Invalid hash algorithm name: " + hashAlgo); |
| 328 | }; |
| 329 | |
| 330 | try { |
| 331 | PrivateKey key = readRSAPrivateKeyFromPem(pemPrivateKey); |
| 332 | Signature sig = Signature.getInstance(jce); |
| 333 | sig.initSign(key); |
| 334 | sig.update(toUtf8(data)); |
| 335 | return sig.sign(); |
| 336 | } catch (Exception e) { |
| 337 | throw new RuntimeException(e); |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | |
| 342 | private static PrivateKey readRSAPrivateKeyFromPem(String pem) throws Exception { |