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;
  }
Exemple #2
0
  private static Dictionary<String, Object> getDictionary(
      Map<String, Type> types, Map<String, Object> values) throws Exception {
    Hashtable<String, Object> ht = new Hashtable<String, Object>();
    for (Map.Entry<String, Object> e : values.entrySet()) {
      String key = e.getKey();
      Object value = e.getValue();

      Type type;
      if (types != null && (type = types.get(key)) != null) {

        if (type.scalar != null) {
          value = convert(value, type.scalar);
        } else if (type.vectorOf != null) {
          Collection<Object> coll = (Collection<Object>) value;
          Vector vector = new Vector();
          for (Object o : coll) {
            vector.add(convert(o, type.vectorOf));
          }
          value = vector;
        } else if (type.arrayOf != null) {
          if (value instanceof String && type.arrayOf.equals("byte")) {
            value = Base64.decodeBase64((String) value);
          } else {
            Collection<Object> coll = (Collection<Object>) value;
            Object array = Array.newInstance(getClass(type.arrayOf), coll.size());
            int n = 0;
            for (Object o : coll) {
              Array.set(array, n++, convert(o, type.arrayOf));
            }
            value = array;
          }
        } else {
          throw new IllegalArgumentException(
              "Key " + key + " has type but neither scalar, vectorOf, nor arrayOf is set");
        }
      }
      ht.put(key, value);
    }
    return ht;
  }