Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/com/sun/crypto/provider/DESKey.java
38922 views
1
/*
2
* Copyright (c) 1997, 2015, Oracle and/or its affiliates. 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. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle 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 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.
24
*/
25
26
package com.sun.crypto.provider;
27
28
import java.security.MessageDigest;
29
import java.security.KeyRep;
30
import java.security.InvalidKeyException;
31
import javax.crypto.SecretKey;
32
import javax.crypto.spec.DESKeySpec;
33
34
/**
35
* This class represents a DES key.
36
*
37
* @author Jan Luehe
38
*
39
*/
40
41
final class DESKey implements SecretKey {
42
43
static final long serialVersionUID = 7724971015953279128L;
44
45
private byte[] key;
46
47
/**
48
* Uses the first 8 bytes of the given key as the DES key.
49
*
50
* @param key the buffer with the DES key bytes.
51
*
52
* @exception InvalidKeyException if less than 8 bytes are available for
53
* the key.
54
*/
55
DESKey(byte[] key) throws InvalidKeyException {
56
this(key, 0);
57
}
58
59
/**
60
* Uses the first 8 bytes in <code>key</code>, beginning at
61
* <code>offset</code>, as the DES key
62
*
63
* @param key the buffer with the DES key bytes.
64
* @param offset the offset in <code>key</code>, where the DES key bytes
65
* start.
66
*
67
* @exception InvalidKeyException if less than 8 bytes are available for
68
* the key.
69
*/
70
DESKey(byte[] key, int offset) throws InvalidKeyException {
71
if (key == null || key.length - offset < DESKeySpec.DES_KEY_LEN) {
72
throw new InvalidKeyException("Wrong key size");
73
}
74
this.key = new byte[DESKeySpec.DES_KEY_LEN];
75
System.arraycopy(key, offset, this.key, 0, DESKeySpec.DES_KEY_LEN);
76
DESKeyGenerator.setParityBit(this.key, 0);
77
}
78
79
public synchronized byte[] getEncoded() {
80
// Return a copy of the key, rather than a reference,
81
// so that the key data cannot be modified from outside
82
return this.key.clone();
83
}
84
85
public String getAlgorithm() {
86
return "DES";
87
}
88
89
public String getFormat() {
90
return "RAW";
91
}
92
93
/**
94
* Calculates a hash code value for the object.
95
* Objects that are equal will also have the same hashcode.
96
*/
97
public int hashCode() {
98
int retval = 0;
99
for (int i = 1; i < this.key.length; i++) {
100
retval += this.key[i] * i;
101
}
102
return(retval ^= "des".hashCode());
103
}
104
105
public boolean equals(Object obj) {
106
if (this == obj)
107
return true;
108
109
if (!(obj instanceof SecretKey))
110
return false;
111
112
String thatAlg = ((SecretKey)obj).getAlgorithm();
113
if (!(thatAlg.equalsIgnoreCase("DES")))
114
return false;
115
116
byte[] thatKey = ((SecretKey)obj).getEncoded();
117
boolean ret = MessageDigest.isEqual(this.key, thatKey);
118
java.util.Arrays.fill(thatKey, (byte)0x00);
119
return ret;
120
}
121
122
/**
123
* readObject is called to restore the state of this key from
124
* a stream.
125
*/
126
private void readObject(java.io.ObjectInputStream s)
127
throws java.io.IOException, ClassNotFoundException
128
{
129
s.defaultReadObject();
130
key = key.clone();
131
}
132
133
/**
134
* Replace the DES key to be serialized.
135
*
136
* @return the standard KeyRep object to be serialized
137
*
138
* @throws java.io.ObjectStreamException if a new object representing
139
* this DES key could not be created
140
*/
141
private Object writeReplace() throws java.io.ObjectStreamException {
142
return new KeyRep(KeyRep.Type.SECRET,
143
getAlgorithm(),
144
getFormat(),
145
getEncoded());
146
}
147
148
/**
149
* Ensures that the bytes of this key are
150
* set to zero when there are no more references to it.
151
*/
152
protected void finalize() throws Throwable {
153
try {
154
synchronized (this) {
155
if (this.key != null) {
156
java.util.Arrays.fill(this.key, (byte)0x00);
157
this.key = null;
158
}
159
}
160
} finally {
161
super.finalize();
162
}
163
}
164
}
165
166