Skip to content

Commit 4f8f3c9

Browse files
committed
Fall back to SHA-1 for base64/binary hashCode
MD5 digest algorithm is unavailable on a FIPS JVM, so fall back to SHA-1 for generating object hashCodes in this case. Only the first 32 bits are used, so the length difference will not matter.
1 parent 795cc85 commit 4f8f3c9

File tree

3 files changed

+62
-67
lines changed

3 files changed

+62
-67
lines changed

src/main/java/org/apache/xmlbeans/impl/values/JavaBase64Holder.java

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,14 @@
2323
import org.apache.xmlbeans.impl.common.ValidationContext;
2424
import org.apache.xmlbeans.impl.schema.BuiltinSchemaTypeSystem;
2525

26-
import java.security.MessageDigest;
27-
import java.security.NoSuchAlgorithmException;
2826
import java.util.Arrays;
2927
import java.util.Base64;
3028

31-
public abstract class JavaBase64Holder extends XmlObjectBase {
29+
public abstract class JavaBase64Holder extends JavaDigestableHolder {
3230
public SchemaType schemaType() {
3331
return BuiltinSchemaTypeSystem.ST_BASE_64_BINARY;
3432
}
3533

36-
protected byte[] _value;
37-
3834
// SIMPLE VALUE ACCESSORS BELOW -------------------------------------------
3935

4036
// gets raw text value
@@ -104,32 +100,4 @@ protected boolean equal_to(XmlObject i) {
104100
byte[] ival = ((XmlBase64Binary) i).getByteArrayValue();
105101
return Arrays.equals(_value, ival);
106102
}
107-
108-
//because computing hashcode is expensive we'll cache it
109-
protected boolean _hashcached = false;
110-
protected int hashcode = 0;
111-
protected static final MessageDigest md5;
112-
113-
static {
114-
try {
115-
md5 = MessageDigest.getInstance("MD5");
116-
} catch (NoSuchAlgorithmException e) {
117-
throw new IllegalStateException("Cannot find MD5 hash Algorithm");
118-
}
119-
}
120-
121-
protected int value_hash_code() {
122-
if (_hashcached) {
123-
return hashcode;
124-
}
125-
126-
_hashcached = true;
127-
128-
if (_value == null) {
129-
return hashcode = 0;
130-
}
131-
132-
byte[] res = md5.digest(_value);
133-
return hashcode = (res[0] << 24) | (res[1] << 16) | (res[2] << 8) | res[3];
134-
}
135103
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/* Copyright 2004 The Apache Software Foundation
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
package org.apache.xmlbeans.impl.values;
17+
18+
import java.security.MessageDigest;
19+
import java.security.NoSuchAlgorithmException;
20+
import java.util.Objects;
21+
import java.util.stream.Stream;
22+
23+
public abstract class JavaDigestableHolder extends XmlObjectBase {
24+
protected byte[] _value;
25+
26+
//because computing hashcode is expensive we'll cache it
27+
protected boolean _hashcached = false;
28+
protected int hashcode = 0;
29+
protected static final MessageDigest digest;
30+
31+
static {
32+
digest = Stream.of("MD5", "SHA-1")
33+
.map(algorithm -> {
34+
try {
35+
return MessageDigest.getInstance(algorithm);
36+
}
37+
catch (NoSuchAlgorithmException ex) {
38+
return null;
39+
}
40+
})
41+
.filter(Objects::nonNull)
42+
.findFirst()
43+
.orElseThrow(() -> new IllegalStateException("Cannot find any suitable hash algorithm"));
44+
}
45+
46+
protected int value_hash_code() {
47+
if (_hashcached) {
48+
return hashcode;
49+
}
50+
51+
_hashcached = true;
52+
53+
if (_value == null) {
54+
return hashcode = 0;
55+
}
56+
57+
byte[] res = digest.digest(_value);
58+
return hashcode = (res[0] << 24) | (res[1] << 16) | (res[2] << 8) | res[3];
59+
}
60+
}

src/main/java/org/apache/xmlbeans/impl/values/JavaHexBinaryHolder.java

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,13 @@
2525
import org.apache.xmlbeans.impl.util.HexBin;
2626

2727
import java.nio.charset.StandardCharsets;
28-
import java.security.MessageDigest;
29-
import java.security.NoSuchAlgorithmException;
3028
import java.util.Arrays;
3129

32-
public abstract class JavaHexBinaryHolder extends XmlObjectBase {
30+
public abstract class JavaHexBinaryHolder extends JavaDigestableHolder {
3331
public SchemaType schemaType() {
3432
return BuiltinSchemaTypeSystem.ST_HEX_BINARY;
3533
}
3634

37-
protected byte[] _value;
38-
3935
// SIMPLE VALUE ACCESSORS BELOW -------------------------------------------
4036

4137
// gets raw text value
@@ -107,33 +103,4 @@ protected boolean equal_to(XmlObject i) {
107103
byte[] ival = ((XmlHexBinary) i).getByteArrayValue();
108104
return Arrays.equals(_value, ival);
109105
}
110-
111-
//because computing hashcode is expensive we'll cache it
112-
protected boolean _hashcached = false;
113-
protected int hashcode = 0;
114-
protected static final MessageDigest md5;
115-
116-
static {
117-
try {
118-
md5 = MessageDigest.getInstance("MD5");
119-
} catch (NoSuchAlgorithmException e) {
120-
throw new IllegalStateException("Cannot find MD5 hash Algorithm");
121-
}
122-
}
123-
124-
protected int value_hash_code() {
125-
if (_hashcached) {
126-
return hashcode;
127-
}
128-
129-
_hashcached = true;
130-
131-
if (_value == null) {
132-
return hashcode = 0;
133-
}
134-
135-
byte[] res = md5.digest(_value);
136-
return hashcode = (res[0] << 24) | (res[1] << 16) | (res[2] << 8) | res[3];
137-
}
138-
139106
}

0 commit comments

Comments
 (0)