Permalink
Browse files

RF-10940: Changed getValidJavascriptName to use the MD5 hash of the s…

…cript, rather than the clientId
  • Loading branch information...
bleathem committed Aug 25, 2011
1 parent 0b31b11 commit 5c37ae5becde2262e3138fa5637ce22839788267
@@ -25,6 +25,8 @@
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Array;
import java.nio.CharBuffer;
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
import java.text.MessageFormat;
import java.util.Arrays;
import java.util.Collection;
@@ -268,7 +270,8 @@ public static void appendEncoded(Appendable appendable, Object obj) throws IOExc
}
}
- public static String getValidJavascriptName(String s) {
+ public static String getValidJavascriptName(String script) {
+ String s = "av_" + getMD5scriptHash(script);
StringBuffer buf = null;
final int len = s.length();
@@ -330,6 +333,36 @@ public static String getValidJavascriptName(String s) {
return buf == null ? s : buf.toString();
}
+ public static String getMD5scriptHash(String script) {
+ byte[] bytesOfScript = new byte[0];
+ try {
+ bytesOfScript = script.getBytes("UTF-8");
+ } catch (UnsupportedEncodingException e) {
+ throw new RuntimeException("UTF-8 appears to be an unsupported character set on this platform", e);
+ }
+
+ MessageDigest md = null;
+ try {
+ md = MessageDigest.getInstance("MD5");
+ } catch (NoSuchAlgorithmException e) {
+ throw new RuntimeException("Unable to locate MD5 hash algorithm", e);
+ }
+ md.reset();
+ //byte[] thedigest = md.digest(bytesOfScript);
+ md.update(bytesOfScript);
+ byte[] messageDigest = md.digest();
+ StringBuffer hexString = new StringBuffer();
+ for (int i = 0; i < messageDigest.length; i++) {
+ String hex = Integer.toHexString(0xFF & messageDigest[i]);
+ if (hex.length() == 1) {
+ hexString.append('0');
+ }
+ hexString.append(hex);
+ }
+ Hex
+ return hexString.toString();
+ }
+
public static boolean shouldRenderAttribute(Object attributeVal) {
if (null == attributeVal) {
return false;
@@ -490,4 +490,12 @@ public void testTimezoneSerialization() throws Exception {
assertThat(serializedSFTimeZone, StringContains.containsString("\"ID\":\"America\\/New_York\""));
assertThat(serializedSFTimeZone, StringContains.containsString("\"rawOffset\":-18000000"));
}
+
+ @Test
+ public void testGetMD5scriptHash() throws Exception {
+ String testString = "Some string to hash";
+ String expectedMD5hash = "7624f3fd394f02f0ff8c53fac249129a";
+ String computedMD5hash = ScriptUtils.getMD5scriptHash(testString);
+ assertEquals(expectedMD5hash, computedMD5hash);
+ }
}

0 comments on commit 5c37ae5

Please sign in to comment.