package io.sidecar.security;


import static com.google.common.base.Preconditions.checkNotNull;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import javax.xml.bind.DatatypeConverter;

public class SecurityUtils {

    private static final String MD5_ALGORITHM = "MD5";

    /**
     * Performs base64-encoding of input bytes.
     *
     * @param rawData * Array of bytes to be encoded.
     * @return * The base64 encoded string representation of rawData.
     */
    public static String encodeBase64(byte[] rawData) {
        return DatatypeConverter.printBase64Binary(rawData);
    }

    /**
     * Generates a String representation of an MD5 Checksum from a given String input.
     * @param input - A non-null String.
     * @return an MD5 checksum as a Hex encoded String.
     */
    public static String md5(String input) {
        checkNotNull(input);
        try {
            //Create MessageDigest object for MD5
            MessageDigest digest = MessageDigest.getInstance(MD5_ALGORITHM);

            //Update input string in message digest
            digest.update(input.getBytes(), 0, input.length());

            //Converts message digest value in base 16 (hex)
            return new BigInteger(1, digest.digest()).toString(16);
        } catch (NoSuchAlgorithmException e) {
            throw new IllegalStateException(e);
        }
    }
}

@@ -90,16 +90,125 @@
 tNull;%0A%0A
+import javax.crypto.Cipher;%0Aimport javax.crypto.spec.SecretKeySpec;%0Aimport javax.xml.bind.DatatypeConverter;%0A
 import j
@@ -224,24 +224,24 @@
 BigInteger;%0A
-
 import java.
@@ -326,36 +326,20 @@
 java
-x.xml.bind.DatatypeConverter
+.util.Base64
 ;%0A%0Ap
@@ -837,16 +837,23 @@
  input.%0A
+     *%0A
      * @
@@ -1522,10 +1522,1162 @@
 %7D%0A    %7D%0A
-%7D
+%0A    /**%0A     * AES encrypt%0A     */%0A    @SuppressWarnings(%22unused%22)%0A    public static String encrypt(String plainText, String key)%0A            throws Exception %7B%0A        SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(), %22AES%22);%0A        Cipher cipher = Cipher.getInstance(%22AES%22);%0A        byte%5B%5D plainTextByte = plainText.getBytes();%0A        cipher.init(Cipher.ENCRYPT_MODE, secretKey);%0A        byte%5B%5D encryptedByte = cipher.doFinal(plainTextByte);%0A        Base64.Encoder encoder = Base64.getEncoder();%0A        return encoder.encodeToString(encryptedByte);%0A    %7D%0A%0A    /**%0A     * AES decrypt%0A     */%0A    @SuppressWarnings(%22unused%22)%0A    public static String decrypt(String encryptedText, String key)%0A            throws Exception %7B%0A        SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(), %22AES%22);%0A        Cipher cipher = Cipher.getInstance(%22AES%22);%0A        Base64.Decoder decoder = Base64.getDecoder();%0A        byte%5B%5D encryptedTextByte = decoder.decode(encryptedText);%0A        cipher.init(Cipher.DECRYPT_MODE, secretKey);%0A        byte%5B%5D decryptedByte = cipher.doFinal(encryptedTextByte);%0A        return new String(decryptedByte);%0A    %7D%0A%7D%0A
 %0A

migrate aes encryption methods to SecurityUtils