author | valeriep |
Tue, 08 May 2012 17:57:48 -0700 | |
changeset 12685 | 8a448b5b9006 |
parent 10336 | 0bb1999251f8 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
2 |
* Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package com.sun.crypto.provider; |
|
27 |
||
28 |
import java.security.KeyRep; |
|
29 |
import java.security.InvalidKeyException; |
|
30 |
import javax.crypto.SecretKey; |
|
31 |
import javax.crypto.spec.DESKeySpec; |
|
32 |
||
33 |
/** |
|
34 |
* This class represents a DES key. |
|
35 |
* |
|
36 |
* @author Jan Luehe |
|
37 |
* |
|
38 |
*/ |
|
39 |
||
40 |
final 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 |
|
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
81 |
return this.key.clone(); |
2 | 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(); |
|
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
129 |
key = key.clone(); |
2 | 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 |
} |