I have a JAR I want to distribute. I have signed this jar file, and I would like for the JAR to verify that the hashes (found in the Manifest) are correct.
I found code on efreedom that verifies the hashes of a JarFile (Can't post the link, but the code is below in the verify_Jar_Integrity function). This code does seem to work. I wrote the following (I know it's not platform-independent, but I'm just trying to get it to work right now) that finds the path of the current JAR and attempts to verify it. It comes up with FileNotFoundException: C:\Users...\dist (Access is denied). I think that this exception is somehow because java is trying to "open" a directory and you can only open files, but I'm not sure how to fix it.
public JarFile get_Path() {
String jarpath = this.getClass().getResource("/" + th开发者_Python百科is.getClass().getName().replace('.', '/') + ".class").toString();
int first = jarpath.indexOf("C:");
int last = jarpath.lastIndexOf(".jar") + 4;
jarpath = jarpath.substring(first, last);
System.out.println("Jar Folder: " + jarpath);
new Popup(jarpath);
JarFile jar = new JarFile(jarpath);
return jar;
}
private static boolean verify_Jar_Integrity(JarFile jar) throws IOException {
System.out.println("verify_Jar_Integrity Path: " + jar.getName());
Enumeration<JarEntry> entries = jar.entries();
while (entries.hasMoreElements()) {
JarEntry entry = entries.nextElement();
try {
InputStream is = jar.getInputStream(entry);
} catch (SecurityException se) {
return false;
}
}
return true;
}
public static void main(String[] args) {
try {
System.out.println(verify_Jar_Integrity(get_Path()));
} catch (Exception e) {
System.out.println("Error: " + e);
}
}
Additionally, I've used BouncyCastle elsewhere in the code, and it needs cldc_classes.zip and cldc_crypto.zip. I would like a single standalone JAR application. Is there any way I can include these two zips in the jar so I don't need the user to download a .lib file?
精彩评论