Methods Summary |
---|
public void | deleteKey(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.String | generateNewKeyHexString(int numBits, java.lang.String algName, java.lang.String keyName)
return toHexString(generateNewKey(numBits, algName, keyName));
|
public static android.security.SystemKeyStore | getInstance()
return mInstance;
|
private java.io.File | getKeyFile(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.String | retrieveKeyHexString(java.lang.String keyName)
return toHexString(retrieveKey(keyName));
|
public static java.lang.String | toHexString(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();
|