1 |
779 |
jeremybenn |
/* Utils.java -- Utility methods for JAR file signing/verification
|
2 |
|
|
Copyright (C) 2006, 2007 Free Software Foundation, Inc.
|
3 |
|
|
|
4 |
|
|
This file is part of GNU Classpath.
|
5 |
|
|
|
6 |
|
|
GNU Classpath is free software; you can redistribute it and/or modify
|
7 |
|
|
it under the terms of the GNU General Public License as published by
|
8 |
|
|
the Free Software Foundation; either version 2, or (at your option)
|
9 |
|
|
any later version.
|
10 |
|
|
|
11 |
|
|
GNU Classpath is distributed in the hope that it will be useful, but
|
12 |
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14 |
|
|
General Public License for more details.
|
15 |
|
|
|
16 |
|
|
You should have received a copy of the GNU General Public License
|
17 |
|
|
along with GNU Classpath; see the file COPYING. If not, write to the
|
18 |
|
|
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
19 |
|
|
02110-1301 USA.
|
20 |
|
|
|
21 |
|
|
Linking this library statically or dynamically with other modules is
|
22 |
|
|
making a combined work based on this library. Thus, the terms and
|
23 |
|
|
conditions of the GNU General Public License cover the whole
|
24 |
|
|
combination.
|
25 |
|
|
|
26 |
|
|
As a special exception, the copyright holders of this library give you
|
27 |
|
|
permission to link this library with independent modules to produce an
|
28 |
|
|
executable, regardless of the license terms of these independent
|
29 |
|
|
modules, and to copy and distribute the resulting executable under
|
30 |
|
|
terms of your choice, provided that you also meet, for each linked
|
31 |
|
|
independent module, the terms and conditions of the license of that
|
32 |
|
|
module. An independent module is a module which is not derived from
|
33 |
|
|
or based on this library. If you modify this library, you may extend
|
34 |
|
|
this exception to your version of the library, but you are not
|
35 |
|
|
obligated to do so. If you do not wish to do so, delete this
|
36 |
|
|
exception statement from your version. */
|
37 |
|
|
|
38 |
|
|
|
39 |
|
|
package gnu.classpath.tools.jarsigner;
|
40 |
|
|
|
41 |
|
|
import gnu.classpath.Configuration;
|
42 |
|
|
import gnu.java.security.hash.Sha160;
|
43 |
|
|
import gnu.java.util.Base64;
|
44 |
|
|
import gnu.java.util.jar.JarUtils;
|
45 |
|
|
|
46 |
|
|
import java.io.BufferedInputStream;
|
47 |
|
|
import java.io.IOException;
|
48 |
|
|
import java.io.InputStream;
|
49 |
|
|
import java.io.UnsupportedEncodingException;
|
50 |
|
|
import java.util.logging.Logger;
|
51 |
|
|
|
52 |
|
|
/**
|
53 |
|
|
* Collection of utility methods used in JAR file signing and verification.
|
54 |
|
|
*/
|
55 |
|
|
class HashUtils
|
56 |
|
|
{
|
57 |
|
|
private static final Logger log = Logger.getLogger(HashUtils.class.getName());
|
58 |
|
|
private Sha160 sha = new Sha160();
|
59 |
|
|
|
60 |
|
|
// default 0-arguments constructor
|
61 |
|
|
|
62 |
|
|
/**
|
63 |
|
|
* @param stream the input stream to digest.
|
64 |
|
|
* @return a base-64 representation of the resulting SHA-1 digest of the
|
65 |
|
|
* contents of the designated input stream.
|
66 |
|
|
* @throws IOException if an I/O related exception occurs during the process.
|
67 |
|
|
*/
|
68 |
|
|
String hashStream(InputStream stream) throws IOException
|
69 |
|
|
{
|
70 |
|
|
BufferedInputStream bis = new BufferedInputStream(stream, 4096);
|
71 |
|
|
byte[] buffer = new byte[4096];
|
72 |
|
|
int count = 0;
|
73 |
|
|
int n;
|
74 |
|
|
while ((n = bis.read(buffer)) != - 1)
|
75 |
|
|
if (n > 0)
|
76 |
|
|
{
|
77 |
|
|
sha.update(buffer, 0, n);
|
78 |
|
|
count += n;
|
79 |
|
|
}
|
80 |
|
|
byte[] hash = sha.digest();
|
81 |
|
|
if (Configuration.DEBUG)
|
82 |
|
|
log.finest("Hashed " + count + " byte(s)");
|
83 |
|
|
String result = Base64.encode(hash);
|
84 |
|
|
return result;
|
85 |
|
|
}
|
86 |
|
|
|
87 |
|
|
/**
|
88 |
|
|
* @param ba the byte array to digest.
|
89 |
|
|
* @return a base-64 representation of the resulting SHA-1 digest of the
|
90 |
|
|
* contents of the designated buffer.
|
91 |
|
|
*/
|
92 |
|
|
String hashByteArray(byte[] ba) throws IOException
|
93 |
|
|
{
|
94 |
|
|
sha.update(ba);
|
95 |
|
|
byte[] hash = sha.digest();
|
96 |
|
|
if (Configuration.DEBUG)
|
97 |
|
|
log.finest("Hashed " + ba.length + " byte(s)");
|
98 |
|
|
String result = Base64.encode(hash);
|
99 |
|
|
return result;
|
100 |
|
|
}
|
101 |
|
|
|
102 |
|
|
/**
|
103 |
|
|
* @param name the JAR entry name
|
104 |
|
|
* @param entryHash the hash of the entry file which appears in the
|
105 |
|
|
* manifest.
|
106 |
|
|
* @return the base-64 encoded form of the hash of the corresponding Manifest
|
107 |
|
|
* JAR entry which will appear in the SF file under the entry with the
|
108 |
|
|
* same name.
|
109 |
|
|
* @throws UnsupportedEncodingException If UTF-8 character encoding is not
|
110 |
|
|
* supported on this platform.
|
111 |
|
|
*/
|
112 |
|
|
String hashManifestEntry(String name, String entryHash)
|
113 |
|
|
throws UnsupportedEncodingException
|
114 |
|
|
{
|
115 |
|
|
sha.update((JarUtils.NAME + ": " + name).getBytes("UTF-8"));
|
116 |
|
|
sha.update(JarUtils.CRLF);
|
117 |
|
|
sha.update((Main.DIGEST + ": " + entryHash).getBytes("UTF-8"));
|
118 |
|
|
sha.update(JarUtils.CRLF);
|
119 |
|
|
sha.update(JarUtils.CRLF);
|
120 |
|
|
byte[] sfHash = sha.digest();
|
121 |
|
|
String result = Base64.encode(sfHash);
|
122 |
|
|
return result;
|
123 |
|
|
}
|
124 |
|
|
}
|