src/java.security.jgss/share/classes/javax/security/auth/kerberos/KeyImpl.java
author weijun
Mon, 22 Jan 2018 12:00:41 +0800
changeset 48651 67abfee27e69
parent 47216 71c04702a3d5
permissions -rw-r--r--
8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5 Reviewed-by: mullan
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
48651
67abfee27e69 8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents: 47216
diff changeset
     2
 * Copyright (c) 2000, 2017, 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: 5457
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: 5457
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: 5457
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5457
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5457
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 javax.security.auth.kerberos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.Arrays;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import javax.crypto.SecretKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import javax.security.auth.Destroyable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import javax.security.auth.DestroyFailedException;
34687
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 25859
diff changeset
    33
import sun.security.util.HexDumpEncoder;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import sun.security.krb5.Asn1Exception;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import sun.security.krb5.PrincipalName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import sun.security.krb5.EncryptionKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import sun.security.krb5.EncryptedData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import sun.security.krb5.KrbException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import sun.security.util.DerValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * This class encapsulates a Kerberos encryption key. It is not associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * with a principal and may represent an ephemeral session key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * @author Mayank Upadhyay
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * @serial include
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
class KeyImpl implements SecretKey, Destroyable, Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private static final long serialVersionUID = -7889313790214321193L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private transient byte[] keyBytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private transient int keyType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    private transient volatile boolean destroyed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * Constructs a KeyImpl from the given bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * @param keyBytes the raw bytes for the secret key
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * @param keyType the key type for the secret key as defined by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * Kerberos protocol specification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    public KeyImpl(byte[] keyBytes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                       int keyType) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
    68
        this.keyBytes = keyBytes.clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        this.keyType = keyType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * Constructs a KeyImpl from a password.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * @param principal the principal from which to derive the salt
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * @param password the password that should be used to compute the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * @param algorithm the name for the algorithm that this key wil be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * used for. This parameter may be null in which case "DES" will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * assumed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    public KeyImpl(KerberosPrincipal principal,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                   char[] password,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                   String algorithm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            PrincipalName princ = new PrincipalName(principal.getName());
23732
44fe768edfd2 8035986: KerberosKey algorithm names are not specified
weijun
parents: 18830
diff changeset
    88
            EncryptionKey key;
44fe768edfd2 8035986: KerberosKey algorithm names are not specified
weijun
parents: 18830
diff changeset
    89
            if ("none".equalsIgnoreCase(algorithm)) {
44fe768edfd2 8035986: KerberosKey algorithm names are not specified
weijun
parents: 18830
diff changeset
    90
                key = EncryptionKey.NULL_KEY;
44fe768edfd2 8035986: KerberosKey algorithm names are not specified
weijun
parents: 18830
diff changeset
    91
            } else {
44fe768edfd2 8035986: KerberosKey algorithm names are not specified
weijun
parents: 18830
diff changeset
    92
                key = new EncryptionKey(password, princ.getSalt(), algorithm);
44fe768edfd2 8035986: KerberosKey algorithm names are not specified
weijun
parents: 18830
diff changeset
    93
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            this.keyBytes = key.getBytes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            this.keyType = key.getEType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        } catch (KrbException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            throw new IllegalArgumentException(e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * Returns the keyType for this key as defined in the Kerberos Spec.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    public final int getKeyType() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        if (destroyed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            throw new IllegalStateException("This key is no longer valid");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        return keyType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * Methods from java.security.Key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    public final String getAlgorithm() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        return getAlgorithmName(keyType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    private String getAlgorithmName(int eType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        if (destroyed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            throw new IllegalStateException("This key is no longer valid");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        switch (eType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        case EncryptedData.ETYPE_DES_CBC_CRC:
23732
44fe768edfd2 8035986: KerberosKey algorithm names are not specified
weijun
parents: 18830
diff changeset
   124
            return "des-cbc-crc";
44fe768edfd2 8035986: KerberosKey algorithm names are not specified
weijun
parents: 18830
diff changeset
   125
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        case EncryptedData.ETYPE_DES_CBC_MD5:
23732
44fe768edfd2 8035986: KerberosKey algorithm names are not specified
weijun
parents: 18830
diff changeset
   127
            return "des-cbc-md5";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        case EncryptedData.ETYPE_DES3_CBC_HMAC_SHA1_KD:
23732
44fe768edfd2 8035986: KerberosKey algorithm names are not specified
weijun
parents: 18830
diff changeset
   130
            return "des3-cbc-sha1-kd";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        case EncryptedData.ETYPE_ARCFOUR_HMAC:
23732
44fe768edfd2 8035986: KerberosKey algorithm names are not specified
weijun
parents: 18830
diff changeset
   133
            return "rc4-hmac";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        case EncryptedData.ETYPE_AES128_CTS_HMAC_SHA1_96:
23732
44fe768edfd2 8035986: KerberosKey algorithm names are not specified
weijun
parents: 18830
diff changeset
   136
            return "aes128-cts-hmac-sha1-96";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        case EncryptedData.ETYPE_AES256_CTS_HMAC_SHA1_96:
23732
44fe768edfd2 8035986: KerberosKey algorithm names are not specified
weijun
parents: 18830
diff changeset
   139
            return "aes256-cts-hmac-sha1-96";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
48651
67abfee27e69 8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents: 47216
diff changeset
   141
        case EncryptedData.ETYPE_AES128_CTS_HMAC_SHA256_128:
67abfee27e69 8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents: 47216
diff changeset
   142
            return "aes128-cts-hmac-sha256-128";
67abfee27e69 8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents: 47216
diff changeset
   143
67abfee27e69 8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents: 47216
diff changeset
   144
        case EncryptedData.ETYPE_AES256_CTS_HMAC_SHA384_192:
67abfee27e69 8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents: 47216
diff changeset
   145
            return "aes256-cts-hmac-sha384-192";
67abfee27e69 8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents: 47216
diff changeset
   146
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        case EncryptedData.ETYPE_NULL:
23732
44fe768edfd2 8035986: KerberosKey algorithm names are not specified
weijun
parents: 18830
diff changeset
   148
            return "none";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        default:
23732
44fe768edfd2 8035986: KerberosKey algorithm names are not specified
weijun
parents: 18830
diff changeset
   151
            return eType > 0 ? "unknown" : "private";
2
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
    public final String getFormat() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        if (destroyed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            throw new IllegalStateException("This key is no longer valid");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        return "RAW";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    public final byte[] getEncoded() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        if (destroyed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            throw new IllegalStateException("This key is no longer valid");
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   164
        return keyBytes.clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    public void destroy() throws DestroyFailedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        if (!destroyed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            destroyed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            Arrays.fill(keyBytes, (byte) 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    public boolean isDestroyed() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        return destroyed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    /**
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 5506
diff changeset
   179
     * @serialData this {@code KeyImpl} is serialized by
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * writing out the ASN1 Encoded bytes of the encryption key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * The ASN1 encoding is defined in RFC4120 and as  follows:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * EncryptionKey   ::= SEQUENCE {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     *          keytype    [0] Int32 -- actually encryption type --,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     *          keyvalue   [1] OCTET STRING
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    private void writeObject(ObjectOutputStream ois)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        if (destroyed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
           throw new IOException("This key is no longer valid");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
           ois.writeObject((new EncryptionKey(keyType, keyBytes)).asn1Encode());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        } catch (Asn1Exception ae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
           throw new IOException(ae.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    private void readObject(ObjectInputStream ois)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                throws IOException, ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            EncryptionKey encKey = new EncryptionKey(new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                                     DerValue((byte[])ois.readObject()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            keyType = encKey.getEType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            keyBytes = encKey.getBytes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        } catch (Asn1Exception ae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            throw new IOException(ae.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        HexDumpEncoder hd = new HexDumpEncoder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        return "EncryptionKey: keyType=" + keyType
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                          + " keyBytes (hex dump)="
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                          + (keyBytes == null || keyBytes.length == 0 ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                             " Empty Key" :
5457
d2782f1ecc9f 6947487: use HexDumpEncoder.encodeBuffer()
weijun
parents: 715
diff changeset
   218
                             '\n' + hd.encodeBuffer(keyBytes)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                          + '\n');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        int result = 17;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        if(isDestroyed()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        result = 37 * result + Arrays.hashCode(keyBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        return 37 * result + keyType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    public boolean equals(Object other) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        if (other == this)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        if (! (other instanceof KeyImpl)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        KeyImpl otherKey = ((KeyImpl) other);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        if (isDestroyed() || otherKey.isDestroyed()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        if(keyType != otherKey.getKeyType() ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                !Arrays.equals(keyBytes, otherKey.getEncoded())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
}