当前位置: 首页>>代码示例>>Java>>正文


Java KeyPairGeneratorSpec.Builder方法代码示例

本文整理汇总了Java中android.security.KeyPairGeneratorSpec.Builder方法的典型用法代码示例。如果您正苦于以下问题:Java KeyPairGeneratorSpec.Builder方法的具体用法?Java KeyPairGeneratorSpec.Builder怎么用?Java KeyPairGeneratorSpec.Builder使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在android.security.KeyPairGeneratorSpec的用法示例。


在下文中一共展示了KeyPairGeneratorSpec.Builder方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: shouldThrowOnNoSuchProviderErrorWhenTryingToObtainRSAKeys

import android.security.KeyPairGeneratorSpec; //导入方法依赖的package包/类
@Test
public void shouldThrowOnNoSuchProviderErrorWhenTryingToObtainRSAKeys() throws Exception {
    ReflectionHelpers.setStaticField(Build.VERSION.class, "SDK_INT", 19);
    exception.expect(KeyException.class);
    exception.expectMessage("An error occurred while trying to obtain the RSA KeyPair Entry from the Android KeyStore.");

    PowerMockito.when(keyStore.containsAlias(KEY_ALIAS)).thenReturn(false);
    KeyPairGeneratorSpec spec = PowerMockito.mock(KeyPairGeneratorSpec.class);
    KeyPairGeneratorSpec.Builder builder = newKeyPairGeneratorSpecBuilder(spec);
    PowerMockito.whenNew(KeyPairGeneratorSpec.Builder.class).withAnyArguments().thenReturn(builder);

    PowerMockito.mockStatic(KeyPairGenerator.class);
    PowerMockito.when(KeyPairGenerator.getInstance(ALGORITHM_RSA, ANDROID_KEY_STORE))
            .thenThrow(new NoSuchProviderException());

    cryptoUtil.getRSAKeyEntry();
}
 
开发者ID:auth0,项目名称:Auth0.Android,代码行数:18,代码来源:CryptoUtilTest.java

示例2: shouldThrowOnNoSuchAlgorithmErrorWhenTryingToObtainRSAKeys

import android.security.KeyPairGeneratorSpec; //导入方法依赖的package包/类
@Test
public void shouldThrowOnNoSuchAlgorithmErrorWhenTryingToObtainRSAKeys() throws Exception {
    ReflectionHelpers.setStaticField(Build.VERSION.class, "SDK_INT", 19);
    exception.expect(KeyException.class);
    exception.expectMessage("An error occurred while trying to obtain the RSA KeyPair Entry from the Android KeyStore.");

    PowerMockito.when(keyStore.containsAlias(KEY_ALIAS)).thenReturn(false);
    KeyPairGeneratorSpec spec = PowerMockito.mock(KeyPairGeneratorSpec.class);
    KeyPairGeneratorSpec.Builder builder = newKeyPairGeneratorSpecBuilder(spec);
    PowerMockito.whenNew(KeyPairGeneratorSpec.Builder.class).withAnyArguments().thenReturn(builder);

    PowerMockito.mockStatic(KeyPairGenerator.class);
    PowerMockito.when(KeyPairGenerator.getInstance(ALGORITHM_RSA, ANDROID_KEY_STORE))
            .thenThrow(new NoSuchAlgorithmException());

    cryptoUtil.getRSAKeyEntry();
}
 
开发者ID:auth0,项目名称:Auth0.Android,代码行数:18,代码来源:CryptoUtilTest.java

示例3: shouldThrowOnInvalidAlgorithmParameterErrorWhenTryingToObtainRSAKeys

import android.security.KeyPairGeneratorSpec; //导入方法依赖的package包/类
@Test
public void shouldThrowOnInvalidAlgorithmParameterErrorWhenTryingToObtainRSAKeys() throws Exception {
    ReflectionHelpers.setStaticField(Build.VERSION.class, "SDK_INT", 19);
    exception.expect(KeyException.class);
    exception.expectMessage("An error occurred while trying to obtain the RSA KeyPair Entry from the Android KeyStore.");

    PowerMockito.when(keyStore.containsAlias(KEY_ALIAS)).thenReturn(false);
    KeyPairGeneratorSpec spec = PowerMockito.mock(KeyPairGeneratorSpec.class);
    KeyPairGeneratorSpec.Builder builder = newKeyPairGeneratorSpecBuilder(spec);
    PowerMockito.whenNew(KeyPairGeneratorSpec.Builder.class).withAnyArguments().thenReturn(builder);

    doThrow(new InvalidAlgorithmParameterException()).when(keyPairGenerator).initialize(any(AlgorithmParameterSpec.class));

    cryptoUtil.getRSAKeyEntry();
}
 
