Challenge/response, refactoring

This commit is contained in:
M66B 2015-12-31 16:55:39 +01:00
parent fff758a81b
commit b1fcc24c26
5 changed files with 53 additions and 40 deletions

View File

@ -254,7 +254,7 @@ public class ActivityMain extends AppCompatActivity implements SharedPreferences
@Override
public void onReady(IAB iab) {
try {
iab.isPurchased(ActivityPro.SKU_DONATION);
iab.updatePurchases();
iab.unbind();
} catch (Throwable ex) {
Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));

View File

@ -65,10 +65,16 @@ public class ActivityPro extends AppCompatActivity {
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
// Initial state
updateState();
// Challenge
TextView tvChallenge = (TextView) findViewById(R.id.tvChallenge);
tvChallenge.setText(Build.SERIAL);
// Response
try {
final String response = Util.md5(Build.SERIAL, "NetGuard");
EditText etResponse = (EditText) findViewById(R.id.etResponse);
etResponse.addTextChangedListener(new TextWatcher() {
@Override
@ -83,16 +89,15 @@ public class ActivityPro extends AppCompatActivity {
@Override
public void afterTextChanged(Editable editable) {
try {
if (Util.sha256(Build.SERIAL, "").equals(editable.toString())) {
if (response.equals(editable.toString().toUpperCase())) {
IAB.setBought(SKU_DONATION, ActivityPro.this);
updateState();
}
}
});
} catch (Throwable ex) {
Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
}
}
});
try {
iab = new IAB(new IAB.Delegate() {
@ -100,7 +105,8 @@ public class ActivityPro extends AppCompatActivity {
public void onReady(final IAB iab) {
Log.i(TAG, "IAB ready");
try {
iab.isPurchased(SKU_DONATION);
iab.updatePurchases();
updateState();
final Button btnSelect = (Button) findViewById(R.id.btnSelect);
final Button btnNotify = (Button) findViewById(R.id.btnNotify);

View File

@ -36,6 +36,7 @@ import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
public class IAB implements ServiceConnection {
private static final String TAG = "Netguard.IAB";
@ -113,7 +114,27 @@ public class IAB implements ServiceConnection {
return found;
}
public void updatePurchases() throws RemoteException {
// Get purchases
List<String> skus = getPurchases();
SharedPreferences prefs = context.getSharedPreferences("IAB", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
for (String product : prefs.getAll().keySet())
//if (!ActivityPro.SKU_DONATION.equals(product))
editor.remove(product);
for (String sku : skus) {
Log.i(TAG, "SKU=" + sku);
editor.putBoolean(sku, true);
}
editor.apply();
}
public boolean isPurchased(String sku) throws RemoteException {
return getPurchases().contains(sku);
}
public List<String> getPurchases() throws RemoteException {
// Get purchases
Bundle bundle = service.getPurchases(IAB_VERSION, context.getPackageName(), "inapp", null);
Log.i(TAG, "getPurchases");
@ -123,20 +144,8 @@ public class IAB implements ServiceConnection {
if (response != 0)
throw new IllegalArgumentException(getResult(response));
// Check purchases
ArrayList<String> skus = bundle.getStringArrayList("INAPP_PURCHASE_ITEM_LIST");
if (skus != null) {
SharedPreferences prefs = context.getSharedPreferences("IAB", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
for (String product : prefs.getAll().keySet())
editor.remove(product);
for (String product : skus)
editor.putBoolean(product, true);
editor.apply();
}
return (skus != null && skus.contains(sku));
ArrayList<String> details = bundle.getStringArrayList("INAPP_PURCHASE_ITEM_LIST");
return (details == null ? new ArrayList<String>() : details);
}
public PendingIntent getBuyIntent(String sku) throws RemoteException {

View File

@ -517,11 +517,9 @@ public class Util {
return sb.toString();
}
public static String sha256(String text, String salt) throws NoSuchAlgorithmException, UnsupportedEncodingException {
MessageDigest digest = MessageDigest.getInstance("SHA-256");
byte[] bytes = (text + salt).getBytes("UTF-8");
digest.update(bytes, 0, bytes.length);
bytes = digest.digest();
public static String md5(String text, String salt) throws NoSuchAlgorithmException, UnsupportedEncodingException {
// MD5
byte[] bytes = MessageDigest.getInstance("MD5").digest((text + salt).getBytes("UTF-8"));
StringBuilder sb = new StringBuilder();
for (byte b : bytes)
sb.append(String.format("%02X", b));

View File

@ -94,7 +94,7 @@ Since NetGuard has no internet permission, you know your internet traffic is not
<string name="title_block">Block</string>
<string name="title_pro">Pro features</string>
<string name="title_pro_description">You can buy the following convenience features:</string>
<string name="title_pro_description">The following convenience features are available:</string>
<string name="title_pro_select">Search, filter, sort</string>
<string name="title_pro_notify">New application notification</string>
<string name="title_pro_theme">Theme</string>