Ejemplo n.º 1
0
 public byte[] getBytes(String className) {
   try {
     Tracer.mark();
     String realName = className.replace(".", "/");
     realName += ".class";
     JarEntry je = jf.getJarEntry(realName);
     InputStream is = jf.getInputStream(je);
     ByteArrayOutputStream baos = new ByteArrayOutputStream();
     byte[] buff = new byte[4096];
     while (is.available() > 0) {
       int read = is.read(buff);
       baos.write(buff, 0, read);
     }
     is.close();
     return baos.toByteArray();
   } catch (Exception e) {
   } finally {
     Tracer.unmark();
   }
   return null;
 }
Ejemplo n.º 2
0
  public String verify(JarFile jar, String... algorithms) throws IOException {
    if (algorithms == null || algorithms.length == 0) algorithms = new String[] {"MD5", "SHA"};
    else if (algorithms.length == 1 && algorithms[0].equals("-")) return null;

    try {
      Manifest m = jar.getManifest();
      if (m.getEntries().isEmpty()) return "No name sections";

      for (Enumeration<JarEntry> e = jar.entries(); e.hasMoreElements(); ) {
        JarEntry je = e.nextElement();
        if (MANIFEST_ENTRY.matcher(je.getName()).matches()) continue;

        Attributes nameSection = m.getAttributes(je.getName());
        if (nameSection == null) return "No name section for " + je.getName();

        for (String algorithm : algorithms) {
          try {
            MessageDigest md = MessageDigest.getInstance(algorithm);
            String expected = nameSection.getValue(algorithm + "-Digest");
            if (expected != null) {
              byte digest[] = Base64.decodeBase64(expected);
              copy(jar.getInputStream(je), md);
              if (!Arrays.equals(digest, md.digest()))
                return "Invalid digest for "
                    + je.getName()
                    + ", "
                    + expected
                    + " != "
                    + Base64.encodeBase64(md.digest());
            } else reporter.error("could not find digest for " + algorithm + "-Digest");
          } catch (NoSuchAlgorithmException nsae) {
            return "Missing digest algorithm " + algorithm;
          }
        }
      }
    } catch (Exception e) {
      return "Failed to verify due to exception: " + e.getMessage();
    }
    return null;
  }
 private InputStream getResourceStream(final File file, final String resourceName) {
   try {
     JarFile jarFile = this.jarFiles.get(file);
     if (jarFile == null && file.isDirectory()) {
       final File resource = new File(file, resourceName);
       if (resource.exists()) {
         return new FileInputStream(resource);
       }
     } else {
       if (jarFile == null) {
         if (!file.exists()) {
           return null;
         }
         jarFile = new JarFile(file);
         this.jarFiles.put(file, jarFile);
         jarFile = this.jarFiles.get(file);
       }
       final JarEntry entry = jarFile.getJarEntry(resourceName);
       if (entry != null) {
         return jarFile.getInputStream(entry);
       }
     }
   } catch (Exception e) {
     this.log(
         "Ignoring Exception "
             + e.getClass().getName()
             + ": "
             + e.getMessage()
             + " reading resource "
             + resourceName
             + " from "
             + file,
         3);
   }
   return null;
 }