Example usage for org.apache.shiro.crypto.hash Hash getIterations

List of usage examples for org.apache.shiro.crypto.hash Hash getIterations

Introduction

In this page you can find the example usage for org.apache.shiro.crypto.hash Hash getIterations.

Prototype

int getIterations();

Source Link

Document

Returns the number of hash iterations used to compute the hash.

Usage

From source file:com.azaptree.services.security.domain.impl.HashedCredentialImpl.java

License:Apache License

public HashedCredentialImpl(final UUID subjectId, final String name, final UUID hashServiceConfigurationId,
        final Hash hash, final Date expiresOn) {
    this(subjectId, name, hashServiceConfigurationId, hash.getBytes(), hash.getAlgorithmName(),
            hash.getIterations(), hash.getSalt().getBytes(), expiresOn);
}

From source file:com.masslink.idea.zigbee.shiro.UserPasswordService.java

License:Apache License

protected HashRequest buildHashRequest(ByteSource plaintext, Hash saved) {
    //keep everything from the saved hash except for the source:
    return new HashRequest.Builder().setSource(plaintext).setAlgorithmName(saved.getAlgorithmName())
            .setSalt(saved.getSalt()).setIterations(saved.getIterations()).build();
}

From source file:de.dominikschadow.javasecurity.hash.SHA512.java

License:Apache License

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());//from  www.jav  a2  s . co  m

    return hash;
}

From source file:de.dominikschadow.javasecurity.hash.SHA512HashSample.java

License:Apache License

private 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());

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

    return hash;/*from w ww  .  ja v  a  2s  .com*/
}

From source file:test.com.azaptree.services.security.dao.HashedCredentialDAOTest.java

License:Apache License

@Transactional
@Test/*from   w w w  .  j ava  2  s. com*/
public void test_create_findById_cascade_delete() {
    final Subject temp = new SubjectImpl(Status.ACTIVATED);
    final Subject subject = subjectDao.create(temp);

    final HashRequest hashRequest = new HashRequest.Builder().setSource("password").build();
    final Hash hash = hashService.computeHash(hashRequest);
    final Calendar now = Calendar.getInstance();
    now.add(Calendar.DATE, 90);
    final long expiresOn = now.getTimeInMillis();
    final HashedCredential password = new HashedCredentialImpl(subject.getEntityId(), "password",
            hashServiceConfig.getEntityId(), hash.getBytes(), hash.getAlgorithmName(), hash.getIterations(),
            hash.getSalt().getBytes(), now.getTime());
    final HashedCredential savedPassword = hashedCredentialDAO.create(password);

    Assert.assertNotNull(savedPassword);
    Assert.assertNotNull(savedPassword.getEntityId());
    Assert.assertNotNull(savedPassword.getSubjectId());
    log.info(savedPassword.toJson());

    final HashedCredential password2 = hashedCredentialDAO.findById(savedPassword.getEntityId());
    Assert.assertNotNull(password2);
    Assert.assertNotNull(password2.getEntityId());
    Assert.assertNotNull(password2.getSubjectId());
    Assert.assertEquals(password2.getExpiresOn().get().getTime(), expiresOn);
    log.info(password2.toJson());

    Assert.assertEquals(password2, savedPassword);

    subjectDao.delete(subject.getEntityId());
    Assert.assertNull(hashedCredentialDAO.findById(savedPassword.getEntityId()));
}

From source file:test.com.azaptree.services.security.dao.HashedCredentialDAOTest.java

License:Apache License

