// optimal side-effects
  private synchronized boolean isSuperMan() throws IOException {

    if (superMan == null) {
      superMan = super.getManifest();
    }

    if (superMan != null) {
      superAttr = superMan.getMainAttributes();
      superEntries = superMan.getEntries();
      return true;
    } else return false;
  }
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;
  }
  public Manifest getManifest() throws IOException {

    if (!isSuperMan()) {
      return null;
    }

    Manifest man = new Manifest();
    Attributes attr = man.getMainAttributes();
    attr.putAll((Map) superAttr.clone());

    // now deep copy the manifest entries
    if (superEntries != null) {
      Map entries = man.getEntries();
      Iterator it = superEntries.keySet().iterator();
      while (it.hasNext()) {
        Object key = it.next();
        Attributes at = (Attributes) superEntries.get(key);
        entries.put(key, at.clone());
      }
    }

    return man;
  }