Skip to content

Commit

Permalink
Fixes #1050
Browse files Browse the repository at this point in the history
Signed-off-by: Kalin Chan <kalin.chan@payara.fish>
  • Loading branch information
kalinchan committed May 31, 2022
1 parent c822731 commit dbe4db7
Showing 1 changed file with 25 additions and 18 deletions.
Expand Up @@ -11,18 +11,21 @@

package ee.jakarta.tck.core.rest.jsonb.cdi;

import jakarta.annotation.PostConstruct;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.inject.Produces;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.PublicKey;
import java.security.spec.EncodedKeySpec;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;

import jakarta.annotation.PostConstruct;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.inject.Produces;

@ApplicationScoped
public class KeysProducer {

Expand All @@ -33,22 +36,26 @@ public class KeysProducer {
* @throws Exception - on failure to read or initialize public key
*/
@PostConstruct
private void loadKeys() throws Exception {
byte[] pubKeyData;
try (InputStream keyIS = getClass().getResourceAsStream("/key.pub")) {
if (keyIS == null) {
throw new IllegalStateException("Failed to find /key.pub");
private void loadKeys() {
try {
byte[] pubKeyData;
try (InputStream keyIS = getClass().getResourceAsStream("/key.pub")) {
if (keyIS == null) {
throw new IllegalStateException("Failed to find /key.pub");
}
pubKeyData = keyIS.readAllBytes();
}
pubKeyData = keyIS.readAllBytes();
String pubKeyString = new String(pubKeyData, StandardCharsets.UTF_8);
System.out.println(pubKeyString);
byte[] keyData = Base64.getDecoder().decode(pubKeyString);
EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(keyData);
System.out.println(publicKeySpec);

KeyFactory keyFactory = KeyFactory.getInstance("EC");
publicKey = keyFactory.generatePublic(publicKeySpec);
} catch (IOException | NoSuchAlgorithmException | InvalidKeySpecException exception) {
System.out.println(exception.getStackTrace());
}
String pubKeyString = new String(pubKeyData, StandardCharsets.UTF_8);
System.out.println(pubKeyString);
byte[] keyData = Base64.getDecoder().decode(pubKeyString);
EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(keyData);
System.out.println(publicKeySpec);

KeyFactory keyFactory = KeyFactory.getInstance("EC");
publicKey = keyFactory.generatePublic(publicKeySpec);
}

@Produces
Expand Down

0 comments on commit dbe4db7

Please sign in to comment.