开发者ID:auth0,项目名称:Auth0.Android,代码行数:16,代码来源:CryptoUtilTest.java

示例4: newKeyPairGeneratorSpecBuilder

import android.security.KeyPairGeneratorSpec; //导入方法依赖的package包/类
private KeyPairGeneratorSpec.Builder newKeyPairGeneratorSpecBuilder(KeyPairGeneratorSpec expectedBuilderOutput) {
    KeyPairGeneratorSpec.Builder builder = PowerMockito.mock(KeyPairGeneratorSpec.Builder.class);
    PowerMockito.when(builder.setAlias(anyString())).thenReturn(builder);
    PowerMockito.when(builder.setSubject(any(X500Principal.class))).thenReturn(builder);
    PowerMockito.when(builder.setKeySize(anyInt())).thenReturn(builder);
    PowerMockito.when(builder.setSerialNumber(any(BigInteger.class))).thenReturn(builder);
    PowerMockito.when(builder.setStartDate(any(Date.class))).thenReturn(builder);
    PowerMockito.when(builder.setEndDate(any(Date.class))).thenReturn(builder);
    PowerMockito.when(builder.setEncryptionRequired()).thenReturn(builder);
    PowerMockito.when(builder.build()).thenReturn(expectedBuilderOutput);
    return builder;
}
 
开发者ID:auth0,项目名称:Auth0.Android,代码行数:13,代码来源:CryptoUtilTest.java

示例5: shouldNotCreateProtectedRSAKeyPairIfMissingAndLockScreenEnabledOnAPI19

import android.security.KeyPairGeneratorSpec; //导入方法依赖的package包/类
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Test
@Config(constants = com.auth0.android.auth0.BuildConfig.class, sdk = 19, manifest = Config.NONE)
public void shouldNotCreateProtectedRSAKeyPairIfMissingAndLockScreenEnabledOnAPI19() throws Exception {
    ReflectionHelpers.setStaticField(Build.VERSION.class, "SDK_INT", 19);

    PowerMockito.when(keyStore.containsAlias(KEY_ALIAS)).thenReturn(false);
    KeyStore.PrivateKeyEntry expectedEntry = PowerMockito.mock(KeyStore.PrivateKeyEntry.class);
    PowerMockito.when(keyStore.getEntry(KEY_ALIAS, null)).thenReturn(expectedEntry);

    KeyPairGeneratorSpec spec = PowerMockito.mock(KeyPairGeneratorSpec.class);
    KeyPairGeneratorSpec.Builder builder = newKeyPairGeneratorSpecBuilder(spec);
    PowerMockito.whenNew(KeyPairGeneratorSpec.Builder.class).withAnyArguments().thenReturn(builder);

    ArgumentCaptor<X500Principal> principalCaptor = ArgumentCaptor.forClass(X500Principal.class);
    ArgumentCaptor<Date> startDateCaptor = ArgumentCaptor.forClass(Date.class);
    ArgumentCaptor<Date> endDateCaptor = ArgumentCaptor.forClass(Date.class);

    //Set LockScreen as Enabled
    KeyguardManager kService = PowerMockito.mock(KeyguardManager.class);
    PowerMockito.when(context.getSystemService(Context.KEYGUARD_SERVICE)).thenReturn(kService);
    PowerMockito.when(kService.isKeyguardSecure()).thenReturn(true);

    final KeyStore.PrivateKeyEntry entry = cryptoUtil.getRSAKeyEntry();

    Mockito.verify(builder).setKeySize(2048);
    Mockito.verify(builder).setSubject(principalCaptor.capture());
    Mockito.verify(builder).setAlias(KEY_ALIAS);
    Mockito.verify(builder).setSerialNumber(BigInteger.ONE);
    Mockito.verify(builder).setStartDate(startDateCaptor.capture());
    Mockito.verify(builder).setEndDate(endDateCaptor.capture());
    Mockito.verify(builder, Mockito.never()).setEncryptionRequired();
    Mockito.verify(keyPairGenerator).initialize(spec);
    Mockito.verify(keyPairGenerator).generateKeyPair();

    assertThat(principalCaptor.getValue(), is(notNullValue()));
    assertThat(principalCaptor.getValue().getName(), is(CERTIFICATE_PRINCIPAL));

    assertThat(startDateCaptor.getValue(), is(notNullValue()));
    long diffMillis = startDateCaptor.getValue().getTime() - new Date().getTime();
    long days = TimeUnit.MILLISECONDS.toDays(diffMillis);
    assertThat(days, is(0L)); //Date is Today

    assertThat(endDateCaptor.getValue(), is(notNullValue()));
    diffMillis = endDateCaptor.getValue().getTime() - new Date().getTime();
    days = TimeUnit.MILLISECONDS.toDays(diffMillis);
    assertThat(days, is(greaterThan(25 * 365L))); //Date more than 25 Years in days

    assertThat(entry, is(expectedEntry));
}
 
开发者ID:auth0,项目名称:Auth0.Android,代码行数:51,代码来源:CryptoUtilTest.java

示例6: shouldCreateUnprotectedRSAKeyPairIfMissingAndLockScreenDisabledOnAPI21

import android.security.KeyPairGeneratorSpec; //导入方法依赖的package包/类
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Test
@Config(constants = com.auth0.android.auth0.BuildConfig.class, sdk = 21, manifest = Config.NONE)
public void shouldCreateUnprotectedRSAKeyPairIfMissingAndLockScreenDisabledOnAPI21() throws Exception {
    ReflectionHelpers.setStaticField(Build.VERSION.class, "SDK_INT", 21);

    PowerMockito.when(keyStore.containsAlias(KEY_ALIAS)).thenReturn(false);
    KeyStore.PrivateKeyEntry expectedEntry = PowerMockito.mock(KeyStore.PrivateKeyEntry.class);
    PowerMockito.when(keyStore.getEntry(KEY_ALIAS, null)).thenReturn(expectedEntry);

    KeyPairGeneratorSpec spec = PowerMockito.mock(KeyPairGeneratorSpec.class);
    KeyPairGeneratorSpec.Builder builder = newKeyPairGeneratorSpecBuilder(spec);
    PowerMockito.whenNew(KeyPairGeneratorSpec.Builder.class).withAnyArguments().thenReturn(builder);

    ArgumentCaptor<X500Principal> principalCaptor = ArgumentCaptor.forClass(X500Principal.class);
    ArgumentCaptor<Date> startDateCaptor = ArgumentCaptor.forClass(Date.class);
    ArgumentCaptor<Date> endDateCaptor = ArgumentCaptor.forClass(Date.class);

    //Set LockScreen as Disabled
    KeyguardManager kService = PowerMockito.mock(KeyguardManager.class);
    PowerMockito.when(context.getSystemService(Context.KEYGUARD_SERVICE)).thenReturn(kService);
    PowerMockito.when(kService.isKeyguardSecure()).thenReturn(false);
    PowerMockito.when(kService.createConfirmDeviceCredentialIntent(any(CharSequence.class), any(CharSequence.class))).thenReturn(null);

    final KeyStore.PrivateKeyEntry entry = cryptoUtil.getRSAKeyEntry();

    Mockito.verify(builder).setKeySize(2048);
    Mockito.verify(builder).setSubject(principalCaptor.capture());
    Mockito.verify(builder).setAlias(KEY_ALIAS);
    Mockito.verify(builder).setSerialNumber(BigInteger.ONE);
    Mockito.verify(builder).setStartDate(startDateCaptor.capture());
    Mockito.verify(builder).setEndDate(endDateCaptor.capture());
    Mockito.verify(builder, Mockito.never()).setEncryptionRequired();
    Mockito.verify(keyPairGenerator).initialize(spec);
    Mockito.verify(keyPairGenerator).generateKeyPair();

    assertThat(principalCaptor.getValue(), is(notNullValue()));
    assertThat(principalCaptor.getValue().getName(), is(CERTIFICATE_PRINCIPAL));

    assertThat(startDateCaptor.getValue(), is(notNullValue()));
    long diffMillis = startDateCaptor.getValue().getTime() - new Date().getTime();
    long days = TimeUnit.MILLISECONDS.toDays(diffMillis);
    assertThat(days, is(0L)); //Date is Today

    assertThat(endDateCaptor.getValue(), is(notNullValue()));
    diffMillis = endDateCaptor.getValue().getTime() - new Date().getTime();
    days = TimeUnit.MILLISECONDS.toDays(diffMillis);
    assertThat(days, is(greaterThan(25 * 365L))); //Date more than 25 Years in days

    assertThat(entry, is(expectedEntry));
}
 
