Google is committed to advancing racial equity for Black communities. See how.

Builder

class Builder
kotlin.Any
   ↳ androidx.security.crypto.MasterKey.Builder

Builder for generating a MasterKey.

Summary

Public constructors
<init>(@NonNull context: Context)

Creates a builder for a MasterKey using the default alias of DEFAULT_MASTER_KEY_ALIAS.

<init>(@NonNull context: Context, @NonNull keyAlias: String)

Creates a builder for a MasterKey.

Public methods
MasterKey

Builds a MasterKey from this builder.

MasterKey.Builder
setKeyGenParameterSpec(@NonNull keyGenParameterSpec: KeyGenParameterSpec)

Sets a custom KeyGenParameterSpec to use as the basis of the master key.

MasterKey.Builder
setKeyScheme(@NonNull keyScheme: MasterKey.KeyScheme)

Sets a KeyScheme to be used for the master key.

MasterKey.Builder
setRequestStrongBoxBacked(requestStrongBoxBacked: Boolean)

Sets whether or not to request this key is strong box backed.

MasterKey.Builder
setUserAuthenticationRequired(authenticationRequired: Boolean)

When used with setKeyScheme(KeyScheme), sets that the built master key should require the user to authenticate before it's unlocked, probably using the androidx.

MasterKey.Builder
setUserAuthenticationRequired(authenticationRequired: Boolean, @IntRange(1) userAuthenticationValidityDurationSeconds: Int)

When used with setKeyScheme(KeyScheme), sets that the built master key should require the user to authenticate before it's unlocked, probably using the androidx.

Public constructors

<init>

Builder(@NonNull context: Context)

Creates a builder for a MasterKey using the default alias of DEFAULT_MASTER_KEY_ALIAS.

Parameters
context Context: The context to use with this master key.

<init>

Builder(
    @NonNull context: Context,
    @NonNull keyAlias: String)

Creates a builder for a MasterKey.

Parameters
context Context: The context to use with this master key.

Public methods

build

@NonNull fun build(): MasterKey

Builds a MasterKey from this builder.

Return
MasterKey The master key.

setKeyGenParameterSpec

@NonNull @RequiresApi(23) fun setKeyGenParameterSpec(@NonNull keyGenParameterSpec: KeyGenParameterSpec): MasterKey.Builder

Sets a custom KeyGenParameterSpec to use as the basis of the master key. NOTE: Either this method OR setKeyScheme(KeyScheme) should be used to set the parameters to use for building the master key. Calling either function after the other will throw an IllegalArgumentException.

Parameters
keyGenParameterSpec KeyGenParameterSpec: The key spec to use.
Return
MasterKey.Builder This builder.

setKeyScheme

@NonNull fun setKeyScheme(@NonNull keyScheme: MasterKey.KeyScheme): MasterKey.Builder

Sets a KeyScheme to be used for the master key. This uses a default KeyGenParameterSpec associated with the provided KeyScheme. NOTE: Either this method OR setKeyGenParameterSpec should be used to set the parameters to use for building the master key. Calling either function after the other will throw an IllegalArgumentException.

Parameters
keyScheme MasterKey.KeyScheme: The KeyScheme to use.
Return
MasterKey.Builder This builder.

setRequestStrongBoxBacked

@NonNull fun setRequestStrongBoxBacked(requestStrongBoxBacked: Boolean): MasterKey.Builder

Sets whether or not to request this key is strong box backed. This setting is only applicable on Build.VERSION_CODES#P and above, and only on devices that support Strongbox.

Parameters
requestStrongBoxBacked Boolean: Whether to request to use strongbox
Return
MasterKey.Builder This builder.

setUserAuthenticationRequired

@NonNull fun setUserAuthenticationRequired(authenticationRequired: Boolean): MasterKey.Builder

When used with setKeyScheme(KeyScheme), sets that the built master key should require the user to authenticate before it's unlocked, probably using the androidx.biometric library. This method sets the validity duration of the key to getDefaultAuthenticationValidityDurationSeconds().

Parameters
authenticationRequired Boolean: Whether user authentication should be required to use the key.
Return
MasterKey.Builder This builder.

setUserAuthenticationRequired

@NonNull fun setUserAuthenticationRequired(
    authenticationRequired: Boolean,
    @IntRange(1) userAuthenticationValidityDurationSeconds: Int
): MasterKey.Builder

When used with setKeyScheme(KeyScheme), sets that the built master key should require the user to authenticate before it's unlocked, probably using the androidx.biometric library, and that the key should remain unlocked for the provided duration.

Parameters
authenticationRequired Boolean: Whether user authentication should be required to use the key.
userAuthenticationValidityDurationSeconds Int: Duration in seconds that the key should remain unlocked following user authentication.
Return
MasterKey.Builder This builder.