我要做一点java自检程序(self-checksum)。这是我的代码示例
public class tamper {
public static int checksum_self () throws Exception {
File file = new File ("tamper.class");
FileInputStream fr = new FileInputStream (file);
int result; // Compute the checksum
return result;
}
public static boolean check1_for_tampering () throws Exception {
return checksum_self () != 0; // TO BE UPDATED!
}
public static int check2_for_tampering () throws Exception {
return checksum_self ();
}
public static void main (String args[]) throws Exception {
if (check1_for_tampering ()) {
System.exit (-1);
}
float celsius = Integer.parseInt (args[0]);
float fahrenheit = 9 * celsius / 5 + 32;
System.out.println (celsius + "C = " + fahrenheit + "F");
}
}
我试图做这样的事情
DigestInputStream sha = new DigestInputStream(fr, MessageDigest.getInstance("SHA"));
byte[] digest = sha.getMessageDigest();
for(..)
{
result = result + digest[i]
}
但是我真的不知道如何检查?
编辑:
感谢您的回答
@Adam Paynter 和 SpoonBenders
当然,这不是供我个人使用的。我不会用它来保护任何软件......
这是我必须为我的java课程做的“练习”......