package page.example;

import java.security.Key;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;

import org.wikiwebserver.core.WareHouse;
import org.wikiwebserver.handler.http.FormData;
import org.wikiwebserver.handler.http.HTTPException;
import org.wikiwebserver.handler.http.interfaces.HTTPResponder;
import page.config.SiteTemplatedPage;
import org.wikiwebserver.util.Cryptography;


import static org.wikiwebserver.html.HTMLHelper.*;

public class EncryptionPage extends SiteTemplatedPage implements HTTPResponder {
	
    public void generate() throws HTTPException {
        
        setTitle("Encryption Tool - WikiWebServer.org");

        String password = "WikiWebServer";
        String input = "Text to encrypt / decrypt...";
        
        String algorithm = "AES";
        String mode = "CBC";
        String padding = "PKCS7Padding";     
        
        byte[] ivBytes = new byte[16];
        byte[] keyBytes = WareHouse.getMD5(password);       

        try {

            Cipher cipher = Cryptography.getCipher(algorithm, mode, padding);
            
            FormData formData = getFormData();
            if (formData != null) {
                String action = formData.getFirst("action");
                
                password = formData.getFirst("password");
                input = formData.getFirst("input");
                
                IvParameterSpec ivSpec = new IvParameterSpec(ivBytes);   
                keyBytes = WareHouse.getMD5(password);
                Key key = Cryptography.getKey(algorithm, keyBytes);                 
                
                if (action.equals("Encrypt")) {
                    input = Cryptography.encryptString(input, cipher, key, ivSpec);
                }
                
                if (action.equals("Decrypt")) {
                    input = Cryptography.decryptString(input, cipher, key, ivSpec);
                }
            }

        }
        catch (Exception ex) {
            String msg = "Failed to perform encryption or decryption";
            throw new HTTPException(500, msg, ex);
        }
        
        // Avoid displaying null
        if (input == null) input = "";

        append(h(1, "Encryption Tool") +
               form(
                   h(2, "Password") +                        
                   textfield("password", password, "size='64'") +
                   p("MD5 of password will be used as key for cypher.") +
                   h(2, "Text") + 
                   textarea("input", WareHouse.escapeHTMLEntities(input),
                            "rows='5' style='width: 100%'") +
                   p("Encrypted text is base64 encoded") +
                   submitbutton("action", "Encrypt") + submitbutton("action", "Decrypt")
               )
        );
        
        String parameters = "IV (hex): " +  WareHouse.bytesToHexString(ivBytes) + LF +
                            "Key (hex): " + WareHouse.bytesToHexString(keyBytes) + LF +
                            "Algorithm: " + algorithm + LF +
                            "Mode: " + mode + LF +
                            "Padding: " + padding + LF +
                            "Strength: " + keyBytes.length * 8 + "bit" + LF;
                            
        append(h(2, "Parameters") + codeBox(parameters));
    } 
}
