summaryrefslogtreecommitdiff
path: root/tools/gnu/classpath/tools/jarsigner/HashUtils.java
blob: a81a0d34da5f33b1939af92def27deab4522b412 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/* Utils.java -- Utility methods for JAR file signing/verification
   Copyright (C) 2006 Free Software Foundation, Inc.

This file is part of GNU Classpath.

GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING.  If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA. */


package gnu.classpath.tools.jarsigner;

import gnu.java.security.hash.Sha160;
import gnu.java.security.util.Base64;
import gnu.java.util.jar.JarUtils;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.logging.Logger;

/**
 * Collection of utility methods used in JAR file signing and verification.
 */
class HashUtils
{
  private static final Logger log = Logger.getLogger(HashUtils.class.getName());
  private Sha160 sha = new Sha160();

  // default 0-arguments constructor

  /**
   * @param stream the input stream to digest.
   * @return a base-64 representation of the resulting SHA-1 digest of the
   *         contents of the designated input stream.
   * @throws IOException if an I/O related exception occurs during the process.
   */
  String hashStream(InputStream stream) throws IOException
  {
    BufferedInputStream bis = new BufferedInputStream(stream, 4096);
    byte[] buffer = new byte[4096];
    int count = 0;
    int n;
    while ((n = bis.read(buffer)) != - 1)
      if (n > 0)
        {
          sha.update(buffer, 0, n);
          count += n;
        }

    byte[] hash = sha.digest();
    log.finest("Hashed " + count + " byte(s)");
    String result = Base64.encode(hash);
    return result;
  }

  /**
   * @param ba the byte array to digest.
   * @return a base-64 representation of the resulting SHA-1 digest of the
   *         contents of the designated buffer.
   */
  String hashByteArray(byte[] ba) throws IOException
  {
    sha.update(ba);
    byte[] hash = sha.digest();
    log.finest("Hashed " + ba.length + " byte(s)");
    String result = Base64.encode(hash);
    return result;
  }

  /**
   * @param name the JAR entry name
   * @param entryHash the hash of the entry file which appears in the
   *          manifest.
   * @return the base-64 encoded form of the hash of the corresponding Manifest
   *         JAR entry which will appear in the SF file under the entry with the
   *         same name.
   * @throws UnsupportedEncodingException If UTF-8 character encoding is not
   *           supported on this platform.
   */
  String hashManifestEntry(String name, String entryHash)
      throws UnsupportedEncodingException
  {
    sha.update((JarUtils.NAME + ": " + name).getBytes("UTF-8"));
    sha.update(JarUtils.CRLF);
    sha.update((Main.DIGEST + ": " + entryHash).getBytes("UTF-8"));
    sha.update(JarUtils.CRLF);
    sha.update(JarUtils.CRLF);
    byte[] sfHash = sha.digest();
    String result = Base64.encode(sfHash);
    return result;
  }
}