Search in sources :

Example 1 with DefaultHashService

use of org.apache.shiro.crypto.hash.DefaultHashService in project cas by apereo.

the class QueryAndEncodeDatabaseAuthenticationHandlerTests method genPassword.

private static String genPassword(final String psw, final String salt, final int iter) {
    try {
        final DefaultHashService hash = new DefaultHashService();
        hash.setPrivateSalt(ByteSource.Util.bytes(STATIC_SALT));
        hash.setHashIterations(iter);
        hash.setGeneratePublicSalt(false);
        hash.setHashAlgorithmName(ALG_NAME);
        return hash.computeHash(new HashRequest.Builder().setSource(psw).setSalt(salt).setIterations(iter).build()).toHex();
    } catch (final Exception e) {
        throw Throwables.propagate(e);
    }
}
Also used : DefaultHashService(org.apache.shiro.crypto.hash.DefaultHashService) FailedLoginException(javax.security.auth.login.FailedLoginException) ExpectedException(org.junit.rules.ExpectedException) AccountDisabledException(org.apereo.cas.authentication.exceptions.AccountDisabledException) AccountNotFoundException(javax.security.auth.login.AccountNotFoundException) AccountPasswordMustChangeException(org.apereo.cas.authentication.exceptions.AccountPasswordMustChangeException) PreventedException(org.apereo.cas.authentication.PreventedException)

Example 2 with DefaultHashService

use of org.apache.shiro.crypto.hash.DefaultHashService in project cas by apereo.

the class QueryAndEncodeDatabaseAuthenticationHandler method digestEncodedPassword.

/**
     * Digest encoded password.
     *
     * @param encodedPassword the encoded password
     * @param values          the values retrieved from database
     * @return the digested password
     */
protected String digestEncodedPassword(final String encodedPassword, final Map<String, Object> values) {
    final ConfigurableHashService hashService = new DefaultHashService();
    if (StringUtils.isNotBlank(this.staticSalt)) {
        hashService.setPrivateSalt(ByteSource.Util.bytes(this.staticSalt));
    }
    hashService.setHashAlgorithmName(this.algorithmName);
    Long numOfIterations = this.numberOfIterations;
    if (values.containsKey(this.numberOfIterationsFieldName)) {
        final String longAsStr = values.get(this.numberOfIterationsFieldName).toString();
        numOfIterations = Long.valueOf(longAsStr);
    }
    hashService.setHashIterations(numOfIterations.intValue());
    if (!values.containsKey(this.saltFieldName)) {
        throw new RuntimeException("Specified field name for salt does not exist in the results");
    }
    final String dynaSalt = values.get(this.saltFieldName).toString();
    final HashRequest request = new HashRequest.Builder().setSalt(dynaSalt).setSource(encodedPassword).build();
    return hashService.computeHash(request).toHex();
}
Also used : HashRequest(org.apache.shiro.crypto.hash.HashRequest) DefaultHashService(org.apache.shiro.crypto.hash.DefaultHashService) ConfigurableHashService(org.apache.shiro.crypto.hash.ConfigurableHashService)

Example 3 with DefaultHashService

use of org.apache.shiro.crypto.hash.DefaultHashService in project qi4j-sdk by Qi4j.

the class PasswordRealmMixin method activateService.

@Override
public void activateService() throws Exception {
    configuration.refresh();
    PasswordRealmConfiguration config = configuration.get();
    String algorithm = config.hashAlgorithmName().get();
    Integer iterations = config.hashIterationsCount().get();
    if (algorithm != null || iterations != null) {
        DefaultHashService hashService = (DefaultHashService) passwordService.getHashService();
        if (algorithm != null) {
            hashService.setHashAlgorithmName(algorithm);
        }
        if (iterations != null) {
            hashService.setHashIterations(iterations);
        }
    }
}
Also used : DefaultHashService(org.apache.shiro.crypto.hash.DefaultHashService)

Aggregations

DefaultHashService (org.apache.shiro.crypto.hash.DefaultHashService)3 AccountNotFoundException (javax.security.auth.login.AccountNotFoundException)1 FailedLoginException (javax.security.auth.login.FailedLoginException)1 ConfigurableHashService (org.apache.shiro.crypto.hash.ConfigurableHashService)1 HashRequest (org.apache.shiro.crypto.hash.HashRequest)1 PreventedException (org.apereo.cas.authentication.PreventedException)1 AccountDisabledException (org.apereo.cas.authentication.exceptions.AccountDisabledException)1 AccountPasswordMustChangeException (org.apereo.cas.authentication.exceptions.AccountPasswordMustChangeException)1 ExpectedException (org.junit.rules.ExpectedException)1