Skip to content

Instantly share code, notes, and snippets.

@jarrodhroberson
Last active August 29, 2015 14:22
Show Gist options
  • Save jarrodhroberson/721ad513736998aaf3bb to your computer and use it in GitHub Desktop.
Save jarrodhroberson/721ad513736998aaf3bb to your computer and use it in GitHub Desktop.
Obfuscated String source code generator functions
final String bytes = new String(new byte[]{0x4a,0x61,0x72,0x72,0x6f,0x64}, Charsets.UTF_8);
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);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment