package net.thechunk.playpen.utils;

import net.thechunk.playpen.protocol.Protocol;
import org.apache.commons.codec.binary.Hex;
import org.jasypt.encryption.pbe.StandardPBEByteEncryptor;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class AuthUtils {

    private static StandardPBEByteEncryptor getEncryptor(String key)
    {
        // Many of jasypt's defaults are truly scary. This makes things less scarier but I still have no confidence in it.
        StandardPBEByteEncryptor encryptor = new StandardPBEByteEncryptor();
        encryptor.setPassword(key);
        encryptor.setKeyObtentionIterations(1500);
        encryptor.setAlgorithm("PBEwithSHA1AndRC4_128");
        return encryptor;
    }

    public static byte[] encrypt(byte[] bytes, String key)
    {
        StandardPBEByteEncryptor encryptor = getEncryptor(key);
        return encryptor.encrypt(bytes);
    }

    public static byte[] decrypt(byte[] bytes, String key)
    {
        StandardPBEByteEncryptor encryptor = getEncryptor(key);
        return encryptor.decrypt(bytes);
    }

    public static String createHash(String key, byte[] message) {
        MessageDigest digest;

        try {
            digest = MessageDigest.getInstance("SHA-1");
        } catch (NoSuchAlgorithmException e) {
            throw new AssertionError(e);
        }

        ByteArrayOutputStream out = new ByteArrayOutputStream();
        try {
            out.write(message);
            out.write(key.getBytes());
        }
        catch(IOException e) {
            throw new AssertionError(e);
        }

        digest.reset();
        digest.update(out.toByteArray());

        return Hex.encodeHexString(digest.digest());
    }

    public static boolean validateHash(String hash, String key, String message) {
        return validateHash(hash, key, message.getBytes(StandardCharsets.UTF_8));
    }

    public static boolean validateHash(String hash, String key, byte[] message) {
        return hash.equals(createHash(key, message));
    }

    public static boolean validateHash(Protocol.AuthenticatedMessage payload, String key) {
        return validateHash(payload.getHash(), key, payload.getPayload().toByteArray());
    }

    private AuthUtils() {}
}

@@ -30,16 +30,55 @@
 utils;%0A%0A
+import com.google.protobuf.ByteString;%0A
 import n
@@ -182,38 +182,31 @@
 ypt.
-encryption.pbe.StandardPBEByte
+util.binary.BasicBinary
 Encr
@@ -436,22 +436,21 @@
 %7B%0A%0A    p
-rivate
+ublic
  static 
@@ -455,430 +455,126 @@
 c St
-andardPBEByteEncryptor getEncryptor(String key)%0A    %7B%0A        // Many of jasypt's defaults are truly scary. This makes things less scarier but I still have no confidence in it.%0A        StandardPBEByteEncryptor encryptor = new StandardPBEByteEncryptor();%0A        encryptor.setPassword(key);%0A        encryptor.setKeyObtentionIterations(1500);%0A        encryptor.setAlgorithm(%22PBEwithSHA1AndRC4_128%22);%0A        return encryptor
+ring createHash(String key, String message) %7B%0A        return createHash(key, message.getBytes(StandardCharsets.UTF_8))
 ;%0A  
@@ -647,39 +647,35 @@
   %7B%0A        
-StandardPBEByte
+BasicBinary
 Encryptor en
@@ -680,36 +680,81 @@
 encryptor = 
-getEncryptor
+new BasicBinaryEncryptor();%0A        encryptor.setPassword
 (key);%0A     
@@ -873,57 +873,98 @@
     
-StandardPBEByteEncryptor encryptor = getEncryptor
+BasicBinaryEncryptor encryptor = new BasicBinaryEncryptor();%0A        encryptor.setPassword
 (key

Revert "I guess this is at least slightly less scary"