MasterKey.Builder


public final class MasterKey.Builder


Builder for generating a MasterKey.

Summary

Public constructors

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

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

Creates a builder for a MasterKey.

Public methods

@NonNull MasterKey

Builds a MasterKey from this builder.

@NonNull MasterKey.Builder
@RequiresApi(value = Build.VERSION_CODES.M)
setKeyGenParameterSpec(@NonNull KeyGenParameterSpec keyGenParameterSpec)

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

@NonNull MasterKey.Builder

Sets a KeyScheme to be used for the master key.

@NonNull MasterKey.Builder
setRequestStrongBoxBacked(boolean requestStrongBoxBacked)

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

@NonNull MasterKey.Builder
setUserAuthenticationRequired(boolean authenticationRequired)

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

@NonNull MasterKey.Builder
setUserAuthenticationRequired(
    boolean authenticationRequired,
    @IntRange(from = 1) int userAuthenticationValidityDurationSeconds
)

When used with setKeyScheme, 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.

Public constructors

Builder

Added in 1.1.0-alpha07
public Builder(@NonNull Context context)

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

Parameters
@NonNull Context context

The context to use with this master key.

Builder

Added in 1.1.0-alpha07
public Builder(@NonNull Context context, @NonNull String keyAlias)

Creates a builder for a MasterKey.

Parameters
@NonNull Context context

The context to use with this master key.

Public methods

build

Added in 1.1.0-alpha07
public @NonNull MasterKey build()

Builds a MasterKey from this builder.

Returns
@NonNull MasterKey

The master key.

setKeyGenParameterSpec

Added in 1.1.0-alpha07
@RequiresApi(value = Build.VERSION_CODES.M)
public @NonNull MasterKey.Builder setKeyGenParameterSpec(@NonNull KeyGenParameterSpec keyGenParameterSpec)

Sets a custom KeyGenParameterSpec to use as the basis of the master key. NOTE: Either this method OR setKeyScheme 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
@NonNull KeyGenParameterSpec keyGenParameterSpec

The key spec to use.

Returns
@NonNull MasterKey.Builder

This builder.

setKeyScheme

Added in 1.1.0-alpha07
public @NonNull MasterKey.Builder setKeyScheme(@NonNull MasterKey.KeyScheme keyScheme)

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
@NonNull MasterKey.KeyScheme keyScheme

The KeyScheme to use.

Returns
@NonNull MasterKey.Builder

This builder.

setRequestStrongBoxBacked

Added in 1.1.0-alpha07
public @NonNull MasterKey.Builder setRequestStrongBoxBacked(boolean requestStrongBoxBacked)

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

Parameters
boolean requestStrongBoxBacked

Whether to request to use strongbox

Returns
@NonNull MasterKey.Builder

This builder.

setUserAuthenticationRequired

Added in 1.1.0-alpha07
public @NonNull MasterKey.Builder setUserAuthenticationRequired(boolean authenticationRequired)

When used with setKeyScheme, 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
boolean authenticationRequired

Whether user authentication should be required to use the key.

Returns
@NonNull MasterKey.Builder

This builder.

setUserAuthenticationRequired

Added in 1.1.0-alpha07
public @NonNull MasterKey.Builder setUserAuthenticationRequired(
    boolean authenticationRequired,
    @IntRange(from = 1) int userAuthenticationValidityDurationSeconds
)

When used with setKeyScheme, 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
boolean authenticationRequired

Whether user authentication should be required to use the key.

@IntRange(from = 1) int userAuthenticationValidityDurationSeconds

Duration in seconds that the key should remain unlocked following user authentication.

Returns
@NonNull MasterKey.Builder

This builder.