jarrodhroberson
6/1/2015 - 10:40 PM

Obfuscated String source code generator functions

Obfuscated String source code generator functions

package com.vertigrated.lang;

import static com.google.common.base.Charsets.UTF_8;

import java.io.*;
import javax.annotation.Nonnull;

import com.google.common.base.Charsets;

public class Sources
{
    static final String bytes = new String(new byte[]{0x4a,0x61,0x72,0x72,0x6f,0x64}, Charsets.UTF_8);

    public static void main(final String[] args)
    {
        System.out.println(toSourceCode("bytes", "Jarrod".getBytes(UTF_8)));
        toSourceCode("bytes", new ByteArrayInputStream("Jarrod".getBytes(UTF_8)), System.out);
        System.out.println("bytes = " + bytes);
    }

    public static String toSourceCode(@Nonnull final String label, @Nonnull final byte[] data)
    {
        final ByteArrayInputStream bais = new ByteArrayInputStream(data);
        final ByteArrayOutputStream baos = new ByteArrayOutputStream((data.length * 5) +24 + data.length - 1);
        toSourceCode(label, bais, baos);
        return new String(baos.toByteArray(), UTF_8);
    }

    public static void toSourceCode(@Nonnull final String label, @Nonnull final InputStream is, @Nonnull final OutputStream os)
    {
        final byte[] zx = "0x".getBytes();
        try
        {
            os.write("final String ".getBytes(UTF_8));
            os.write(label.getBytes(UTF_8));
            os.write(" = new String(new byte[]{".getBytes());
            int i = is.read();
            while (i != -1)
            {
                    os.write(zx);
                    os.write(Integer.toHexString(i).getBytes());
                    i = is.read();
                    if (i != -1) { os.write(",".getBytes()); }
            }
            os.write("}, Charsets.UTF_8);".getBytes());
        }
        catch (IOException e)
        {
            throw new RuntimeException(e);
        }
    }
}
final String bytes = new String(new byte[]{0x4a,0x61,0x72,0x72,0x6f,0x64}, Charsets.UTF_8);