// Path: src/main/java/info/interactivesystems/gamificationengine/api/exeption/ApiError.java
// @ApplicationException
// public class ApiError extends WebApplicationException {
// 
// 	private static final long serialVersionUID = 1L;
// 
// 	/**
// 	 * 
// 	 * @param status
// 	 *            a HTTP status code.
// 	 * @param message
// 	 *            a description of the error cause.
// 	 */
// 	public ApiError(Response.Status status, String message) {
// 		this(status, message, new Object[] {});
// 	}
// 
// 	/**
// 	 * @param status
// 	 *            a HTTP status code
// 	 * @param message
// 	 *            a description of the error cause, may be a formatted string.
// 	 * @param args
// 	 *            for format string, may be zero, see {@link String#format test}
// 	 *            .
// 	 */
// 	public ApiError(Response.Status status, String message, Object... args) {
// 		super(ResponseSurrogate.of(status, null, Notification.of(String.format(message, args))));
// 	}
// }


import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;
import java.util.UUID;
import javax.ws.rs.core.Response;
import info.interactivesystems.gamificationengine.api.exeption.ApiError;
package info.interactivesystems.gamificationengine.utils;




public class SecurityTools {

	private SecurityTools() {
	}

	/**
	 * Generates API-Key by creating a new universal unique identifier (UUID).
	 * TODO: Maybe change this to a encrypted timestamp/salt pair to decrypt
	 * creation date.
	 * 
	 * @return A randomly generated code used as API key
	 */
	public static String generateApiKey() {
		return UUID.randomUUID().toString();
	}

	/**
	 * Encodes a password to a encoded password with SHA 512.
	 * @param plainText
	 * 			The original password.
	 * @return An encoded password.
	 */
	//Based on http://stackoverflow.com/questions/3103652/hash-string-via-sha-256-in-java
	public static String encryptWithSHA512(String plainText) {
	       MessageDigest md;
		try {
			md = MessageDigest.getInstance("SHA-512");
			md.update(plainText.getBytes("UTF-8"));
			byte[] hashedPW = md.digest();
			
			String encoded = Base64.getEncoder().encodeToString(hashedPW);
			return encoded;
		} catch (NoSuchAlgorithmException | UnsupportedEncodingException e) {
// Path: src/main/java/info/interactivesystems/gamificationengine/api/exeption/ApiError.java
// @ApplicationException
// public class ApiError extends WebApplicationException {
// 
// 	private static final long serialVersionUID = 1L;
// 
// 	/**
// 	 * 
// 	 * @param status
// 	 *            a HTTP status code.
// 	 * @param message
// 	 *            a description of the error cause.
// 	 */
// 	public ApiError(Response.Status status, String message) {
// 		this(status, message, new Object[] {});
// 	}
// 
// 	/**
// 	 * @param status
// 	 *            a HTTP status code
// 	 * @param message
// 	 *            a description of the error cause, may be a formatted string.
// 	 * @param args
// 	 *            for format string, may be zero, see {@link String#format test}
// 	 *            .
// 	 */
// 	public ApiError(Response.Status status, String message, Object... args) {
// 		super(ResponseSurrogate.of(status, null, Notification.of(String.format(message, args))));
// 	}
// }


// Path: src/main/java/info/interactivesystems/gamificationengine/utils/SecurityTools.java
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;
import java.util.UUID;
import javax.ws.rs.core.Response;
import info.interactivesystems.gamificationengine.api.exeption.ApiError;

package info.interactivesystems.gamificationengine.utils;




public class SecurityTools {

	private SecurityTools() {
	}

	/**
	 * Generates API-Key by creating a new universal unique identifier (UUID).
	 * TODO: Maybe change this to a encrypted timestamp/salt pair to decrypt
	 * creation date.
	 * 
	 * @return A randomly generated code used as API key
	 */
	public static String generateApiKey() {
		return UUID.randomUUID().toString();
	}

	/**
	 * Encodes a password to a encoded password with SHA 512.
	 * @param plainText
	 * 			The original password.
	 * @return An encoded password.
	 */
	//Based on http://stackoverflow.com/questions/3103652/hash-string-via-sha-256-in-java
	public static String encryptWithSHA512(String plainText) {
	       MessageDigest md;
		try {
			md = MessageDigest.getInstance("SHA-512");
			md.update(plainText.getBytes("UTF-8"));
			byte[] hashedPW = md.digest();
			
			String encoded = Base64.getEncoder().encodeToString(hashedPW);
			return encoded;
		} catch (NoSuchAlgorithmException | UnsupportedEncodingException e) {
			throw new ApiError(Response.Status.FORBIDDEN, "The password cannot be hashed.");