# KeyStore.PasswordProtection

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)

# PasswordProtection

```
open class PasswordProtection : Destroyable, KeyStore.ProtectionParameter
```

|---|----------------------------------------------------------------------------------------------------------------------------------------|
| [kotlin.Any](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-any/index.html)                                                         ||
| ↳ | [java.security.KeyStore.PasswordProtection](https://developer.android.com/reference/kotlin/java/security/KeyStore.PasswordProtection#) |

A password-based implementation of`ProtectionParameter`.

## Summary

|                                                                                                                                                                                                                                                                                                                                                       Public constructors                                                                                                                                                                                                                                                                                                                                                        ||
|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---|
| [PasswordProtection](https://developer.android.com/reference/kotlin/java/security/KeyStore.PasswordProtection#PasswordProtection(kotlin.CharArray))`(`password:` `[CharArray](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-char-array/index.html)!`)` Creates a password parameter.                                                                                                                                                                                                                                                                                                                                                                                                                                   |
| [PasswordProtection](https://developer.android.com/reference/kotlin/java/security/KeyStore.PasswordProtection#PasswordProtection(kotlin.CharArray,%20kotlin.String,%20java.security.spec.AlgorithmParameterSpec))`(`password:` `[CharArray](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-char-array/index.html)!`, `protectionAlgorithm:` `[String](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html)!`, `protectionParameters:` `[AlgorithmParameterSpec](https://developer.android.com/reference/kotlin/java/security/spec/AlgorithmParameterSpec)!`)` Creates a password parameter and specifies the protection algorithm and associated parameters to use when encrypting a keystore entry. |

|                                                                                                                                                            Public methods                                                                                                                                                             ||
|-------------------------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| open[Unit](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html)                                        | [destroy](https://developer.android.com/reference/kotlin/java/security/KeyStore.PasswordProtection#destroy())`()` Clears the password.                                                                       |
| open[CharArray](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-char-array/index.html)!                            | [getPassword](https://developer.android.com/reference/kotlin/java/security/KeyStore.PasswordProtection#getPassword())`()` Gets the password.                                                                 |
| open[String](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html)!                                   | [getProtectionAlgorithm](https://developer.android.com/reference/kotlin/java/security/KeyStore.PasswordProtection#getProtectionAlgorithm())`()` Gets the name of the protection algorithm.                   |
| open[AlgorithmParameterSpec](https://developer.android.com/reference/kotlin/java/security/spec/AlgorithmParameterSpec)! | [getProtectionParameters](https://developer.android.com/reference/kotlin/java/security/KeyStore.PasswordProtection#getProtectionParameters())`()` Gets the parameters supplied for the protection algorithm. |
| open[Boolean](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-boolean/index.html)                                  | [isDestroyed](https://developer.android.com/reference/kotlin/java/security/KeyStore.PasswordProtection#isDestroyed())`()` Determines if password has been cleared.                                           |

## Public constructors

### PasswordProtection

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)  

```
PasswordProtection(password: CharArray!)
```

Creates a password parameter.

The specified`password`is cloned before it is stored in the new`PasswordProtection`object.

|                                                              Parameters                                                               ||
|------------|---------------------------------------------------------------------------------------------------------------------------|
| `password` | [CharArray](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-char-array/index.html)!:the password, which may be`null` |

### PasswordProtection

Added in[API level 26](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)  

```
PasswordProtection(
    password: CharArray!, 
    protectionAlgorithm: String!, 
    protectionParameters: AlgorithmParameterSpec!)
```

Creates a password parameter and specifies the protection algorithm and associated parameters to use when encrypting a keystore entry.

The specified`password`is cloned before it is stored in the new`PasswordProtection`object.

|                                                                                                                                                                                                                 Parameters                                                                                                                                                                                                                  ||
|------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `password`             | [CharArray](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-char-array/index.html)!:the password, which may be`null`                                                                                                                                                                                                                                                                                           |
| `protectionAlgorithm`  | [String](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html)!:the encryption algorithm name, for example,`PBEWithHmacSHA256AndAES_256`. See the Cipher section in the[Java Security Standard Algorithm Names Specification](https://docs.oracle.com/en/java/javase/17/docs/specs/security/standard-names.html#cipher-algorithm-names)for information about standard encryption algorithm names. |
| `protectionParameters` | [AlgorithmParameterSpec](https://developer.android.com/reference/kotlin/java/security/spec/AlgorithmParameterSpec)!:the encryption algorithm parameter specification, which may be`null`                                                                                                                                                                                                                            |

|                            Exceptions                             ||
|----------------------------------|---------------------------------|
| `java.lang.NullPointerException` | if`protectionAlgorithm`is`null` |

## Public methods

### destroy

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)  

```
open fun destroy(): Unit
```

Clears the password.

|                                          Exceptions                                           ||
|----------------------------------------------|-------------------------------------------------|
| `javax.security.auth.DestroyFailedException` | if this method was unable to clear the password |

### getPassword

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)  

```
open fun getPassword(): CharArray!
```

Gets the password.

Note that this method returns a reference to the password. If a clone of the array is created it is the caller's responsibility to zero out the password information after it is no longer needed.

|                                                           Return                                                           ||
|------------------------------------------------------------------------------------------|----------------------------------|
| [CharArray](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-char-array/index.html)! | the password, which may be`null` |

**See Also**

- <https://developer.android.com/reference/kotlin/java/security/KeyStore.PasswordProtection#destroy()>

|                                   Exceptions                                    ||
|-----------------------------------|----------------------------------------------|
| `java.lang.IllegalStateException` | if the password has been cleared (destroyed) |

### getProtectionAlgorithm

Added in[API level 26](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)  

```
open fun getProtectionAlgorithm(): String!
```

Gets the name of the protection algorithm. If none was set then the keystore provider will use its default protection algorithm. The name of the default protection algorithm for a given keystore type is set using the`'keystore.<type>.keyProtectionAlgorithm'`security property. For example, the`keystore.PKCS12.keyProtectionAlgorithm`property stores the name of the default key protection algorithm used for PKCS12 keystores. If the security property is not set, an implementation-specific algorithm will be used.

|                                                             Return                                                             ||
|-----------------------------------------------------------------------------------|---------------------------------------------|
| [String](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html)! | the algorithm name, or`null`if none was set |

### getProtectionParameters

Added in[API level 26](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)  

```
open fun getProtectionParameters(): AlgorithmParameterSpec!
```

Gets the parameters supplied for the protection algorithm.

|                                                                                        Return                                                                                         ||
|---------------------------------------------------------------------------------------------------------------------|------------------------------------------------------------------|
| [AlgorithmParameterSpec](https://developer.android.com/reference/kotlin/java/security/spec/AlgorithmParameterSpec)! | the algorithm parameter specification, or`null`, if none was set |

### isDestroyed

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)  

```
open fun isDestroyed(): Boolean
```

Determines if password has been cleared.

|                                                                   Return                                                                   ||
|------------------------------------------------------------------------------------|--------------------------------------------------------|
| [Boolean](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-boolean/index.html) | true if the password has been cleared, false otherwise |