@Transactional
@Test/* ww  w  . j  ava2 s .  c  om*/
public void test_create_findById_delete() {
    final Subject temp = new SubjectImpl(Status.ACTIVATED);
    final Subject subject = subjectDao.create(temp);

    final HashRequest hashRequest = new HashRequest.Builder().setSource("password").build();
    final Hash hash = hashService.computeHash(hashRequest);
    final HashedCredential password = new HashedCredentialImpl(subject.getEntityId(), "password",
            hashServiceConfig.getEntityId(), hash.getBytes(), hash.getAlgorithmName(), hash.getIterations(),
            hash.getSalt().getBytes(), null);
    final HashedCredential savedPassword = hashedCredentialDAO.create(password);

    Assert.assertNotNull(savedPassword);
    Assert.assertNotNull(savedPassword.getEntityId());
    Assert.assertNotNull(savedPassword.getSubjectId());
    log.info(savedPassword.toJson());

    final HashedCredential password2 = hashedCredentialDAO.findById(savedPassword.getEntityId());
    Assert.assertNotNull(password2);
    Assert.assertNotNull(password2.getEntityId());
    Assert.assertNotNull(password2.getSubjectId());
    Assert.assertNotNull(password2.getHash());
    Assert.assertNotNull(password2.getHashAlgorithm());
    Assert.assertNotNull(password2.getSalt());
    Assert.assertEquals(password2.getSubjectId(), subject.getEntityId());
    Assert.assertTrue(Arrays.equals(hash.getBytes(), password2.getHash()));
    Assert.assertTrue(Arrays.equals(hash.getSalt().getBytes(), password2.getSalt()));

    log.info(password2.toJson());

    Assert.assertEquals(password2, savedPassword);

    hashedCredentialDAO.delete(savedPassword.getEntityId());
    Assert.assertNull(hashedCredentialDAO.findById(savedPassword.getEntityId()));
}

From source file:test.com.azaptree.services.security.dao.HashedCredentialDAOTest.java

License:Apache License

@Transactional
@Test// w w w.j  ava 2s  . c  om
public void test_create_withCreatedBy_findById_delete() {
    final Subject temp = new SubjectImpl(Status.ACTIVATED);
    final Subject subject = subjectDao.create(temp);

    final HashRequest hashRequest = new HashRequest.Builder().setSource("password").build();
    final Hash hash = hashService.computeHash(hashRequest);
    final HashedCredential password = new HashedCredentialImpl(subject.getEntityId(), "password",
            hashServiceConfig.getEntityId(), hash.getBytes(), hash.getAlgorithmName(), hash.getIterations(),
            hash.getSalt().getBytes(), null);
    final HashedCredential savedPassword = hashedCredentialDAO.create(password, UUID.randomUUID());

    Assert.assertNotNull(savedPassword);
    Assert.assertNotNull(savedPassword.getEntityId());
    Assert.assertNotNull(savedPassword.getSubjectId());
    log.info(savedPassword.toJson());

    final HashedCredential password2 = hashedCredentialDAO.findById(savedPassword.getEntityId());
    Assert.assertNotNull(password2);
    Assert.assertTrue(password2.getCreatedByEntityId().isPresent());
    Assert.assertNotNull(password2.getCreatedByEntityId().get());
    Assert.assertNotNull(password2.getEntityId());
    Assert.assertNotNull(password2.getSubjectId());
    Assert.assertNotNull(password2.getHash());
    Assert.assertNotNull(password2.getHashAlgorithm());
    Assert.assertNotNull(password2.getSalt());
    Assert.assertEquals(password2.getSubjectId(), subject.getEntityId());
    Assert.assertTrue(Arrays.equals(hash.getBytes(), password2.getHash()));
    Assert.assertTrue(Arrays.equals(hash.getSalt().getBytes(), password2.getSalt()));

    log.info(password2.toJson());

    Assert.assertEquals(password2, savedPassword);

    hashedCredentialDAO.delete(savedPassword.getEntityId());
    Assert.assertNull(hashedCredentialDAO.findById(savedPassword.getEntityId()));
}

From source file:test.com.azaptree.services.security.dao.HashedCredentialDAOTest.java

License:Apache License

