I am generating CSR using java code, I have taken the code from online website journaldev.com
public byte[] generateCSR(final String sigAlg, final PublicKey publicKey, final PrivateKey
privateKey, final String organisationUrl) {
final ByteArrayOutputStream outStream = new ByteArrayOutputStream();
final PrintStream printStream = new PrintStream(outStream);
final X500Name x500Name = new X500Name("CN=" + organisationUrl);
final Signature signature = Signature.getInstance(sigAlg);
signature.initSign(privateKey);
final PKCS10 pkcs10 = new PKCS10(publicKey);
pkcs10.encodeAndSign(x500Name, signature);
pkcs10.print(printStream);
return outStream.toByteArray();
As you can see that above I have used printStream constructor, which takes ByteArrayOutputStream as a parameter. Because of that sonar is saying not to use this constructor as that constructor will use default charset of the system, Anyone is aware that How can I resolve this?
I have tried as an alternative code :
final Writer writer = new OutputStreamWriter(outStream, StandardCharsets.UTF_8);
final OutputStream out = new WriterOutputStream(writer,StandardCharsets.UTF_8);
final PrintStream printStream = new PrintStream(out, Boolean.FALSE, "UTF-8");
This code run, but it return an empty result. can you please help me out with this?