List of usage examples for org.apache.shiro.crypto.hash HashService computeHash
Hash computeHash(HashRequest request);
From source file:test.com.azaptree.services.security.config.HashServiceConfigTest.java
License:Apache License
@Test public void testHashService() { final SecureRandomNumberGenerator rng = new SecureRandomNumberGenerator(); final byte[] privateSalt = rng.nextBytes(32).getBytes(); final int hashIterations = 1024 * 128; final String algo = "SHA-256"; final int nextBytesSize = 32; final HashServiceConfig config1 = new HashServiceConfig("testHash", privateSalt, hashIterations, algo, nextBytesSize);// w w w . ja v a2 s . co m log.info("hashConfig: {}", config1); final HashService hashService1 = config1.getHashService(); final HashService hashService2 = config1.getHashService(); final HashRequest req1 = new HashRequest.Builder().setSource("password").build(); final Hash hash1 = hashService1.computeHash(req1); final HashRequest req2 = new HashRequest.Builder().setSource("password").setSalt(hash1.getSalt()).build(); final Hash hash2 = hashService2.computeHash(req2); Assert.assertEquals(hash2.toBase64(), hash1.toBase64()); }
From source file:test.com.azaptree.services.security.config.HashServiceConfigTest.java
License:Apache License
@Test public void testHashService2() { final HashServiceConfig config1 = new HashServiceConfig("testHash"); log.info("hashConfig: {}", config1); final HashService hashService1 = config1.getHashService(); final HashService hashService2 = config1.getHashService(); final HashRequest req1 = new HashRequest.Builder().setSource("password").build(); final Hash hash1 = hashService1.computeHash(req1); final HashRequest req2 = new HashRequest.Builder().setSource("password").setSalt(hash1.getSalt()).build(); final Hash hash2 = hashService2.computeHash(req2); Assert.assertEquals(hash2.toBase64(), hash1.toBase64()); }
From source file:test.com.azaptree.services.security.dao.HashServiceConfigurationDAOTest.java
License:Apache License
@Transactional @Test//from w ww .j a v a2 s .com public void test_create() { final HashServiceConfig config = new HashServiceConfig("test_create" + UUID.randomUUID()); final HashServiceConfiguration savedConfig = dao.create(config); final HashServiceConfiguration config2 = dao.findById(savedConfig.getEntityId()); Assert.assertNotNull(config2); Assert.assertEquals(config2, savedConfig); final HashService hashService = config2.getHashService(); final HashRequest hashRequest = new HashRequest.Builder().setSource("password").build(); final Hash hash = hashService.computeHash(hashRequest); final Subject subject = subjectDao.create(new SubjectImpl(Status.ACTIVATED)); final HashedCredential cred = hashedCredentialDAO.create(new HashedCredentialImpl(subject.getEntityId(), "password", savedConfig.getEntityId(), hash.getBytes(), hash.getAlgorithmName(), hash.getIterations(), hash.getSalt().getBytes(), null)); final HashRequest hashRequest2 = new HashRequest.Builder().setSource("password") .setAlgorithmName(cred.getHashAlgorithm()).setIterations(cred.getHashIterations()) .setSalt(ByteSource.Util.bytes(cred.getSalt())).build(); final Hash hash2 = hashService.computeHash(hashRequest2); Assert.assertTrue(Arrays.equals(hash2.getBytes(), cred.getHash())); }