Skip to content

Commit 3aeff68

Browse files
authored
Merge pull request #4528 from jamezp/RESTEASY-3585-6.2
[RESTEASY-3585] Add an option to allow overriding the algorithm used …
2 parents 6d146eb + d6d1e39 commit 3aeff68

File tree

2 files changed

+24
-24
lines changed
  • resteasy-client/src/main/java/org/jboss/resteasy/client/jaxrs/internal/proxy/processors
  • resteasy-core-spi/src/main/java/org/jboss/resteasy/spi/util

2 files changed

+24
-24
lines changed

resteasy-client/src/main/java/org/jboss/resteasy/client/jaxrs/internal/proxy/processors/FormProcessor.java

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
package org.jboss.resteasy.client.jaxrs.internal.proxy.processors;
22

3-
import java.io.ByteArrayOutputStream;
4-
import java.io.DataOutputStream;
53
import java.lang.annotation.Annotation;
64
import java.lang.reflect.Field;
75
import java.lang.reflect.InvocationTargetException;
86
import java.lang.reflect.Method;
97
import java.lang.reflect.Modifier;
108
import java.lang.reflect.Type;
11-
import java.security.DigestOutputStream;
12-
import java.security.MessageDigest;
139
import java.util.ArrayList;
1410
import java.util.HashMap;
1511
import java.util.List;
@@ -20,6 +16,7 @@
2016
import org.jboss.resteasy.client.jaxrs.internal.ClientConfiguration;
2117
import org.jboss.resteasy.client.jaxrs.internal.ClientInvocation;
2218
import org.jboss.resteasy.spi.LoggableFailure;
19+
import org.jboss.resteasy.spi.util.MethodHashing;
2320

2421
/**
2522
* @author <a href="mailto:[email protected]">Bill Burke</a>
@@ -48,32 +45,25 @@ public FormProcessor(final Class clazz, final ClientConfiguration configuration,
4845
populateMap(clazz, configuration, defaultFormName);
4946
}
5047

48+
/**
49+
* @deprecated use {@link MethodHashing#methodHash(Method)}
50+
*/
51+
@Deprecated(forRemoval = true, since = "6.2.12.Final")
5152
public static long methodHash(Method method)
5253
throws Exception {
53-
Class[] parameterTypes = method.getParameterTypes();
54-
StringBuilder methodDesc = new StringBuilder(method.getName()).append("(");
55-
for (int j = 0; j < parameterTypes.length; j++) {
56-
methodDesc.append(getTypeString(parameterTypes[j]));
57-
}
58-
methodDesc.append(")").append(getTypeString(method.getReturnType()));
59-
return createHash(methodDesc.toString());
54+
return MethodHashing.methodHash(method);
6055
}
6156

57+
/**
58+
* @deprecated use {@link MethodHashing#createHash(String)}
59+
*/
60+
@Deprecated(forRemoval = true, since = "6.2.12.Final")
6261
public static long createHash(String methodDesc)
6362
throws Exception {
64-
long hash = 0;
65-
ByteArrayOutputStream bytearrayoutputstream = new ByteArrayOutputStream(512);
66-
MessageDigest messagedigest = MessageDigest.getInstance("SHA");
67-
DataOutputStream dataoutputstream = new DataOutputStream(new DigestOutputStream(bytearrayoutputstream, messagedigest));
68-
dataoutputstream.writeUTF(methodDesc);
69-
dataoutputstream.flush();
70-
byte[] abyte0 = messagedigest.digest();
71-
for (int j = 0; j < Math.min(8, abyte0.length); j++)
72-
hash += (long) (abyte0[j] & 0xff) << j * 8;
73-
return hash;
74-
63+
return MethodHashing.createHash(methodDesc);
7564
}
7665

66+
@Deprecated(forRemoval = true, since = "6.2.12.Final")
7767
static String getTypeString(Class cl) {
7868
if (cl == Byte.TYPE) {
7969
return "B";
@@ -136,7 +126,7 @@ protected void populateMap(Class clazz, ClientConfiguration configuration, Strin
136126
if (processor != null) {
137127
long hash = 0;
138128
try {
139-
hash = methodHash(method);
129+
hash = MethodHashing.methodHash(method);
140130
} catch (Exception e) {
141131
throw new RuntimeException(e);
142132
}

resteasy-core-spi/src/main/java/org/jboss/resteasy/spi/util/MethodHashing.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@
55
import java.lang.reflect.Method;
66
import java.security.DigestOutputStream;
77
import java.security.MessageDigest;
8+
import java.security.NoSuchAlgorithmException;
89

910
/**
1011
* @author <a href="mailto:[email protected]">Bill Burke</a>
1112
* @version $Revision: 1 $
1213
*/
1314
public final class MethodHashing {
15+
1416
public static long methodHash(Method method)
1517
throws Exception {
1618
Class<?>[] parameterTypes = method.getParameterTypes();
@@ -26,7 +28,7 @@ public static long createHash(String methodDesc)
2628
throws Exception {
2729
long hash = 0;
2830
ByteArrayOutputStream bytearrayoutputstream = new ByteArrayOutputStream(512);
29-
MessageDigest messagedigest = MessageDigest.getInstance("SHA");
31+
MessageDigest messagedigest = getMessageDigest();
3032
DataOutputStream dataoutputstream = new DataOutputStream(new DigestOutputStream(bytearrayoutputstream, messagedigest));
3133
dataoutputstream.writeUTF(methodDesc);
3234
dataoutputstream.flush();
@@ -62,4 +64,12 @@ static String getTypeString(Class<?> cl) {
6264
return "L" + cl.getName().replace('.', '/') + ";";
6365
}
6466
}
67+
68+
private static MessageDigest getMessageDigest() throws NoSuchAlgorithmException {
69+
try {
70+
return MessageDigest.getInstance("SHA-1");
71+
} catch (Throwable ignore) {
72+
}
73+
return MessageDigest.getInstance("SHA-256");
74+
}
6575
}

0 commit comments

Comments
 (0)