Skip to content

Instantly share code, notes, and snippets.

@ok3141
Last active August 9, 2021 11:36
Show Gist options
  • Save ok3141/68f4fe360be0edac95e4 to your computer and use it in GitHub Desktop.
Save ok3141/68f4fe360be0edac95e4 to your computer and use it in GitHub Desktop.
Gradle obfuscate string
apply from: "$rootDir/utils.gradle"
android {
defaultConfig {
buildConfigField 'String', 'SECRET_KEY', toJavaCodeString(SECRET_KEY)
}
}
SECRET_KEY=ab76239ef64454356754239ef6210898
class Utils {
static def r = new Random(System.currentTimeMillis())
}
def String toJavaCodeString(String string) {
byte[] b = string.getBytes();
int c = b.length;
StringBuilder sb = new StringBuilder();
sb.append("(new Object() {");
sb.append("int t;");
sb.append("public String toString() {");
sb.append("byte[] buf = new byte[");
sb.append(c);
sb.append("];");
for (int i = 0; i < c; ++i) {
int t = Utils.r.nextInt();
int f = Utils.r.nextInt(24) + 1;
t = (t & ~(0xff << f)) | (b[i] << f);
sb.append("t = ");
sb.append(t);
sb.append(";");
sb.append("buf[");
sb.append(i);
sb.append("] = (byte) (t >>> ");
sb.append(f);
sb.append(");");
}
sb.append("return new String(buf);");
sb.append("}}.toString())");
return sb.toString();
}
ext.toJavaCodeString = this.&toJavaCodeString
@ok3141
Copy link
Author

ok3141 commented Mar 24, 2015

Original repo is here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment