# MasterKey.Builder

Artifact: [androidx.security:security-crypto](https://developer.android.com/jetpack/androidx/releases/security) [View Source](https://cs.android.com/search?q=file:androidx/security/crypto/MasterKey.java+class:androidx.security.crypto.MasterKey.Builder) Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/security#1.1.0) Deprecated in [1.1.0](https://developer.android.com/jetpack/androidx/releases/security#1.1.0)

*** ** * ** ***


```
public final class MasterKey.Builder
```

<br />

*** ** * ** ***

> [!CAUTION]
> **This class is deprecated.**   
>
> Use `https://docs.oracle.com/javase/8/docs/api/javax/crypto/KeyGenerator.html` with AndroidKeyStore instance instead.

Builder for generating a `https://developer.android.com/reference/androidx/security/crypto/MasterKey`.

## Summary

| ### Public constructors |
|---|
| `[Builder](https://developer.android.com/reference/androidx/security/crypto/MasterKey.Builder#Builder(android.content.Context))(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/content/Context.html context)` **This method is deprecated.** Deprecated in Java |
| `[Builder](https://developer.android.com/reference/androidx/security/crypto/MasterKey.Builder#Builder(android.content.Context,java.lang.String))(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/content/Context.html context, @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/lang/String.html keyAlias)` **This method is deprecated.** Deprecated in Java |

| ### Public methods |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/security/crypto/MasterKey` | `[build](https://developer.android.com/reference/androidx/security/crypto/MasterKey.Builder#build())()` **This method is deprecated.** Deprecated in Java |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/security/crypto/MasterKey.Builder` | `@https://developer.android.com/reference/androidx/annotation/RequiresApi(value = https://developer.android.com/reference/android/os/Build.VERSION_CODES.html#M) [setKeyGenParameterSpec](https://developer.android.com/reference/androidx/security/crypto/MasterKey.Builder#setKeyGenParameterSpec(android.security.keystore.KeyGenParameterSpec))(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/security/keystore/KeyGenParameterSpec.html keyGenParameterSpec)` **This method is deprecated.** Deprecated in Java |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/security/crypto/MasterKey.Builder` | `[setKeyScheme](https://developer.android.com/reference/androidx/security/crypto/MasterKey.Builder#setKeyScheme(androidx.security.crypto.MasterKey.KeyScheme))(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/security/crypto/MasterKey.KeyScheme keyScheme)` **This method is deprecated.** Deprecated in Java |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/security/crypto/MasterKey.Builder` | `[setRequestStrongBoxBacked](https://developer.android.com/reference/androidx/security/crypto/MasterKey.Builder#setRequestStrongBoxBacked(boolean))(boolean requestStrongBoxBacked)` **This method is deprecated.** Deprecated in Java |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/security/crypto/MasterKey.Builder` | `[setUserAuthenticationRequired](https://developer.android.com/reference/androidx/security/crypto/MasterKey.Builder#setUserAuthenticationRequired(boolean))(boolean authenticationRequired)` **This method is deprecated.** Deprecated in Java |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/security/crypto/MasterKey.Builder` | `[setUserAuthenticationRequired](https://developer.android.com/reference/androidx/security/crypto/MasterKey.Builder#setUserAuthenticationRequired(boolean,int))( boolean authenticationRequired, @https://developer.android.com/reference/androidx/annotation/IntRange(from = 1) int userAuthenticationValidityDurationSeconds )` **This method is deprecated.** Deprecated in Java |

## Public constructors

### Builder

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/security#1.1.0) Deprecated in [1.1.0](https://developer.android.com/jetpack/androidx/releases/security#1.1.0)

```
public [Builder](https://developer.android.com/reference/androidx/security/crypto/MasterKey.Builder#Builder(android.content.Context))(@NonNull Context context)
```

> [!CAUTION]
> **This method is deprecated.**   
> Deprecated in Java

Creates a builder for a `https://developer.android.com/reference/androidx/security/crypto/MasterKey` using the default alias of `https://developer.android.com/reference/androidx/security/crypto/MasterKey#DEFAULT_MASTER_KEY_ALIAS()`.

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/content/Context.html context` | The context to use with this master key. |

### Builder

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/security#1.1.0) Deprecated in [1.1.0](https://developer.android.com/jetpack/androidx/releases/security#1.1.0)

```
public [Builder](https://developer.android.com/reference/androidx/security/crypto/MasterKey.Builder#Builder(android.content.Context,java.lang.String))(@NonNull Context context, @NonNull String keyAlias)
```

> [!CAUTION]
> **This method is deprecated.**   
> Deprecated in Java

Creates a builder for a `https://developer.android.com/reference/androidx/security/crypto/MasterKey`.

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/content/Context.html context` | The context to use with this master key. |

## Public methods

### build

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/security#1.1.0) Deprecated in [1.1.0](https://developer.android.com/jetpack/androidx/releases/security#1.1.0)

```
public @NonNull MasterKey [build](https://developer.android.com/reference/androidx/security/crypto/MasterKey.Builder#build())()
```

> [!CAUTION]
> **This method is deprecated.**   
> Deprecated in Java

Builds a `https://developer.android.com/reference/androidx/security/crypto/MasterKey` from this builder.

| Returns |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/security/crypto/MasterKey` | The master key. |

| Throws |
|---|---|
| `https://developer.android.com/reference/java/security/GeneralSecurityException.html` |   |
| `https://developer.android.com/reference/java/io/IOException.html` |   |

### setKeyGenParameterSpec

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/security#1.1.0) Deprecated in [1.1.0](https://developer.android.com/jetpack/androidx/releases/security#1.1.0)

```
@RequiresApi(value = Build.VERSION_CODES.M)
public @NonNull MasterKey.Builder [setKeyGenParameterSpec](https://developer.android.com/reference/androidx/security/crypto/MasterKey.Builder#setKeyGenParameterSpec(android.security.keystore.KeyGenParameterSpec))(@NonNull KeyGenParameterSpec keyGenParameterSpec)
```

> [!CAUTION]
> **This method is deprecated.**   
> Deprecated in Java

Sets a custom `https://developer.android.com/reference/android/security/keystore/KeyGenParameterSpec.html` to use as the basis of the master key. NOTE: Either this method OR `https://developer.android.com/reference/androidx/security/crypto/MasterKey.Builder#setKeyScheme(androidx.security.crypto.MasterKey.KeyScheme)` should be used to set the parameters to use for building the master key. Calling either function after the other will throw an `https://developer.android.com/reference/java/lang/IllegalArgumentException.html`.

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/security/keystore/KeyGenParameterSpec.html keyGenParameterSpec` | The key spec to use. |

| Returns |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/security/crypto/MasterKey.Builder` | This builder. |

### setKeyScheme

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/security#1.1.0) Deprecated in [1.1.0](https://developer.android.com/jetpack/androidx/releases/security#1.1.0)

```
public @NonNull MasterKey.Builder [setKeyScheme](https://developer.android.com/reference/androidx/security/crypto/MasterKey.Builder#setKeyScheme(androidx.security.crypto.MasterKey.KeyScheme))(@NonNull MasterKey.KeyScheme keyScheme)
```

> [!CAUTION]
> **This method is deprecated.**   
> Deprecated in Java

Sets a `https://developer.android.com/reference/androidx/security/crypto/MasterKey.KeyScheme` to be used for the master key.

This uses a default `https://developer.android.com/reference/android/security/keystore/KeyGenParameterSpec.html` associated with the provided `KeyScheme`.

NOTE: Either this method OR `https://developer.android.com/reference/androidx/security/crypto/MasterKey.Builder#setKeyGenParameterSpec(android.security.keystore.KeyGenParameterSpec)` should be used to set the parameters to use for building the master key. Calling either function after the other will throw an `https://developer.android.com/reference/java/lang/IllegalArgumentException.html`.

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/security/crypto/MasterKey.KeyScheme keyScheme` | The KeyScheme to use. |

| Returns |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/security/crypto/MasterKey.Builder` | This builder. |

### setRequestStrongBoxBacked

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/security#1.1.0) Deprecated in [1.1.0](https://developer.android.com/jetpack/androidx/releases/security#1.1.0)

```
public @NonNull MasterKey.Builder [setRequestStrongBoxBacked](https://developer.android.com/reference/androidx/security/crypto/MasterKey.Builder#setRequestStrongBoxBacked(boolean))(boolean requestStrongBoxBacked)
```

> [!CAUTION]
> **This method is deprecated.**   
> Deprecated in Java

Sets whether or not to request this key is strong box backed. This setting is only applicable on `https://developer.android.com/reference/android/os/Build.VERSION_CODES.html#P` and above, and only on devices that support Strongbox.

| Parameters |
|---|---|
| `boolean requestStrongBoxBacked` | Whether to request to use strongbox |

| Returns |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/security/crypto/MasterKey.Builder` | This builder. |

### setUserAuthenticationRequired

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/security#1.1.0) Deprecated in [1.1.0](https://developer.android.com/jetpack/androidx/releases/security#1.1.0)

```
public @NonNull MasterKey.Builder [setUserAuthenticationRequired](https://developer.android.com/reference/androidx/security/crypto/MasterKey.Builder#setUserAuthenticationRequired(boolean))(boolean authenticationRequired)
```

> [!CAUTION]
> **This method is deprecated.**   
> Deprecated in Java

When used with `https://developer.android.com/reference/androidx/security/crypto/MasterKey.Builder#setKeyScheme(androidx.security.crypto.MasterKey.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 `https://developer.android.com/reference/androidx/security/crypto/MasterKey#getDefaultAuthenticationValidityDurationSeconds()`.

| Parameters |
|---|---|
| `boolean authenticationRequired` | Whether user authentication should be required to use the key. |

| Returns |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/security/crypto/MasterKey.Builder` | This builder. |

### setUserAuthenticationRequired

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/security#1.1.0) Deprecated in [1.1.0](https://developer.android.com/jetpack/androidx/releases/security#1.1.0)

```
public @NonNull MasterKey.Builder [setUserAuthenticationRequired](https://developer.android.com/reference/androidx/security/crypto/MasterKey.Builder#setUserAuthenticationRequired(boolean,int))(
    boolean authenticationRequired,
    @IntRange(from = 1) int userAuthenticationValidityDurationSeconds
)
```

> [!CAUTION]
> **This method is deprecated.**   
> Deprecated in Java

When used with `https://developer.android.com/reference/androidx/security/crypto/MasterKey.Builder#setKeyScheme(androidx.security.crypto.MasterKey.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 |
|---|---|
| `boolean authenticationRequired` | Whether user authentication should be required to use the key. |
| `@https://developer.android.com/reference/androidx/annotation/IntRange(from = 1) int userAuthenticationValidityDurationSeconds` | Duration in seconds that the key should remain unlocked following user authentication. |

| Returns |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/security/crypto/MasterKey.Builder` | This builder. |