// Path: src/main/java/net/seleucus/wsp/crypto/WebSpaEncoder.java
// public class WebSpaEncoder {
// 
// 	private CharSequence passPhrase;	
// 	private int actionNumber;
// 	
// 	public WebSpaEncoder(CharSequence passPhrase, int actionNumber) {
// 		this.passPhrase = passPhrase;
// 		this.actionNumber = actionNumber;
// 	}
// 	
// 	public String getKnock() {
// 		byte[] passKnockBytes = PassPhraseCrypto.getHashedPassPhraseNow(passPhrase);
// 		byte[] actionKnockBytes = ActionNumberCrypto.getHashedActionNumberNow(passPhrase, actionNumber);
// 		
// 		byte[] allBytes = ArrayUtils.addAll(passKnockBytes, actionKnockBytes);
// 		
// 		return Base64.encodeBase64URLSafeString(allBytes);
// 	}
// 
// 	public static String encode(CharSequence passPhrase) {
// 		byte[] passKnockBytes = PassPhraseCrypto.getHashedPassPhraseNow(passPhrase);
// 		return Base64.encodeBase64URLSafeString(passKnockBytes);
// 	}
// 
// 	public static boolean matches(CharSequence rawPassword, String webSpaRequest) {
// 
// 		if(webSpaRequest.length() != 100) {
// 			
// 			return false;
// 			
// 		} else {
// 			
// 			byte[] webSpaBytes = Base64.decodeBase64(webSpaRequest);
// 			byte[] passBytes = ArrayUtils.subarray(webSpaBytes, 0, 51);
// 			byte randomByte = webSpaBytes[0];
// 			
// 			byte[] expectedBytes = PassPhraseCrypto.getHashedPassPhraseNowWithSalt(rawPassword, randomByte);
// 			
// 			return WebSpaUtils.equals(passBytes, expectedBytes);
// 		}
// 	}
// 	
// 	public static int getActionNumber(CharSequence rawPassword, String webSpaRequest) {
// 		
// 		int returnAction = -1;
// 		
// 		if(WebSpaEncoder.matches(rawPassword, webSpaRequest)) {
// 			
// 			byte[] webSpaBytes = Base64.decodeBase64(webSpaRequest);
// 			byte[] actionBytes = ArrayUtils.subarray(webSpaBytes, 51, 75);
// 			byte[] actionSalt = ArrayUtils.subarray(actionBytes, 0, 4);
// 						
// 			for(int count = 0; count <= 9; count++) {
// 				
// 				byte[] calculatedActionBytes = 
// 						ActionNumberCrypto.getHashedActionNumberNowWithSalt(rawPassword, count, actionSalt);
// 				
// 				if(WebSpaUtils.equals(calculatedActionBytes, actionBytes)) {
// 					returnAction = count;
// 					break;
// 				}
// 				
// 			} // for loop
// 		}
// 		
// 		return returnAction;
// 	}
// 
// }


