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


Java EncryptablePasswordSpec类代码示例

本文整理汇总了Java中org.wildfly.security.password.spec.EncryptablePasswordSpec的典型用法代码示例。如果您正苦于以下问题:Java EncryptablePasswordSpec类的具体用法?Java EncryptablePasswordSpec怎么用?Java EncryptablePasswordSpec使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


EncryptablePasswordSpec类属于org.wildfly.security.password.spec包,在下文中一共展示了EncryptablePasswordSpec类的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: createConfiguration

import org.wildfly.security.password.spec.EncryptablePasswordSpec; //导入依赖的package包/类
@Override
public Map<String, String> createConfiguration(final Map<String, String> attributes)
        throws GeneralSecurityException, IOException {
    final Provider provider = ProviderHelper
            .provider(option(attributes, "provider", ProviderHelper.WILDFLY_PROVIDER));

    final String algorithm = option(attributes, "algorithm", DEFAULT_ALGORITHM);
    final PasswordFactory passwordFactory = PasswordFactory.getInstance(algorithm, provider);

    final String password = option(attributes, "password", null);

    final String salt = option(attributes, "salt", "");
    final String iterations = option(attributes, "iterations", "");

    final AlgorithmParameterSpec algorithmParameterSpec;
    if (salt.isEmpty() && iterations.isEmpty()) {
        algorithmParameterSpec = null;
    } else if (salt.isEmpty()) {
        algorithmParameterSpec = new IteratedPasswordAlgorithmSpec(parseInt(iterations));
    } else {
        final byte[] saltBytes = Base64.getDecoder().decode(salt);
        algorithmParameterSpec = new IteratedSaltedPasswordAlgorithmSpec(parseInt(iterations), saltBytes);
    }

    final EncryptablePasswordSpec keySpec = new EncryptablePasswordSpec(password.toCharArray(),
            algorithmParameterSpec);

    final MaskedPassword maskedPassword = passwordFactory.generatePassword(keySpec).castAs(MaskedPassword.class);

    final MaskedPasswordAlgorithmSpec maskedPasswordAlgorithmSpec = maskedPassword.getParameterSpec();

    final Map<String, String> configuration = new HashMap<>();
    final Encoder encoder = Base64.getEncoder();

    if (!DEFAULT_ALGORITHM.equals(algorithm)) {
        configuration.put(CREDENTIAL_STORE_PROTECTION_ALGORITHM, algorithm);
    }

    configuration.put(CREDENTIAL_STORE_PROTECTION, encoder.encodeToString(maskedPassword.getMaskedPasswordBytes()));

    final AlgorithmParameters algorithmParameters = AlgorithmParameters.getInstance(algorithm, provider);
    algorithmParameters.init(maskedPasswordAlgorithmSpec);
    final byte[] encoded = algorithmParameters.getEncoded();

    configuration.put(CREDENTIAL_STORE_PROTECTION_PARAMS, encoder.encodeToString(encoded));

    return configuration;
}
 
开发者ID:jboss-fuse,项目名称:fuse-karaf,代码行数:49,代码来源:MaskedPasswordHelper.java


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