jdk/src/java.base/share/classes/sun/security/util/ManifestEntryVerifier.java
author darcy
Thu, 23 Apr 2015 18:51:18 -0700
changeset 30033 b9c86c17164a
parent 25859 3317bb8137f4
child 31538 0981099a3e54
permissions -rw-r--r--
8078468: Update security libraries to use diamond with anonymous classes Reviewed-by: weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 16020
diff changeset
     2
 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2436
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2436
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2436
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2436
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2436
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.security.util;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.CodeSigner;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.jar.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
16020
b57c48f16179 8006182: cleanup to use java.util.Base64 in java security component, providers, and regression tests
msheppar
parents: 9365
diff changeset
    34
import java.util.Base64;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import sun.security.jca.Providers;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * This class is used to verify each entry in a jar file with its
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * manifest value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
public class ManifestEntryVerifier {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    private static final Debug debug = Debug.getInstance("jar");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
7539
d9cc884e12aa 6998860: Signed jar file verification is currently creating many extra new Sun providers.
mullan
parents: 7524
diff changeset
    47
    /**
d9cc884e12aa 6998860: Signed jar file verification is currently creating many extra new Sun providers.
mullan
parents: 7524
diff changeset
    48
     * Holder class to lazily load Sun provider. NOTE: if
d9cc884e12aa 6998860: Signed jar file verification is currently creating many extra new Sun providers.
mullan
parents: 7524
diff changeset
    49
     * Providers.getSunProvider returned a cached provider, we could avoid the
d9cc884e12aa 6998860: Signed jar file verification is currently creating many extra new Sun providers.
mullan
parents: 7524
diff changeset
    50
     * need for caching the provider with this holder class; we should try to
d9cc884e12aa 6998860: Signed jar file verification is currently creating many extra new Sun providers.
mullan
parents: 7524
diff changeset
    51
     * revisit this in JDK 8.
d9cc884e12aa 6998860: Signed jar file verification is currently creating many extra new Sun providers.
mullan
parents: 7524
diff changeset
    52
     */
d9cc884e12aa 6998860: Signed jar file verification is currently creating many extra new Sun providers.
mullan
parents: 7524
diff changeset
    53
    private static class SunProviderHolder {
d9cc884e12aa 6998860: Signed jar file verification is currently creating many extra new Sun providers.
mullan
parents: 7524
diff changeset
    54
        private static final Provider instance = Providers.getSunProvider();
d9cc884e12aa 6998860: Signed jar file verification is currently creating many extra new Sun providers.
mullan
parents: 7524
diff changeset
    55
    }
d9cc884e12aa 6998860: Signed jar file verification is currently creating many extra new Sun providers.
mullan
parents: 7524
diff changeset
    56
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    /** the created digest objects */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    HashMap<String, MessageDigest> createdDigests;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    /** the digests in use for a given entry*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    ArrayList<MessageDigest> digests;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    /** the manifest hashes for the digests in use */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    ArrayList<byte[]> manifestHashes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    private String name = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    private Manifest man;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    private boolean skip = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    private JarEntry entry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    private CodeSigner[] signers = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * Create a new ManifestEntryVerifier object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    public ManifestEntryVerifier(Manifest man)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    {
30033
b9c86c17164a 8078468: Update security libraries to use diamond with anonymous classes
darcy
parents: 25859
diff changeset
    80
        createdDigests = new HashMap<>(11);
b9c86c17164a 8078468: Update security libraries to use diamond with anonymous classes
darcy
parents: 25859
diff changeset
    81
        digests = new ArrayList<>();
b9c86c17164a 8078468: Update security libraries to use diamond with anonymous classes
darcy
parents: 25859
diff changeset
    82
        manifestHashes = new ArrayList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        this.man = man;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * Find the hashes in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * manifest for this entry, save them, and set the MessageDigest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * objects to calculate the hashes on the fly. If name is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * null it signifies that update/verify should ignore this entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    public void setEntry(String name, JarEntry entry)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        digests.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        manifestHashes.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        this.name = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        this.entry = entry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        skip = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        signers = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        if (man == null || name == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        /* get the headers from the manifest for this entry */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        /* if there aren't any, we can't verify any digests for this entry */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        Attributes attr = man.getAttributes(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        if (attr == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            // ugh. we should be able to remove this at some point.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            // there are broken jars floating around with ./name and /name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            // in the manifest, and "name" in the zip/jar file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            attr = man.getAttributes("./"+name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            if (attr == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                attr = man.getAttributes("/"+name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                if (attr == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        for (Map.Entry<Object,Object> se : attr.entrySet()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            String key = se.getKey().toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            if (key.toUpperCase(Locale.ENGLISH).endsWith("-DIGEST")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                // 7 is length of "-Digest"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                String algorithm = key.substring(0, key.length()-7);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                MessageDigest digest = createdDigests.get(algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                if (digest == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                        digest = MessageDigest.getInstance
7539
d9cc884e12aa 6998860: Signed jar file verification is currently creating many extra new Sun providers.
mullan
parents: 7524
diff changeset
   136
                                        (algorithm, SunProviderHolder.instance);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                        createdDigests.put(algorithm, digest);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                    } catch (NoSuchAlgorithmException nsae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                        // ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                if (digest != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                    skip = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                    digest.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                    digests.add(digest);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                    manifestHashes.add(
16020
b57c48f16179 8006182: cleanup to use java.util.Base64 in java security component, providers, and regression tests
msheppar
parents: 9365
diff changeset
   148
                                Base64.getMimeDecoder().decode((String)se.getValue()));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * update the digests for the digests we are interested in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    public void update(byte buffer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        if (skip) return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        for (int i=0; i < digests.size(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            digests.get(i).update(buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * update the digests for the digests we are interested in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    public void update(byte buffer[], int off, int len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        if (skip) return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        for (int i=0; i < digests.size(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            digests.get(i).update(buffer, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * get the JarEntry for this object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    public JarEntry getEntry()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        return entry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * go through all the digests, calculating the final digest
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * and comparing it to the one in the manifest. If this is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * the first time we have verified this object, remove its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * code signers from sigFileSigners and place in verifiedSigners.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     */
9365
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   192
    public CodeSigner[] verify(Hashtable<String, CodeSigner[]> verifiedSigners,
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   193
                Hashtable<String, CodeSigner[]> sigFileSigners)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        throws JarException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    {
9001
4eb5b77d7425 7023056: NPE from sun.security.util.ManifestEntryVerifier.verify during Maven build
weijun
parents: 7539
diff changeset
   196
        if (skip) {
7524
ec12e1e6fa20 7004035: signed jar with only META-INF/* inside is not verifiable
weijun
parents: 5506
diff changeset
   197
            return null;
ec12e1e6fa20 7004035: signed jar with only META-INF/* inside is not verifiable
weijun
parents: 5506
diff changeset
   198
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        if (signers != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            return signers;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        for (int i=0; i < digests.size(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            MessageDigest digest  = digests.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            byte [] manHash = manifestHashes.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            byte [] theHash = digest.digest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                debug.println("Manifest Entry: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                                   name + " digest=" + digest.getAlgorithm());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                debug.println("  manifest " + toHex(manHash));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                debug.println("  computed " + toHex(theHash));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                debug.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            if (!MessageDigest.isEqual(theHash, manHash))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                throw new SecurityException(digest.getAlgorithm()+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                                            " digest error for "+name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        // take it out of sigFileSigners and put it in verifiedSigners...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        signers = sigFileSigners.remove(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        if (signers != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            verifiedSigners.put(name, signers);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        return signers;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    // for the toHex function
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    private static final char[] hexc =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * convert a byte array to a hex string for debugging purposes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * @param data the binary data to be converted to a hex string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * @return an ASCII hex string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    static String toHex(byte[] data) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
24969
afa6934dd8e8 8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents: 23010
diff changeset
   241
        StringBuilder sb = new StringBuilder(data.length*2);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        for (int i=0; i<data.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            sb.append(hexc[(data[i] >>4) & 0x0f]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            sb.append(hexc[data[i] & 0x0f]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
}