package org.bouncycastle.jsse.provider;

import java.util.HashSet;
import java.util.Set;
import java.util.regex.Pattern;

class JcaAlgorithmDecomposer
    implements AlgorithmDecomposer
{
    private static final Pattern PATTERN = Pattern.compile("with|and|(?<!padd)in", Pattern.CASE_INSENSITIVE);

    static final JcaAlgorithmDecomposer INSTANCE_JCA = new JcaAlgorithmDecomposer();

    public Set<String> decompose(String algorithm)
    {
        Set<String> result = new HashSet<String>();

        for (String section : algorithm.split("/"))
        {
            if (section.length() > 0)
            {
                for (String part : PATTERN.split(section))
                {
                    if (part.length() > 0)
                    {
                        result.add(part);
                    }
                }
            }
        }

        result.remove(algorithm);

        if (!result.isEmpty())
        {
            ensureBothIfEither(result, "SHA1", "SHA-1");
            ensureBothIfEither(result, "SHA224", "SHA-224");
            ensureBothIfEither(result, "SHA256", "SHA-256");
            ensureBothIfEither(result, "SHA384", "SHA-384");
            ensureBothIfEither(result, "SHA512", "SHA-512");
        }

        return result;
    }

    private static void ensureBothIfEither(Set<String> elements, String a, String b)
    {
        boolean hasA = elements.contains(a), hasB = elements.contains(b);
        if (hasA ^ hasB)
        {
            elements.add(hasA ? b : a);
        }
    }
}

@@ -34,16 +34,46 @@
 vider;%0A%0A
+import java.util.Collections;%0A
 import j
@@ -86,24 +86,24 @@
 il.HashSet;%0A
-
 import java.
@@ -457,32 +457,136 @@
 lgorithm)%0A    %7B%0A
+        if (algorithm.indexOf('/') %3C 0)%0A        %7B%0A            return Collections.emptySet();%0A        %7D%0A%0A
         Set%3CStri
@@ -989,88 +989,8 @@
  %7D%0A%0A
-        result.remove(algorithm);%0A%0A        if (!result.isEmpty())%0A        %7B%0A    
     
@@ -1038,36 +1038,32 @@
 HA-1%22);%0A        
-    
 ensureBothIfEith
@@ -1095,36 +1095,32 @@
 -224%22);%0A        
-   
- 
 ensureBothIfEith
@@ -1148,28 +1148,24 @@
 %22SHA-256%22);%0A
-    
         ensu
@@ -1213,28 +1213,24 @@
 %22);%0A        
- 
-   
 ensureBothIf
@@ -1265,26 +1265,16 @@
 A-512%22);
-%0A        %7D
 %0A%0A      

Add fast check for common case