FileDocCategorySizeDatePackage
KeyGeneratorThread.javaAPI DocAndroid 1.5 API2359Wed May 06 22:41:02 BST 2009org.apache.harmony.crypto.tests.javax.crypto.func

KeyGeneratorThread

public class KeyGeneratorThread extends TestThread

Fields Summary
Constructors Summary
KeyGeneratorThread(String[] names)

        super(names);
    
Methods Summary
public voidtest()

        KeyGenerator kg = KeyGenerator.getInstance(algName);
        Key k = kg.generateKey();
        if(kg.getAlgorithm().toLowerCase().equals(k.getAlgorithm().toLowerCase()) != true) {
            throw new Exception ("Algorithm names not matched for KeyGenerator" +
                    " and for Key objects");
        }
        if(kg.getAlgorithm().toLowerCase().equals(algName.toLowerCase()) != true) {
            throw new Exception ("Algorithm names not matched for KeyGenerator" +
            " and for Key objects");
        }
        byte[] array1 = k.getEncoded();
        k = kg.generateKey();
        byte[] array2 = k.getEncoded();
        int matches = 0; 
        for (int i = 0; i < array1.length; i++) {
            if (array1[i] == array2[i]) {
                matches++;
            }
        }
        if (matches > array1.length / 2) {
            throw new Exception("Generated keys are simular");
        }
        SecureRandom random = new SecureRandom();
        kg.init(random);
        matches = 0; 
        k = kg.generateKey();
        array1 = k.getEncoded();
        random = new SecureRandom();
        kg.init(random);
        k = kg.generateKey();
        array2 = k.getEncoded();
        for (int i = 0; i < array1.length; i++) {
            if (array1[i] == array2[i]) {
                matches++;
            }
        }