org.apache.shiro.crypto.hash.HashRequest Java Examples

The following examples show how to use org.apache.shiro.crypto.hash.HashRequest. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: QueryAndEncodeDatabaseAuthenticationHandlerTests.java    From springboot-shiro-cas-mybatis with MIT License 6 votes vote down vote up
private 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);

        final String pswEnc = hash.computeHash(new HashRequest.Builder()
                .setSource(psw).setSalt(salt).setIterations(iter).build()).toHex();

        return pswEnc;

    } catch (final Exception e) {
        throw new RuntimeException(e);
    }
}
 
Example #2
Source File: SHA512.java    From JavaSecurity with Apache License 2.0 6 votes vote down vote up
private static Hash calculateHash(String password) {
    ByteSource privateSalt = ByteSource.Util.bytes(PRIVATE_SALT_BYTES);
    DefaultHashService hashService = new DefaultHashService();
    hashService.setPrivateSalt(privateSalt);
    hashService.setGeneratePublicSalt(true);
    hashService.setHashIterations(ITERATIONS);

    HashRequest.Builder builder = new HashRequest.Builder();
    builder.setSource(ByteSource.Util.bytes(password));

    Hash hash = hashService.computeHash(builder.build());

    log.info("Hash algorithm {}, iterations {}, public salt {}", hash.getAlgorithmName(), hash.getIterations(), hash.getSalt());

    return hash;
}
 
Example #3
Source File: SHA512.java    From JavaSecurity with Apache License 2.0 6 votes vote down vote up
private static boolean verifyPassword(byte[] originalHash, ByteSource publicSalt, String password) {
    ByteSource privateSalt = ByteSource.Util.bytes(PRIVATE_SALT_BYTES);
    DefaultHashService hashService = new DefaultHashService();
    hashService.setPrivateSalt(privateSalt);
    hashService.setHashIterations(ITERATIONS);

    HashRequest.Builder builder = new HashRequest.Builder();
    builder.setSource(ByteSource.Util.bytes(password));
    builder.setSalt(publicSalt);

    Hash comparisonHash = hashService.computeHash(builder.build());

    log.info("password: {}", password);
    log.info("1 hash: {}", Hex.encodeToString(originalHash));
    log.info("2 hash: {}", comparisonHash.toHex());

    return Arrays.equals(originalHash, comparisonHash.getBytes());
}
 
Example #4
Source File: QueryAndEncodeDatabaseAuthenticationHandler.java    From springboot-shiro-cas-mybatis with MIT License 5 votes vote down vote up
/**
 * 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();
}
 
Example #5
Source File: PasswordSaltTest.java    From sso with MIT License 5 votes vote down vote up
@Test
    public void test() throws Exception {
        ConfigurableHashService hashService = new DefaultHashService();
        hashService.setPrivateSalt(ByteSource.Util.bytes(this.staticSalt));
        hashService.setHashAlgorithmName(this.algorithmName);
        hashService.setHashIterations(2);
        HashRequest request = new HashRequest.Builder()
                .setSalt(dynaSalt)
                .setSource(encodedPassword)
                .build();
        String res =  hashService.computeHash(request).toHex();
//        System.out.println(res);
        Assert.assertEquals("bfb194d5bd84a5fc77c1d303aefd36c3", res);
    }
 
Example #6
Source File: LdapRealm.java    From zeppelin with Apache License 2.0 5 votes vote down vote up
@Override
protected AuthenticationInfo createAuthenticationInfo(AuthenticationToken token,
    Object ldapPrincipal, Object ldapCredentials, LdapContext ldapContext)
    throws NamingException {
  HashRequest.Builder builder = new HashRequest.Builder();
  Hash credentialsHash = hashService
      .computeHash(builder.setSource(token.getCredentials())
          .setAlgorithmName(HASHING_ALGORITHM).build());
  return new SimpleAuthenticationInfo(token.getPrincipal(),
      credentialsHash.toHex(), credentialsHash.getSalt(),
      getName());
}
 
Example #7
Source File: EntityCrypto.java    From scipio-erp with Apache License 2.0 4 votes vote down vote up
@Override
protected String getHashedKeyName(String originalKeyName) {
    HashRequest hashRequest = new HashRequest.Builder().setSource(originalKeyName).build();
    return hashService.computeHash(hashRequest).toBase64();
}
 
Example #8
Source File: KnoxLdapRealm.java    From knox with Apache License 2.0 4 votes vote down vote up
@Override
protected AuthenticationInfo createAuthenticationInfo(AuthenticationToken token, Object ldapPrincipal, Object ldapCredentials, LdapContext ldapContext) throws NamingException {
  HashRequest.Builder builder = new HashRequest.Builder();
  Hash credentialsHash = hashService.computeHash(builder.setSource(token.getCredentials()).setAlgorithmName(HASHING_ALGORITHM).build());
  return new SimpleAuthenticationInfo(token.getPrincipal(), credentialsHash.toHex(), credentialsHash.getSalt(), getName());
}