Skip to content

Instantly share code, notes, and snippets.

@mrclay
Created April 8, 2012 21:16
Show Gist options
  • Save mrclay/2339879 to your computer and use it in GitHub Desktop.
Save mrclay/2339879 to your computer and use it in GitHub Desktop.
Reorder Java cipher suites
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Enable all the supported cipher suites favoring stronger ciphers
*/
public class CipherSuiteOrderer {
static String[] orderSuites(String[] supportedSuites) {
ArrayList<String> list = new ArrayList<String>(Arrays.asList(supportedSuites));
Collections.sort(list, new SuiteComparator());
return list.toArray(new String[list.size()]);
}
static class SuiteComparator implements Comparator<String> {
private Pattern pattern;
public SuiteComparator() {
pattern = Pattern.compile("_(\\d+)_");
}
@Override
public int compare(String s1, String s2) {
int i1 = score(s1);
int i2 = score(s2);
if (i1 == i2) {
return s1.compareTo(s2);
} else {
return (i1 > i2 ? -1 : 1);
}
}
public int score(String s) {
int i = 0;
// tests from most to least importance
if (s.contains("_AES_")) {
i += 1 << 10;
}
if (s.startsWith("TLS")) {
i += 1 << 9;
}
Matcher m = pattern.matcher(s);
if (m.find()) {
i += Integer.parseInt(m.group(1));
}
if (s.endsWith("SHA")) {
i += 1;
}
return i;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment