Android APIs
public static final class

KeyPairGeneratorSpec.Builder

extends Object
java.lang.Object
   ↳ android.security.KeyPairGeneratorSpec.Builder

Class Overview

Builder class for KeyPairGeneratorSpec objects.

This will build a parameter spec for use with the Android KeyStore facility.

The required fields must be filled in with the builder.

Example:

 Calendar start = new Calendar();
 Calendar end = new Calendar();
 end.add(1, Calendar.YEAR);

 KeyPairGeneratorSpec spec =
         new KeyPairGeneratorSpec.Builder(mContext).setAlias("myKey")
                 .setSubject(new X500Principal("CN=myKey")).setSerial(BigInteger.valueOf(1337))
                 .setStartDate(start.getTime()).setEndDate(end.getTime()).build();
 

Summary

Public Constructors
KeyPairGeneratorSpec.Builder(Context context)
Creates a new instance of the Builder with the given context.
Public Methods
KeyPairGeneratorSpec build()
Builds the instance of the KeyPairGeneratorSpec.
KeyPairGeneratorSpec.Builder setAlgorithmParameterSpec(AlgorithmParameterSpec spec)
Sets the algorithm-specific key generation parameters.
KeyPairGeneratorSpec.Builder setAlias(String alias)
Sets the alias to be used to retrieve the key later from a KeyStore instance using the AndroidKeyStore provider.
KeyPairGeneratorSpec.Builder setEncryptionRequired()
Indicates that this key must be encrypted at rest on storage.
KeyPairGeneratorSpec.Builder setEndDate(Date endDate)
Sets the end of the validity period for the self-signed certificate of the generated key pair.
KeyPairGeneratorSpec.Builder setKeySize(int keySize)
Sets the key size for the keypair to be created.
KeyPairGeneratorSpec.Builder setKeyType(String keyType)
Sets the key type (e.g., EC, RSA) of the keypair to be created.
KeyPairGeneratorSpec.Builder setSerialNumber(BigInteger serialNumber)
Sets the serial number used for the self-signed certificate of the generated key pair.
KeyPairGeneratorSpec.Builder setStartDate(Date startDate)
Sets the start of the validity period for the self-signed certificate of the generated key pair.
KeyPairGeneratorSpec.Builder setSubject(X500Principal subject)
Sets the subject used for the self-signed certificate of the generated key pair.
[Expand]
Inherited Methods
From class java.lang.Object

Public Constructors

public KeyPairGeneratorSpec.Builder (Context context)

Added in API level 18

Creates a new instance of the Builder with the given context. The context passed in may be used to pop up some UI to ask the user to unlock or initialize the Android KeyStore facility.

Public Methods

public KeyPairGeneratorSpec build ()

Added in API level 18

Builds the instance of the KeyPairGeneratorSpec.

Returns
  • built instance of KeyPairGeneratorSpec
Throws
IllegalArgumentException if a required field is missing

public KeyPairGeneratorSpec.Builder setAlgorithmParameterSpec (AlgorithmParameterSpec spec)

Added in API level 19

Sets the algorithm-specific key generation parameters. For example, for RSA keys this may be an instance of RSAKeyGenParameterSpec.

public KeyPairGeneratorSpec.Builder setAlias (String alias)

Added in API level 18

Sets the alias to be used to retrieve the key later from a KeyStore instance using the AndroidKeyStore provider.

public KeyPairGeneratorSpec.Builder setEncryptionRequired ()

Added in API level 18

Indicates that this key must be encrypted at rest on storage. Note that enabling this will require that the user enable a strong lock screen (e.g., PIN, password) before creating or using the generated key is successful.

public KeyPairGeneratorSpec.Builder setEndDate (Date endDate)

Added in API level 18

Sets the end of the validity period for the self-signed certificate of the generated key pair.

The date must be specified on API Level LOLLIPOP_MR1 and older platforms. On newer platforms the date defaults to Jan 1 2048 if not specified.

public KeyPairGeneratorSpec.Builder setKeySize (int keySize)

Added in API level 19

Sets the key size for the keypair to be created. For instance, for a key type of RSA this will set the modulus size and for a key type of EC it will select a curve with a matching field size.

public KeyPairGeneratorSpec.Builder setKeyType (String keyType)

Added in API level 19

Sets the key type (e.g., EC, RSA) of the keypair to be created.

public KeyPairGeneratorSpec.Builder setSerialNumber (BigInteger serialNumber)

Added in API level 18

Sets the serial number used for the self-signed certificate of the generated key pair.

The serial number must be specified on API Level LOLLIPOP_MR1 and older platforms. On newer platforms the serial number defaults to 1 if not specified.

public KeyPairGeneratorSpec.Builder setStartDate (Date startDate)

Added in API level 18

Sets the start of the validity period for the self-signed certificate of the generated key pair.

The date must be specified on API Level LOLLIPOP_MR1 and older platforms. On newer platforms the date defaults to Jan 1 1970 if not specified.

public KeyPairGeneratorSpec.Builder setSubject (X500Principal subject)

Added in API level 18

Sets the subject used for the self-signed certificate of the generated key pair.

The subject must be specified on API Level LOLLIPOP_MR1 and older platforms. On newer platforms the subject defaults to CN=fake if not specified.