Skip to content

Commit e801911

Browse files
committed
testing static context
1 parent ed493e9 commit e801911

File tree

8 files changed

+189
-74
lines changed

8 files changed

+189
-74
lines changed

README.md

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,15 @@ android.applicationVariants.all{ variant ->
3535
}
3636
```
3737

38+
Setup
39+
-----
40+
Init the library:
41+
```
42+
AndroidStringObfuscator.init(this);
43+
```
3844

39-
Encrypt Strings
40-
---------------
45+
46+
#### Encrypt Strings
4147
The script will encrypt all string tags with `hidden="true"` as attribute.
4248

4349
```xml
@@ -50,13 +56,12 @@ The script will encrypt all string tags with `hidden="true"` as attribute.
5056
Or encrypt strings programmatically by doing:
5157

5258
```java
53-
String encrypted = AndroidStringObfuscator.simulateString(context, some_string_var);
59+
String encrypted = AndroidStringObfuscator.simulateString(some_string_var);
5460
```
5561

56-
Decrypt Strings
57-
---------------
62+
#### Decrypt Strings
5863
```java
59-
String decrypted = AndroidStringObfuscator.getString(context, R.string.app_name);
64+
String decrypted = AndroidStringObfuscator.getString(R.string.app_name);
6065
```
6166

