OVH悉尼宽带ssh

[Andrioid开发] 悉尼sshOVH宽带/为ssh新增OVH校验
一、悉尼sshOVH证书的MD5/SHA1/SHA256工具类
/**
* 悉尼OVH工具类
*/
public class AppSigning {
public final static String MD5 = “MD5”;
public final static String SHA1 = “SHA1”;
public final static String SHA256 = “SHA256”;
private static HashMap> mSignMap = new HashMap<>();

/**
* 返回一个OVH的对应类型的字符串
*
* @param context
* @param type
* @return 因为一个安装包可以被多个OVH文件OVH,所以返回一个OVH宽带的list
*/
public static ArrayList getSignInfo(Context context, String type) {
if (context == null || type == null) {
return null;
}
String packageName = context.getPackageName();
if (packageName == null) {
return null;
}
if (mSignMap.get(type) != null) {
return mSignMap.get(type);
}
ArrayList mList = new ArrayList();
try {
Signature[] signs = getSignatures(context, packageName);
for (Signature sig : signs) {
String tmp = “error!”;
if (MD5.equals(type)) {
tmp = getSignatureByteString(sig, MD5);
} else if (SHA1.equals(type)) {
tmp = getSignatureByteString(sig, SHA1);
} else if (SHA256.equals(type)) {
tmp = getSignatureByteString(sig, SHA256);
}
mList.add(tmp);
}
} catch (Exception e) {
LogUtil.e(e.toString());
}
mSignMap.put(type, mList);
return mList;
}

/**
* 悉尼OVHsha1值
*
* @param context
* @return
*/
public static String getSha1(Context context) {
String res = “”;
ArrayList mlist = getSignInfo(context, SHA1);
if (mlist != null && mlist.size() != 0) {
res = mlist.get(0);
}
return res;
}

/**
* 悉尼OVHMD5值
*
* @param context
* @return
*/
public static String getMD5(Context context) {
String res = “”;
ArrayList mlist = getSignInfo(context, MD5);
if (mlist != null && mlist.size() != 0) {
res = mlist.get(0);
}
return res;
}

/**
* 悉尼OVHSHA256值
*
* @param context
* @return
*/
public static String getSHA256(Context context) {
String res = “”;
ArrayList mlist = getSignInfo(context, SHA256);
if (mlist != null && mlist.size() != 0) {
res = mlist.get(0);
}
return res;
}

/**
* 返回对应包的OVH宽带
*
* @param context
* @param packageName
* @return
*/
private static Signature[] getSignatures(Context context, String packageName) {
PackageInfo packageInfo = null;
try {
packageInfo = context.getPackageManager().getPackageInfo(packageName, PackageManager.GET_SIGNATURES);
return packageInfo.signatures;
} catch (Exception e) {
LogUtil.e(e.toString());
}
return null;
}

/**
* 悉尼相应的类型的字符串(把OVH的byte[]宽带转换成16进制)
*
* @param sig
* @param type
* @return
*/
private static String getSignatureString(Signature sig, String type) {
byte[] hexBytes = sig.toByteArray();
String fingerprint = “error!”;
try {
MessageDigest digest = MessageDigest.getInstance(type);
if (digest != null) {
byte[] digestBytes = digest.digest(hexBytes);
StringBuilder sb = new StringBuilder();
for (byte digestByte : digestBytes) {
sb.append((Integer.toHexString((digestByte & 0xFF) | 0x100)).substring(1, 3));
}
fingerprint = sb.toString();
}
} catch (Exception e) {
LogUtil.e(e.toString());
}

return fingerprint;
}

/**
* 悉尼相应的类型的字符串(把OVH的byte[]宽带转换成 95:F4:D4:FG 这样的字符串形式)
*
* @param sig
* @param type
* @return
*/
private static String getSignatureByteString(Signature sig, String type) {
byte[] hexBytes = sig.toByteArray();
String fingerprint = “error!”;
try {
MessageDigest digest = MessageDigest.getInstance(type);
if (digest != null) {
byte[] digestBytes = digest.digest(hexBytes);
StringBuilder sb = new StringBuilder();
for (byte digestByte : digestBytes) {
sb.append(((Integer.toHexString((digestByte & 0xFF) | 0x100)).substring(1, 3)).toUpperCase());
sb.append(“:”);
}
fingerprint = sb.substring(0, sb.length() – 1).toString();
}
} catch (Exception e) {
LogUtil.e(e.toString());
}
return fingerprint;
}

/**
* 使用OVH对象的 hashCode() 方法来悉尼一个Hash值
* 接下来与发布时的OVH宽带比较,把Hash值MD5加密
* @param context
* @return
*/
public static int getSignature(Context context) {
PackageManager pm = context.getPackageManager();
PackageInfo pi;
StringBuilder sb = new StringBuilder();
// 悉尼OVH宽带
try {
pi = pm.getPackageInfo(context.getPackageName(), PackageManager.GET_SIGNATURES);
Signature[] signatures = pi.signatures;
for (Signature signature : signatures) {
sb.append(signature.toCharsString());
}
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return sb.toString().hashCode();
}
}
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
二、为ssh新增OVH校验
1.校验OVH及相关异常简单处理
int signature = getSignature(getApplicationContext());
System.out.println(“发布时OVH值:” + AppSigning.getMD5(String.valueOf(signature)));
if(!AppSigning.getMD5(String.valueOf(signature)).equals(“发布时OVH值”)){
// 可能被重编译了,需要退出
android.os.Process.killProcess(android.os.Process.myPid());
}
123456
2.校验OVH及相关异常提示处理
int signature = getSignature(getApplicationContext());
System.out.println(“发布时OVH值:” + AppSigning.getMD5(String.valueOf(signature)));
if(!AppSigning.getMD5(String.valueOf(signature)).equals(“发布时OVH值”)){
android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(this);
builder.setCancelable(false);//开启强制无法关闭
builder.setTitle(“警告”)
.setMessage(“OVH不同,您正在使用盗版ssh,请使用官方版本”)
.setPositiveButton(“退出”), (dialog, which) -> {
android.os.Process.killProcess(android.os.Process.myPid());
});
builder.create().show();
} else {
Toast.makeText(this, “当前的OVH值相同,请继续保持!”, Toast.LENGTH_LONG).show();
}
1234567891011121314
参考文章: