blob: 58d38e236a321141dd59c9844149b8293e8afdd8 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 1997-2007 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25
26package com.sun.crypto.provider;
27
28import java.security.KeyRep;
29import java.security.InvalidKeyException;
30import javax.crypto.SecretKey;
31import javax.crypto.spec.DESKeySpec;
32
33/**
34 * This class represents a DES key.
35 *
36 * @author Jan Luehe
37 *
38 */
39
40final class DESKey implements SecretKey {
41
42 static final long serialVersionUID = 7724971015953279128L;
43
44 private byte[] key;
45
46 /**
47 * Uses the first 8 bytes of the given key as the DES key.
48 *
49 * @param key the buffer with the DES key bytes.
50 *
51 * @exception InvalidKeyException if less than 8 bytes are available for
52 * the key.
53 */
54 DESKey(byte[] key) throws InvalidKeyException {
55 this(key, 0);
56 }
57
58 /**
59 * Uses the first 8 bytes in <code>key</code>, beginning at
60 * <code>offset</code>, as the DES key
61 *
62 * @param key the buffer with the DES key bytes.
63 * @param offset the offset in <code>key</code>, where the DES key bytes
64 * start.
65 *
66 * @exception InvalidKeyException if less than 8 bytes are available for
67 * the key.
68 */
69 DESKey(byte[] key, int offset) throws InvalidKeyException {
70 if (key == null || key.length - offset < DESKeySpec.DES_KEY_LEN) {
71 throw new InvalidKeyException("Wrong key size");
72 }
73 this.key = new byte[DESKeySpec.DES_KEY_LEN];
74 System.arraycopy(key, offset, this.key, 0, DESKeySpec.DES_KEY_LEN);
75 DESKeyGenerator.setParityBit(this.key, 0);
76 }
77
78 public byte[] getEncoded() {
79 // Return a copy of the key, rather than a reference,
80 // so that the key data cannot be modified from outside
81 return (byte[])this.key.clone();
82 }
83
84 public String getAlgorithm() {
85 return "DES";
86 }
87
88 public String getFormat() {
89 return "RAW";
90 }
91
92 /**
93 * Calculates a hash code value for the object.
94 * Objects that are equal will also have the same hashcode.
95 */
96 public int hashCode() {
97 int retval = 0;
98 for (int i = 1; i < this.key.length; i++) {
99 retval += this.key[i] * i;
100 }
101 return(retval ^= "des".hashCode());
102 }
103
104 public boolean equals(Object obj) {
105 if (this == obj)
106 return true;
107
108 if (!(obj instanceof SecretKey))
109 return false;
110
111 String thatAlg = ((SecretKey)obj).getAlgorithm();
112 if (!(thatAlg.equalsIgnoreCase("DES")))
113 return false;
114
115 byte[] thatKey = ((SecretKey)obj).getEncoded();
116 boolean ret = java.util.Arrays.equals(this.key, thatKey);
117 java.util.Arrays.fill(thatKey, (byte)0x00);
118 return ret;
119 }
120
121 /**
122 * readObject is called to restore the state of this key from
123 * a stream.
124 */
125 private void readObject(java.io.ObjectInputStream s)
126 throws java.io.IOException, ClassNotFoundException
127 {
128 s.defaultReadObject();
129 key = (byte[])key.clone();
130 }
131
132 /**
133 * Replace the DES key to be serialized.
134 *
135 * @return the standard KeyRep object to be serialized
136 *
137 * @throws java.io.ObjectStreamException if a new object representing
138 * this DES key could not be created
139 */
140 private Object writeReplace() throws java.io.ObjectStreamException {
141 return new KeyRep(KeyRep.Type.SECRET,
142 getAlgorithm(),
143 getFormat(),
144 getEncoded());
145 }
146
147 /**
148 * Ensures that the bytes of this key are
149 * set to zero when there are no more references to it.
150 */
151 protected void finalize() throws Throwable {
152 try {
153 if (this.key != null) {
154 java.util.Arrays.fill(this.key, (byte)0x00);
155 this.key = null;
156 }
157 } finally {
158 super.finalize();
159 }
160 }
161}