6267
Gradle Console Output Example
@@ -75,7 +80,7 @@ Gradle Console Output Example
7580
...
7681
```
7782

78-
### Possible errors
83+
#### Possible errors
7984
Missing `~/.android/debug.keystore`. Run your app to generate that file.
8085
```
8186
...
@@ -91,6 +96,15 @@ Missing `~/.android/debug.keystore`. Run your app to generate that file.
9196
...
9297
```
9398

99+
#### More Info
100+
At first run, if you haven't installed Gradle:
101+
```
102+
...
103+
:sample:obfuscator-script - Downloading https://services.gradle.org/distributions/gradle-2.14.1-all.zip
104+
:sample:obfuscator-script - Unzipping /Users/efraespada/.gradle/wrapper/dists/gradle-2.14.1-all/8bnwg5hd3w55iofp58khbp6yv/gradle-2.14.1-all.zip to /Users/efraespada/.gradle/wrapper/dists/gradle-2.14.1-all/8bnwg5hd3w55iofp58khbp6yv
105+
:sample:obfuscator-script - Set executable permissions for: /Users/efraespada/.gradle/wrapper/dists/gradle-2.14.1-all/8bnwg5hd3w55iofp58khbp6yv/gradle-2.14.1/bin/gradle
106+
...
107+
```
94108
License
95109
-------
96110
Copyright 2017 Efraín Espada

androidstringobfuscator/androidstringobfuscator.iml

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,6 @@
6767
<sourceFolder url="file://$MODULE_DIR$/src/main/jni" isTestSource="false" />
6868
<sourceFolder url="file://$MODULE_DIR$/src/main/rs" isTestSource="false" />
6969
<sourceFolder url="file://$MODULE_DIR$/src/main/shaders" isTestSource="false" />
70-
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/res" type="java-test-resource" />
71-
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/resources" type="java-test-resource" />
72-
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/assets" type="java-test-resource" />
73-
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/aidl" isTestSource="true" />
74-
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/java" isTestSource="true" />
75-
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/jni" isTestSource="true" />
76-
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/rs" isTestSource="true" />
77-
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/shaders" isTestSource="true" />
7870
<sourceFolder url="file://$MODULE_DIR$/src/test/res" type="java-test-resource" />
7971
<sourceFolder url="file://$MODULE_DIR$/src/test/resources" type="java-test-resource" />
8072
<sourceFolder url="file://$MODULE_DIR$/src/test/assets" type="java-test-resource" />
@@ -83,6 +75,14 @@
8375
<sourceFolder url="file://$MODULE_DIR$/src/test/jni" isTestSource="true" />
8476
<sourceFolder url="file://$MODULE_DIR$/src/test/rs" isTestSource="true" />
8577
<sourceFolder url="file://$MODULE_DIR$/src/test/shaders" isTestSource="true" />
78+
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/res" type="java-test-resource" />
79+
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/resources" type="java-test-resource" />
80+
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/assets" type="java-test-resource" />
81+
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/aidl" isTestSource="true" />
82+
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/java" isTestSource="true" />
83+
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/jni" isTestSource="true" />
84+
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/rs" isTestSource="true" />
85+
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/shaders" isTestSource="true" />
8686
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/annotations" />
8787
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/blame" />
8888
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/bundles" />
@@ -141,6 +141,5 @@
141141
<orderEntry type="library" exported="" name="appcompat-v7-25.1.0" level="project" />
142142
<orderEntry type="library" exported="" scope="TEST" name="jsr305-2.0.1" level="project" />
143143
<orderEntry type="library" exported="" name="support-core-utils-25.1.0" level="project" />
144-
<orderEntry type="library" exported="" name="android-android-25" level="project" />
145144
</component>
146145
</module>

androidstringobfuscator/src/main/java/com/efraespada/androidstringobfuscator/AndroidStringObfuscator.java

Lines changed: 30 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import android.content.pm.PackageInfo;
55
import android.content.pm.PackageManager;
66
import android.content.pm.Signature;
7+
import android.util.Log;
78

89
import java.io.ByteArrayInputStream;
910
import java.io.InputStream;
@@ -26,9 +27,17 @@
2627

2728
public class AndroidStringObfuscator {
2829

29-
private final String TAG = this.getClass().getSimpleName();
30+
private static final String CODIFICATION = "UTF-8";
31+
private static final String TRANSFORMATION = "AES/ECB/PKCS5Padding";
32+
private static final char[] hexArray = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
33+
private static final String TAG = AndroidStringObfuscator.class.getSimpleName();
34+
private static Context context;
3035

31-
public static String getCertificateSHA1Fingerprint(Context context) {
36+
public static void init(Context c) {
37+
context = c;
38+
}
39+
40+
private static String getCertificateSHA1Fingerprint() {
3241
PackageManager pm = context.getPackageManager();
3342
String packageName = context.getPackageName();
3443
int flags = PackageManager.GET_SIGNATURES;
@@ -77,40 +86,11 @@ private static String byte2HexFormatted(byte[] arr) {
7786
return str.toString();
7887
}
7988

80-
private static String convertToHex(byte[] data) {
81-
StringBuilder buf = new StringBuilder();
82-
for (byte b : data) {
83-
int halfbyte = (b >>> 4) & 0x0F;
84-
int two_halfs = 0;
85-
do {
86-
buf.append((0 <= halfbyte) && (halfbyte <= 9) ? (char) ('0' + halfbyte) : (char) ('a' + (halfbyte - 10)));
87-
halfbyte = b & 0x0F;
88-
} while (two_halfs++ < 1);
89-
}
90-
return buf.toString();
91-
}
92-
93-
private static String SHA1(String text){
94-
MessageDigest md = null;
95-
try {
96-
md = MessageDigest.getInstance("SHA-1");
97-
md.update(text.getBytes("iso-8859-1"), 0, text.length());
98-
} catch (NoSuchAlgorithmException e) {
99-
e.printStackTrace();
100-
return null;
101-
} catch (UnsupportedEncodingException e) {
102-
e.printStackTrace();
103-
return null;
104-
}
105-
byte[] sha1hash = md.digest();
106-
return convertToHex(sha1hash);
107-
}
108-
10989
private static SecretKey generateKey(String key) throws NoSuchAlgorithmException {
11090
MessageDigest digest = MessageDigest.getInstance("SHA-1");
11191
byte[] passphrase = null;
11292
try {
113-
passphrase = digest.digest(key.getBytes("UTF-8"));
93+
passphrase = digest.digest(key.getBytes(CODIFICATION));
11494
} catch (UnsupportedEncodingException e) {
11595
e.printStackTrace();
11696
}
@@ -120,8 +100,8 @@ private static SecretKey generateKey(String key) throws NoSuchAlgorithmException
120100
}
121101

122102
private static String encrypt(String message, String key) throws Exception {
123-
byte[] data = message.getBytes("UTF-8");
124-
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
103+
byte[] data = message.getBytes(CODIFICATION);
104+
Cipher cipher = Cipher.getInstance(TRANSFORMATION);
125105
cipher.init(Cipher.ENCRYPT_MODE, generateKey(key));
126106
byte[] encryptData = cipher.doFinal(data);
127107

@@ -131,10 +111,10 @@ private static String encrypt(String message, String key) throws Exception {
131111
private static String decrypt(String v, String key) throws Exception {
132112
byte[] tmp = hexStringToByteArray(v);
133113
SecretKeySpec spec = new SecretKeySpec(generateKey(key).getEncoded(), "AES");
134-
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
114+
Cipher cipher = Cipher.getInstance(TRANSFORMATION);
135115
cipher.init(Cipher.DECRYPT_MODE, spec);
136116

137-
String result = new String(cipher.doFinal(tmp), "UTF-8");
117+
String result = new String(cipher.doFinal(tmp), CODIFICATION);
138118
return result;
139119
}
140120

@@ -149,7 +129,6 @@ private static byte[] hexStringToByteArray(String s) {
149129
return data;
150130
}
151131

152-
final private static char[] hexArray = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
153132
private static String byteArrayToHexString(byte[] bytes) {
154133
char[] hexChars = new char[bytes.length*2];
155134
int v;
@@ -163,8 +142,13 @@ private static String byteArrayToHexString(byte[] bytes) {
163142
return new String(hexChars);
164143
}
165144

166-
public static String getString(Context context, int id) {
167-
String hash = getCertificateSHA1Fingerprint(context);
145+
public static String getString(int id) {
146+
if (context == null) {
147+
Log.e(TAG, "Library not initiated: AndroidStringObfuscator.init(Context)");
148+
return null;
149+
}
150+
151+
String hash = getCertificateSHA1Fingerprint();
168152
try {
169153
return decrypt(context.getString(id), hash);
170154
} catch (Exception e) {
@@ -173,8 +157,13 @@ public static String getString(Context context, int id) {
173157
return context.getString(id); // returns original value, maybe not encrypted
174158
}
175159

176-
public static String simulateString(Context context, String text) {
177-
String hash = getCertificateSHA1Fingerprint(context);
160+
public static String simulateString(String text) {
161+
if (context == null) {
162+
Log.e(TAG, "Library not initiated: AndroidStringObfuscator.init(Context)");
163+
return null;
164+
}
165+
166+
String hash = getCertificateSHA1Fingerprint();
178167
try {
179168
return encrypt(text, hash);
180169
} catch (Exception e) {
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<items version="2" >
3+
4+
<item
5+
is-multidex="true"
6+
jar="/Users/efraespada/Documents/Github/AndroidStringObfuscator/sample/build/intermediates/exploded-aar/com.android.support/support-compat/25.1.0/jars/classes.jar"
7+
jumboMode="true"
8+
optimize="true"
9+
revision="25.0.2"
10+
sha1="ca3c7aa31e2726a400a6e552b93cb9f63c9a1e6d">
11+
<dex dex="/Users/efraespada/Documents/Github/AndroidStringObfuscator/sample/build/intermediates/transforms/dex/debug/folders/1000/10/com.android.support-support-compat-25.1.0_c42c88e322ac3b88765b26189c8953ebd6dc21df/classes.dex" />
12+
</item>
13+
<item
14+
is-multidex="true"
15+
jar="/Users/efraespada/Documents/Github/AndroidStringObfuscator/sample/build/intermediates/exploded-aar/com.android.support/support-core-utils/25.1.0/jars/classes.jar"
16+
jumboMode="true"
17+
optimize="true"
18+
revision="25.0.2"
19+
sha1="0a0ef86b7fa568a52bf6214a0326ca1f1d618da7">
20+
<dex dex="/Users/efraespada/Documents/Github/AndroidStringObfuscator/sample/build/intermediates/transforms/dex/debug/folders/1000/10/com.android.support-support-core-utils-25.1.0_e264706844a2372a19dd84f3ee0a6d7c36421de2/classes.dex" />
21+
</item>
22+
<item
23+
is-multidex="true"
24+
jar="/Users/efraespada/Documents/Github/AndroidStringObfuscator/sample/build/intermediates/exploded-aar/com.android.support/support-fragment/25.1.0/jars/classes.jar"
25+
jumboMode="true"
26+
optimize="true"
27+
revision="25.0.2"
28+
sha1="2a53c4f3518116b8c6aee0e477233f0f7da8616f">
29+
<dex dex="/Users/efraespada/Documents/Github/AndroidStringObfuscator/sample/build/intermediates/transforms/dex/debug/folders/1000/10/com.android.support-support-fragment-25.1.0_ea5b72fbfeb71e48be05b0fc8e228c8ccee34fd5/classes.dex" />
30+
</item>
31+
<item
32+
is-multidex="true"
33+
jar="/Users/efraespada/Documents/Github/AndroidStringObfuscator/sample/build/intermediates/exploded-aar/com.android.support/animated-vector-drawable/25.1.0/jars/classes.jar"
34+
jumboMode="true"
35+
optimize="true"
36+
revision="25.0.2"
37+
sha1="6fb6ac3e8d3b9013819920d9ea11f3803c4c0033">
38+
<dex dex="/Users/efraespada/Documents/Github/AndroidStringObfuscator/sample/build/intermediates/transforms/dex/debug/folders/1000/10/com.android.support-animated-vector-drawable-25.1.0_cc0567ef4f621201554c836df6653e2aab4f1c06/classes.dex" />
39+
</item>
40+
<item
41+
is-multidex="true"
42+
jar="/Users/efraespada/Documents/Github/AndroidStringObfuscator/sample/build/intermediates/exploded-aar/com.android.support/support-media-compat/25.1.0/jars/classes.jar"
43+
jumboMode="true"
44+
optimize="true"
45+
revision="25.0.2"
46+
sha1="f02263e35e52668dd17b73bf961437e3d562b35f">
47+
<dex dex="/Users/efraespada/Documents/Github/AndroidStringObfuscator/sample/build/intermediates/transforms/dex/debug/folders/1000/10/com.android.support-support-media-compat-25.1.0_0d5c62ef13894667b9a5126845b4970f8a2de7c0/classes.dex" />
48+
</item>
49+
<item
50+
is-multidex="true"
51+
jar="/Users/efraespada/Library/Android/sdk/extras/android/m2repository/com/android/support/support-annotations/25.1.0/support-annotations-25.1.0.jar"
52+
jumboMode="true"
53+
optimize="true"
54+
revision="25.0.2"
55+
sha1="64cb6b229a8bdfd907bc8dd46839a0053e4c018d">
56+
<dex dex="/Users/efraespada/Documents/Github/AndroidStringObfuscator/sample/build/intermediates/transforms/dex/debug/folders/1000/10/support-annotations-25.1.0_0e071d643e7a09b3e3ee2910520d929863cfa76b/classes.dex" />
57+
</item>
58+
<item
59+
is-multidex="true"
60+
jar="/Users/efraespada/Documents/Github/AndroidStringObfuscator/sample/build/intermediates/exploded-aar/com.android.support/support-vector-drawable/25.1.0/jars/classes.jar"
61+
jumboMode="true"
62+
optimize="true"
63+
revision="25.0.2"
64+
sha1="e79aa3d9a16f8c9f16377c0608de70a8f31e5d80">
65+
<dex dex="/Users/efraespada/Documents/Github/AndroidStringObfuscator/sample/build/intermediates/transforms/dex/debug/folders/1000/10/com.android.support-support-vector-drawable-25.1.0_dd678d52aa6ff2f455176fcf7e911e8721c3bccd/classes.dex" />
66+
</item>
67+
<item
68+
is-multidex="true"
69+
jar="/Users/efraespada/Documents/Github/AndroidStringObfuscator/sample/build/intermediates/exploded-aar/com.android.support/support-core-ui/25.1.0/jars/classes.jar"
70+
jumboMode="true"
71+
optimize="true"
72+
revision="25.0.2"
73+
sha1="aee64634871d88a419d59eb743c1258a60f19700">
74+
<dex dex="/Users/efraespada/Documents/Github/AndroidStringObfuscator/sample/build/intermediates/transforms/dex/debug/folders/1000/10/com.android.support-support-core-ui-25.1.0_078eabeca9f94b26bc23d049453df2ec99e85d07/classes.dex" />
75+
</item>
76+
<item
77+
is-multidex="true"
78+
jar="/Users/efraespada/Documents/Github/AndroidStringObfuscator/sample/build/intermediates/exploded-aar/com.android.support/support-v4/25.1.0/jars/classes.jar"
79+
jumboMode="true"
80+
optimize="true"
81+
revision="25.0.2"
82+
sha1="d7c42c80f6509b9ea4db28ca7859ffae08ec5741">
83+
<dex dex="/Users/efraespada/Documents/Github/AndroidStringObfuscator/sample/build/intermediates/transforms/dex/debug/folders/1000/10/com.android.support-support-v4-25.1.0_14f41bb9e9137bcfd68b39681fa0606e46b17399/classes.dex" />
84+
</item>
85+
<item
86+
is-multidex="true"
87+
jar="/Users/efraespada/Documents/Github/AndroidStringObfuscator/sample/build/intermediates/exploded-aar/com.android.support/appcompat-v7/25.1.0/jars/classes.jar"
88+
jumboMode="true"
89+
optimize="true"
90+
revision="25.0.2"
91+
sha1="1630658fbc2d222b988603c27b4a4249586fbfe7">
92+
<dex dex="/Users/efraespada/Documents/Github/AndroidStringObfuscator/sample/build/intermediates/transforms/dex/debug/folders/1000/10/com.android.support-appcompat-v7-25.1.0_dd2608b8669d70a095f8cc233ddb74bcc716921c/classes.dex" />
93+
</item>
94+
<item
95+
is-multidex="true"
96+
jar="/Users/efraespada/Documents/Github/AndroidStringObfuscator/sample/build/intermediates/incremental-classes/debug/instant-run-bootstrap.jar"
97+
jumboMode="true"
98+
optimize="true"
99+
revision="25.0.2"
100+
sha1="47a44716171c36c5925cd1236143cf728cfe192f">
101+
<dex dex="/Users/efraespada/Documents/Github/AndroidStringObfuscator/sample/build/intermediates/transforms/dex/debug/folders/1000/10/instant-run-bootstrap_fc52976f1d2209d8b20e906d22e11747f5bb8009/classes.dex" />
102+
</item>
103+
<item
104+
is-multidex="true"
105+
jar="/Users/efraespada/Documents/Github/AndroidStringObfuscator/sample/build/intermediates/incremental-runtime-classes/debug/instant-run.jar"
106+
jumboMode="true"
107+
optimize="true"
108+
revision="25.0.2"
109+
sha1="a444c8272a2c8807eef8192bfe0e246973ce12b8">
110+
<dex dex="/Users/efraespada/Documents/Github/AndroidStringObfuscator/sample/build/intermediates/transforms/dex/debug/folders/1000/10/instant-run_0e777c06790c06898f56c90fdbb9417acb334706/classes.dex" />
111+
</item>
112+
113+
</items>

local.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
# Location of the SDK. This is only used by Gradle.
88
# For customization when using a Version Control System, please read the
99
# header note.
10-
#Sun Jan 22 18:03:31 CET 2017
10+
#Tue Feb 07 22:20:17 CET 2017
1111
bintrayUser=efff
1212
bintrayApiKey=1acf1ed062001f9a372e2391fdd0c16aed54a88a
13-
sdk.dir=C\:\\Users\\USUARIO\\AppData\\Local\\Android\\Sdk
13+
sdk.dir=/Users/efraespada/Library/Android/sdk

sample/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ dependencies {
3737
})
3838
compile 'com.android.support:appcompat-v7:25.1.0'
3939
testCompile 'junit:junit:4.12'
40-
// compile project(path: ':androidstringobfuscator')
41-
compile 'efraespada:androidstringobfuscator:0.2'
40+
compile project(path: ':androidstringobfuscator')
41+
// compile 'efraespada:androidstringobfuscator:0.2'
4242

4343
}
4444

0 commit comments

Comments
 (0)