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/DESedeKey.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.DESedeKeySpec;
33
34
/**
35
* This class represents a DES-EDE key.
36
*
37
* @author Jan Luehe
38
*
39
*/
40
41
final class DESedeKey implements SecretKey {
42
43
static final long serialVersionUID = 2463986565756745178L;
44
45
private byte[] key;
46
47
/**
48
* Creates a DES-EDE key from a given key.
49
*
50
* @param key the given key
51
*
52
* @exception InvalidKeyException if the given key has a wrong size
53
*/
54
DESedeKey(byte[] key) throws InvalidKeyException {
55
this(key, 0);
56
}
57
58
/**
59
* Uses the first 24 bytes in <code>key</code>, beginning at
60
* <code>offset</code>, as the DES-EDE key
61
*
62
* @param key the buffer with the DES-EDE key
63
* @param offset the offset in <code>key</code>, where the DES-EDE key
64
* starts
65
*
66
* @exception InvalidKeyException if the given key has a wrong size
67
*/
68
DESedeKey(byte[] key, int offset) throws InvalidKeyException {
69
70
if (key==null || ((key.length-offset)<DESedeKeySpec.DES_EDE_KEY_LEN)) {
71
throw new InvalidKeyException("Wrong key size");
72
}
73
this.key = new byte[DESedeKeySpec.DES_EDE_KEY_LEN];
74
System.arraycopy(key, offset, this.key, 0,
75
DESedeKeySpec.DES_EDE_KEY_LEN);
76
DESKeyGenerator.setParityBit(this.key, 0);
77
DESKeyGenerator.setParityBit(this.key, 8);
78
DESKeyGenerator.setParityBit(this.key, 16);
79
}
80
81
public synchronized byte[] getEncoded() {
82
return this.key.clone();
83
}
84
85
public String getAlgorithm() {
86
return "DESede";
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 ^= "desede".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("DESede"))
114
&& !(thatAlg.equalsIgnoreCase("TripleDES")))
115
return false;
116
117
byte[] thatKey = ((SecretKey)obj).getEncoded();
118
boolean ret = MessageDigest.isEqual(this.key, thatKey);
119
java.util.Arrays.fill(thatKey, (byte)0x00);
120
return ret;
121
}
122
123
/**
124
* readObject is called to restore the state of this key from
125
* a stream.
126
*/
127
private void readObject(java.io.ObjectInputStream s)
128
throws java.io.IOException, ClassNotFoundException
129
{
130
s.defaultReadObject();
131
key = key.clone();
132
}
133
134
/**
135
* Replace the DESede key to be serialized.
136
*
137
* @return the standard KeyRep object to be serialized
138
*
139
* @throws java.io.ObjectStreamException if a new object representing
140
* this DESede key could not be created
141
*/
142
private Object writeReplace() throws java.io.ObjectStreamException {
143
return new KeyRep(KeyRep.Type.SECRET,
144
getAlgorithm(),
145
getFormat(),
146
getEncoded());
147
}
148
149
/**
150
* Ensures that the bytes of this key are
151
* set to zero when there are no more references to it.
152
*/
153
protected void finalize() throws Throwable {
154
try {
155
synchronized (this) {
156
if (this.key != null) {
157
java.util.Arrays.fill(this.key, (byte)0x00);
158
this.key = null;
159
}
160
}
161
} finally {
162
super.finalize();
163
}
164
}
165
}
166
167