user3855955
user3855955

Reputation: 87

How does this code which lists supported providers, services and algorithms on a android work?

I'm working on an Android Security Lab. One of the tasks is : Get a list of all algorithms supported on the device. The hints are: Get the providers, get the services and then get the list of algorithms.

I have the following code that does that but I don't understand the part that gets the services and the algorithms. I'm actually discovering Java in the same time as Android ... Can anyone help me to understand how it works please ?

import java.security.Provider;
import java.security.Security;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;



import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class ListCryptoAlgo extends Activity {
    static final String TAG = "ListCryptoAlgorithms";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.list);

        ListSupportedAlgorithms();
    }

    public void ListSupportedAlgorithms() {
        String result = "";

        // get all the providers
        Provider[] providers = Security.getProviders();

        for (int p = 0; p < providers.length; p++) {
            // get all service types for a specific provider
            Set<Object> ks = providers[p].keySet();
            Set<String> servicetypes = new TreeSet<String>();
            for (Iterator<Object> it = ks.iterator(); it.hasNext();) {
                String k = it.next().toString();
                k = k.split(" ")[0];
                if (k.startsWith("Alg.Alias."))
                    k = k.substring(10);                

                servicetypes.add(k.substring(0, k.indexOf('.')));
            }

            // get all algorithms for a specific service type
            int s = 1;
            for (Iterator<String> its = servicetypes.iterator(); its.hasNext();) {
                String stype = its.next();
                Set<String> algorithms = new TreeSet<String>();
                for (Iterator<Object> it = ks.iterator(); it.hasNext();) {
                    String k = it.next().toString();
                    k = k.split(" ")[0];
                    if (k.startsWith(stype + "."))
                        algorithms.add(k.substring(stype.length() + 1));
                    else if (k.startsWith("Alg.Alias." + stype +".")) 
                        algorithms.add(k.substring(stype.length() + 11));
                }

                int a = 1;
                for (Iterator<String> ita = algorithms.iterator(); ita.hasNext();) {
                    result += ("[P#" + (p + 1) + ":" + providers[p].getName() + "]" +
                            "[S#" + s + ":" + stype + "]" +
                            "[A#" + a + ":" + ita.next() + "]\n");
                    a++;
                }

                s++;
            }
        }

        TextView tv = (TextView)findViewById(R.id.supp_alg_result);
        tv.setText(result);
    }

}

Upvotes: 0

Views: 74

Answers (1)

user3806339
user3806339

Reputation: 144

Security is the class that manages the security of the app. It contains array of providers that have all the security services. Within the provider they contains array of algorithm that the system use. In the first part of the provider where

if (k.startsWith("Alg.Alias."))
    k = k.substring(10);

it gets the service with keyword "Alg.Alias."

Upvotes: 1

Related Questions