开发者ID:auth0,项目名称:Auth0.Android,代码行数:52,代码来源:CryptoUtilTest.java

示例7: shouldCreateProtectedRSAKeyPairIfMissingAndLockScreenEnabledOnAPI21

import android.security.KeyPairGeneratorSpec; //导入方法依赖的package包/类
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Test
@Config(constants = com.auth0.android.auth0.BuildConfig.class, sdk = 21, manifest = Config.NONE)
public void shouldCreateProtectedRSAKeyPairIfMissingAndLockScreenEnabledOnAPI21() throws Exception {
    ReflectionHelpers.setStaticField(Build.VERSION.class, "SDK_INT", 21);

    PowerMockito.when(keyStore.containsAlias(KEY_ALIAS)).thenReturn(false);
    KeyStore.PrivateKeyEntry expectedEntry = PowerMockito.mock(KeyStore.PrivateKeyEntry.class);
    PowerMockito.when(keyStore.getEntry(KEY_ALIAS, null)).thenReturn(expectedEntry);

    KeyPairGeneratorSpec spec = PowerMockito.mock(KeyPairGeneratorSpec.class);
    KeyPairGeneratorSpec.Builder builder = newKeyPairGeneratorSpecBuilder(spec);
    PowerMockito.whenNew(KeyPairGeneratorSpec.Builder.class).withAnyArguments().thenReturn(builder);

    ArgumentCaptor<X500Principal> principalCaptor = ArgumentCaptor.forClass(X500Principal.class);
    ArgumentCaptor<Date> startDateCaptor = ArgumentCaptor.forClass(Date.class);
    ArgumentCaptor<Date> endDateCaptor = ArgumentCaptor.forClass(Date.class);

    //Set LockScreen as Enabled
    KeyguardManager kService = PowerMockito.mock(KeyguardManager.class);
    PowerMockito.when(context.getSystemService(Context.KEYGUARD_SERVICE)).thenReturn(kService);
    PowerMockito.when(kService.isKeyguardSecure()).thenReturn(true);
    PowerMockito.when(kService.createConfirmDeviceCredentialIntent(any(CharSequence.class), any(CharSequence.class))).thenReturn(new Intent());

    final KeyStore.PrivateKeyEntry entry = cryptoUtil.getRSAKeyEntry();

    Mockito.verify(builder).setKeySize(2048);
    Mockito.verify(builder).setSubject(principalCaptor.capture());
    Mockito.verify(builder).setAlias(KEY_ALIAS);
    Mockito.verify(builder).setSerialNumber(BigInteger.ONE);
    Mockito.verify(builder).setStartDate(startDateCaptor.capture());
    Mockito.verify(builder).setEndDate(endDateCaptor.capture());
    Mockito.verify(builder).setEncryptionRequired();
    Mockito.verify(keyPairGenerator).initialize(spec);
    Mockito.verify(keyPairGenerator).generateKeyPair();

    assertThat(principalCaptor.getValue(), is(notNullValue()));
    assertThat(principalCaptor.getValue().getName(), is(CERTIFICATE_PRINCIPAL));

    assertThat(startDateCaptor.getValue(), is(notNullValue()));
    long diffMillis = startDateCaptor.getValue().getTime() - new Date().getTime();
    long days = TimeUnit.MILLISECONDS.toDays(diffMillis);
    assertThat(days, is(0L)); //Date is Today

    assertThat(endDateCaptor.getValue(), is(notNullValue()));
    diffMillis = endDateCaptor.getValue().getTime() - new Date().getTime();
    days = TimeUnit.MILLISECONDS.toDays(diffMillis);
    assertThat(days, is(greaterThan(25 * 365L))); //Date more than 25 Years in days

    assertThat(entry, is(expectedEntry));
}
 
开发者ID:auth0,项目名称:Auth0.Android,代码行数:52,代码来源:CryptoUtilTest.java


注:本文中的android.security.KeyPairGeneratorSpec.Builder方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。