/*
 * Copyright (c) 2015, the IRMA Team
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice, this
 *   list of conditions and the following disclaimer.
 *
 *  Redistributions in binary form must reproduce the above copyright notice,
 *   this list of conditions and the following disclaimer in the documentation
 *   and/or other materials provided with the distribution.
 *
 *  Neither the name of the IRMA project nor the names of its
 *   contributors may be used to endorse or promote products derived from
 *   this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package org.irmacard.verification.web;

import io.jsonwebtoken.SignatureAlgorithm;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.security.*;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;

public class TokenKeyManager {
	private static PrivateKey key;

	public static PrivateKey getKey() throws KeyManagementException {
		if (key == null) {
			try {
				byte[] bytes = getResource("sk.der");
				if (bytes == null || bytes.length == 0)
					throw new KeyManagementException("Could not read private key");

				PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(bytes);

				key = KeyFactory.getInstance("RSA").generatePrivate(spec);
			} catch (IOException | NoSuchAlgorithmException | InvalidKeySpecException e) {
				throw new KeyManagementException(e);
			}
		}

		return key;
	}

	public static SignatureAlgorithm getAlgorithm() {
		return SignatureAlgorithm.RS256;
	}

	private static byte[] getResource(String filename) throws IOException {
		URL url = TokenKeyManager.class.getClassLoader().getResource("/" + filename);
		if (url == null)
			throw new IOException("Could not load private key");

		return convertSteamToByteArray(url.openStream(), 2048);
	}

	private static byte[] convertSteamToByteArray(InputStream stream, int size) throws IOException {
		byte[] buffer = new byte[size];
		ByteArrayOutputStream os = new ByteArrayOutputStream();

		int line;
		while ((line = stream.read(buffer)) != -1) {
			os.write(buffer, 0, line);
		}
		stream.close();

		os.flush();
		os.close();
		return os.toByteArray();
	}
}

@@ -1881,16 +1881,62 @@
 KeySpec;
+%0Aimport java.security.spec.X509EncodedKeySpec;
 %0A%0Apublic
@@ -1991,16 +1991,53 @@
 Key key;
+%0A%09private static PublicKey publicKey;
 %0A%0A%09publi
@@ -2561,16 +2561,566 @@
 ey;%0A%09%7D%0A%0A
+%09public static PublicKey getPublicKey() throws KeyManagementException %7B%0A%09%09if (publicKey == null) %7B%0A%09%09%09try %7B%0A%09%09%09%09byte%5B%5D bytes = getResource(%22pk.der%22);%0A%09%09%09%09if (bytes == null %7C%7C bytes.length == 0)%0A%09%09%09%09%09throw new KeyManagementException(%22Could not read public key%22);%0A%0A%09%09%09%09X509EncodedKeySpec spec = new X509EncodedKeySpec(bytes);%0A%0A%09%09%09%09publicKey = KeyFactory.getInstance(%22RSA%22).generatePublic(spec);%0A%09%09%09%7D catch (IOException %7C NoSuchAlgorithmException %7C InvalidKeySpecException e) %7B%0A%09%09%09%09throw new KeyManagementException(e);%0A%09%09%09%7D%0A%09%09%7D%0A%0A%09%09return publicKey;%0A%09%7D%0A%0A
 %09public 
@@ -3341,14 +3341,8 @@
 rce(
-%22/%22 + 
 file
@@ -3408,28 +3408,33 @@
 ot load 
-private key%22
+file %22 + filename
 );%0A%0A%09%09re

Add getPublicKey() method for unit tests