FileDocCategorySizeDatePackage
SystemKeyStore.javaAPI DocAndroid 5.1 API4158Thu Mar 12 22:22:30 GMT 2015android.security

SystemKeyStore

public class SystemKeyStore extends Object
hide

Fields Summary
private static final String
SYSTEM_KEYSTORE_DIRECTORY
private static final String
KEY_FILE_EXTENSION
private static SystemKeyStore
mInstance
Constructors Summary
private SystemKeyStore()


       
Methods Summary
public voiddeleteKey(java.lang.String keyName)


        // Get the file first.
        File keyFile = getKeyFile(keyName);
        if (!keyFile.exists()) {
            throw new IllegalArgumentException();
        }

        keyFile.delete();
    
public byte[]generateNewKey(int numBits, java.lang.String algName, java.lang.String keyName)


        // Check if key with similar name exists. If so, return null.
        File keyFile = getKeyFile(keyName);
        if (keyFile.exists()) {
            throw new IllegalArgumentException();
        }

        KeyGenerator skg = KeyGenerator.getInstance(algName);
        SecureRandom srng = SecureRandom.getInstance("SHA1PRNG");
        skg.init(numBits, srng);

        SecretKey sk = skg.generateKey();
        byte[] retKey = sk.getEncoded();

        try {
            // Store the key
            if (!keyFile.createNewFile()) {
                throw new IllegalArgumentException();
            }

            FileOutputStream fos = new FileOutputStream(keyFile);
            fos.write(retKey);
            fos.flush();
            FileUtils.sync(fos);
            fos.close();
            FileUtils.setPermissions(keyFile.getName(), (FileUtils.S_IRUSR | FileUtils.S_IWUSR),
                -1, -1);
        } catch (IOException ioe) {
            return null;
        }
        return retKey;
    
public java.lang.StringgenerateNewKeyHexString(int numBits, java.lang.String algName, java.lang.String keyName)

        return toHexString(generateNewKey(numBits, algName, keyName));
    
public static android.security.SystemKeyStoregetInstance()

        return mInstance;
    
private java.io.FilegetKeyFile(java.lang.String keyName)

        File sysKeystoreDir = new File(Environment.getDataDirectory(),
                SYSTEM_KEYSTORE_DIRECTORY);
        File keyFile = new File(sysKeystoreDir, keyName + KEY_FILE_EXTENSION);
        return keyFile;
    
public byte[]retrieveKey(java.lang.String keyName)

        File keyFile = getKeyFile(keyName);
        if (!keyFile.exists()) {
            return null;
        }
        return IoUtils.readFileAsByteArray(keyFile.toString());
    
public java.lang.StringretrieveKeyHexString(java.lang.String keyName)

        return toHexString(retrieveKey(keyName));
    
public static java.lang.StringtoHexString(byte[] keyData)

        if (keyData == null) {
            return null;
        }
        int keyLen = keyData.length;
        int expectedStringLen = keyData.length * 2;
        StringBuilder sb = new StringBuilder(expectedStringLen);
        for (int i = 0; i < keyData.length; i++) {
            String hexStr = Integer.toString(keyData[i] & 0x00FF, 16);
            if (hexStr.length() == 1) {
                hexStr = "0" + hexStr;
            }
            sb.append(hexStr);
        }
        return sb.toString();