import java.nio.CharBuffer;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import net.seleucus.wsp.crypto.WebSpaEncoder;
			preStatement.setString(3, osCommand);
			
			preStatement.executeUpdate();
			
			preStatement.close();
			
		} catch (SQLException ex) {
			
			 LOGGER.error("Add Add Action - A Database exception has occured: {}.", ex.getMessage());
			
		}
		
	}

	public synchronized int getActionNumberFromRequest(final int ppID, final String webSpaRequest) {
		
		int actionNumber = -1;
		
		if(ppID > 0) {
			
			String sqlActivationLookup = "SELECT PASSPHRASE FROM PASSPHRASES WHERE PPID = ? ;";
			try {
				PreparedStatement psPassPhrase = wsConnection.prepareStatement(sqlActivationLookup);
				psPassPhrase.setInt(1, ppID);
				ResultSet rs = psPassPhrase.executeQuery();
				
				if (rs.next()) {
					char[] dbPassPhraseArray = rs.getString(1).toCharArray();
					CharSequence rawPassword = CharBuffer.wrap(dbPassPhraseArray);
					
// Path: src/main/java/net/seleucus/wsp/crypto/WebSpaEncoder.java
// public class WebSpaEncoder {
// 
// 	private CharSequence passPhrase;	
// 	private int actionNumber;
// 	
// 	public WebSpaEncoder(CharSequence passPhrase, int actionNumber) {
// 		this.passPhrase = passPhrase;
// 		this.actionNumber = actionNumber;
// 	}
// 	
// 	public String getKnock() {
// 		byte[] passKnockBytes = PassPhraseCrypto.getHashedPassPhraseNow(passPhrase);
// 		byte[] actionKnockBytes = ActionNumberCrypto.getHashedActionNumberNow(passPhrase, actionNumber);
// 		
// 		byte[] allBytes = ArrayUtils.addAll(passKnockBytes, actionKnockBytes);
// 		
// 		return Base64.encodeBase64URLSafeString(allBytes);
// 	}
// 
// 	public static String encode(CharSequence passPhrase) {
// 		byte[] passKnockBytes = PassPhraseCrypto.getHashedPassPhraseNow(passPhrase);
// 		return Base64.encodeBase64URLSafeString(passKnockBytes);
// 	}
// 
// 	public static boolean matches(CharSequence rawPassword, String webSpaRequest) {
// 
// 		if(webSpaRequest.length() != 100) {
// 			
// 			return false;
// 			
// 		} else {
// 			
// 			byte[] webSpaBytes = Base64.decodeBase64(webSpaRequest);
// 			byte[] passBytes = ArrayUtils.subarray(webSpaBytes, 0, 51);
// 			byte randomByte = webSpaBytes[0];
// 			
// 			byte[] expectedBytes = PassPhraseCrypto.getHashedPassPhraseNowWithSalt(rawPassword, randomByte);
// 			
// 			return WebSpaUtils.equals(passBytes, expectedBytes);
// 		}
// 	}
// 	
// 	public static int getActionNumber(CharSequence rawPassword, String webSpaRequest) {
// 		
// 		int returnAction = -1;
// 		
// 		if(WebSpaEncoder.matches(rawPassword, webSpaRequest)) {
// 			
// 			byte[] webSpaBytes = Base64.decodeBase64(webSpaRequest);
// 			byte[] actionBytes = ArrayUtils.subarray(webSpaBytes, 51, 75);
// 			byte[] actionSalt = ArrayUtils.subarray(actionBytes, 0, 4);
// 						
// 			for(int count = 0; count <= 9; count++) {
// 				
// 				byte[] calculatedActionBytes = 
// 						ActionNumberCrypto.getHashedActionNumberNowWithSalt(rawPassword, count, actionSalt);
// 				
// 				if(WebSpaUtils.equals(calculatedActionBytes, actionBytes)) {
// 					returnAction = count;
// 					break;
// 				}
// 				
// 			} // for loop
// 		}
// 		
// 		return returnAction;
// 	}
// 
// }


// Path: src/main/java/net/seleucus/wsp/db/WSActionsAvailable.java
import java.nio.CharBuffer;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import net.seleucus.wsp.crypto.WebSpaEncoder;

			preStatement.setString(3, osCommand);
			
			preStatement.executeUpdate();
			
			preStatement.close();
			
		} catch (SQLException ex) {
			
			 LOGGER.error("Add Add Action - A Database exception has occured: {}.", ex.getMessage());
			
		}
		
	}

	public synchronized int getActionNumberFromRequest(final int ppID, final String webSpaRequest) {
		
		int actionNumber = -1;
		
		if(ppID > 0) {
			
			String sqlActivationLookup = "SELECT PASSPHRASE FROM PASSPHRASES WHERE PPID = ? ;";
			try {
				PreparedStatement psPassPhrase = wsConnection.prepareStatement(sqlActivationLookup);
				psPassPhrase.setInt(1, ppID);
				ResultSet rs = psPassPhrase.executeQuery();
				
				if (rs.next()) {
					char[] dbPassPhraseArray = rs.getString(1).toCharArray();
					CharSequence rawPassword = CharBuffer.wrap(dbPassPhraseArray);
					
					actionNumber = WebSpaEncoder.getActionNumber(rawPassword, webSpaRequest);