@Transactional
@Test//from   w ww.j a v  a  2  s  .  c  o m
public void test_deleteBySubjectIdAndName() {
    final Subject temp = new SubjectImpl(Status.ACTIVATED);
    final Subject subject = subjectDao.create(temp);

    final HashRequest hashRequest = new HashRequest.Builder().setSource("password").build();
    final Hash hash = hashService.computeHash(hashRequest);
    final HashedCredential password = new HashedCredentialImpl(subject.getEntityId(), "password",
            hashServiceConfig.getEntityId(), hash.getBytes(), hash.getAlgorithmName(), hash.getIterations(),
            hash.getSalt().getBytes(), null);
    final HashedCredential savedPassword = hashedCredentialDAO.create(password);

    final HashedCredential retrievedCredential = hashedCredentialDAO
            .findBySubjectIdAndName(subject.getEntityId(), savedPassword.getName());
    Assert.assertEquals(retrievedCredential, savedPassword);

    Assert.assertNull(hashedCredentialDAO.findBySubjectIdAndName(subject.getEntityId(),
            savedPassword.getEntityId().toString()));

    Assert.assertTrue(
            hashedCredentialDAO.deleteBySubjectIdAndName(subject.getEntityId(), savedPassword.getName()));
    Assert.assertFalse(
            hashedCredentialDAO.deleteBySubjectIdAndName(subject.getEntityId(), savedPassword.getName()));
}

From source file:test.com.azaptree.services.security.dao.HashedCredentialDAOTest.java

License:Apache License

@Transactional
@Test//from  w w w .ja  v  a 2s .c om
public void test_existsForSubjectIdAndName() {
    final Subject temp = new SubjectImpl(Status.ACTIVATED);
    final Subject subject = subjectDao.create(temp);

    final HashRequest hashRequest = new HashRequest.Builder().setSource("password").build();
    final Hash hash = hashService.computeHash(hashRequest);
    final HashedCredential password = new HashedCredentialImpl(subject.getEntityId(), "password",
            hashServiceConfig.getEntityId(), hash.getBytes(), hash.getAlgorithmName(), hash.getIterations(),
            hash.getSalt().getBytes(), null);
    final HashedCredential savedPassword = hashedCredentialDAO.create(password);

    final HashedCredential retrievedCredential = hashedCredentialDAO
            .findBySubjectIdAndName(subject.getEntityId(), savedPassword.getName());
    Assert.assertEquals(retrievedCredential, savedPassword);
    Assert.assertTrue(
            hashedCredentialDAO.existsForSubjectIdAndName(subject.getEntityId(), savedPassword.getName()));

    Assert.assertNull(hashedCredentialDAO.findBySubjectIdAndName(subject.getEntityId(),
            savedPassword.getEntityId().toString()));
}

From source file:test.com.azaptree.services.security.dao.HashedCredentialDAOTest.java

License:Apache License

@Transactional
@Test/*from w w  w  .  java 2 s .c om*/
public void test_findBySubjectId() {
    final Subject temp = new SubjectImpl(Status.ACTIVATED);
    final Subject subject = subjectDao.create(temp);

    final HashRequest hashRequest = new HashRequest.Builder().setSource("password").build();
    final Hash hash = hashService.computeHash(hashRequest);
    final HashedCredential password = new HashedCredentialImpl(subject.getEntityId(), "password",
            hashServiceConfig.getEntityId(), hash.getBytes(), hash.getAlgorithmName(), hash.getIterations(),
            hash.getSalt().getBytes(), null);
    final HashedCredential savedPassword = hashedCredentialDAO.create(password);

    final Set<HashedCredential> credentials = hashedCredentialDAO.findBySubjectId(subject.getEntityId());
    Assert.assertFalse(credentials.isEmpty());
    Assert.assertEquals(credentials.size(), 1);
    Assert.assertTrue(credentials.contains(savedPassword));
    Assert.assertEquals(credentials.iterator().next().hashCode(), savedPassword.hashCode());

    Assert.assertTrue(hashedCredentialDAO.findBySubjectId(savedPassword.getEntityId()).isEmpty());
}