|
| 1 | +package fuzion24.device.vulnerability.vulnerabilities.helper; |
| 2 | + |
| 3 | +import android.content.Context; |
| 4 | +import android.content.SharedPreferences; |
| 5 | +import android.os.Build; |
| 6 | +import android.util.Base64; |
| 7 | + |
| 8 | +import java.io.File; |
| 9 | +import java.security.MessageDigest; |
| 10 | +import java.util.HashSet; |
| 11 | +import java.util.Scanner; |
| 12 | + |
| 13 | +import fuzion24.device.vulnerability.util.SharedPreferencesUtils; |
| 14 | + |
| 15 | +/** |
| 16 | + * Created by fuzion24 on 12/4/15. |
| 17 | + */ |
| 18 | +public class DeviceUpdateChecker { |
| 19 | + public static boolean wasDeviceUpdated(Context ctx){ |
| 20 | + |
| 21 | + /* |
| 22 | + Take a hash of the build.prop and /proc/version |
| 23 | + If this changes, assume the device has been updated |
| 24 | + */ |
| 25 | + String buildPropFile = readFile("/system/build.prop"); |
| 26 | + String procVersion = readFile("/proc/version"); |
| 27 | + |
| 28 | + String fingerprint = hashString(buildPropFile + procVersion); |
| 29 | + String lastFingerprint = SharedPreferencesUtils.getBuildUpdateFingerprint(ctx); |
| 30 | + if(fingerprint.equals(lastFingerprint)){ |
| 31 | + return false; |
| 32 | + } else { |
| 33 | + SharedPreferencesUtils.setBuildUpdateFingerPrint(ctx, fingerprint); |
| 34 | + return true; |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + private static String hashString(String data){ |
| 39 | + try { |
| 40 | + MessageDigest md = MessageDigest.getInstance("SHA"); |
| 41 | + md.update(data.getBytes()); |
| 42 | + byte[] hash = md.digest(); |
| 43 | + return Base64.encodeToString(hash, Base64.DEFAULT); |
| 44 | + } catch (Exception e) { |
| 45 | + return ""; |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + private static String readFile(String filename){ |
| 50 | + try { |
| 51 | + return new Scanner(new File(filename)).useDelimiter("\\Z").next(); |
| 52 | + }catch(Exception e){ |
| 53 | + return ""; |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | +} |
0 commit comments