验证正在运行的 JAR
我有一个想要分发的 JAR。我已经签署了这个 jar 文件,并且我希望 JAR 验证哈希值(在清单中找到)是否正确。
我在 efreedom 上找到了验证 JarFile 哈希值的代码(无法发布链接,但代码位于 verify_Jar_Integrity 函数下面)。这段代码似乎确实有效。我编写了以下内容(我知道它不是独立于平台的,但我只是想让它立即工作)来查找当前 JAR 的路径并尝试验证它。它出现 FileNotFoundException: C:\Users...\dist (访问被拒绝)。我认为这个异常是因为java试图“打开”一个目录而你只能打开文件,但我不知道如何修复它。
public JarFile get_Path() {
String jarpath = this.getClass().getResource("/" + this.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);
}
}
此外,我在代码的其他地方使用了 BouncyCastle,它需要 cldc_classes.zip 和 cldc_crypto.zip。我想要一个独立的 JAR 应用程序。有什么方法可以将这两个 zip 包含在 jar 中,这样我就不需要用户下载 .lib 文件?
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("/" + this.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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论