List of usage examples for org.apache.shiro.crypto.hash Sha512Hash Sha512Hash
public Sha512Hash(Object source, Object salt, int hashIterations)
From source file:CryptoTest.java
License:Apache License
@Test public void test_hashingService() { log.info("*** test_hashingService ***"); final DefaultHashService hashService = new DefaultHashService(); final SecureRandomNumberGenerator secureRandomNumberGenerator = new SecureRandomNumberGenerator(); secureRandomNumberGenerator.setDefaultNextBytesSize(64); final ByteSource privateSalt = secureRandomNumberGenerator.nextBytes(); final ByteSource publicSalt = secureRandomNumberGenerator.nextBytes(); log.info("privateSalt .length = {}", privateSalt.getBytes().length); hashService.setHashAlgorithmName("SHA-512"); hashService.setHashIterations(1024 * 64); hashService.setPrivateSalt(privateSalt); hashService.setRandomNumberGenerator(secureRandomNumberGenerator); hashService.setGeneratePublicSalt(true); final HashRequest hashRequest = new HashRequest.Builder().setSource("password").setSalt(publicSalt).build(); final Hash hash = hashService.computeHash(hashRequest); log.info("hash.salt : {}", hash.getSalt()); log.info("publicSalt : {}", publicSalt); log.info("hash Base64 : {}", hash.toBase64()); final String hash1 = hashService.computeHash(hashRequest).toBase64(); final String hash2 = hashService.computeHash(hashRequest).toBase64(); log.info("hash1 Base64 : {}", hash1); log.info("hash2 Base64 : {}", hash2); Assert.assertEquals(hash1, hash2);/*from ww w .ja va 2s . c o m*/ Sha512Hash encodedPassword = new Sha512Hash("password", publicSalt, 1024 * 64); Sha512Hash encodedPassword2 = new Sha512Hash(encodedPassword.getBytes(), privateSalt, 1024 * 64); log.info("encodedPassword Base64 : {}", encodedPassword.toBase64()); log.info("encodedPassword2 Base64 : {}", encodedPassword2.toBase64()); Sha512Hash encodedPassword3 = new Sha512Hash("password", publicSalt, 1024 * 64); Sha512Hash encodedPassword4 = new Sha512Hash(encodedPassword3.getBytes(), privateSalt, 1024 * 64); log.info("encodedPassword3 Base64 : {}", encodedPassword3.toBase64()); log.info("encodedPassword4 Base64 : {}", encodedPassword4.toBase64()); Assert.assertEquals(encodedPassword2, encodedPassword4); }
From source file:CryptoTest.java
License:Apache License
@Test public void test_hashingService_usingRandomSalts() { log.info("*** test_hashingService_usingRandomSalts ***"); final DefaultHashService hashService = new DefaultHashService(); final SecureRandomNumberGenerator secureRandomNumberGenerator = new SecureRandomNumberGenerator(); secureRandomNumberGenerator.setDefaultNextBytesSize(64); final ByteSource privateSalt = secureRandomNumberGenerator.nextBytes(); hashService.setHashAlgorithmName("SHA-512"); hashService.setHashIterations(1024 * 128); hashService.setPrivateSalt(privateSalt); hashService.setRandomNumberGenerator(secureRandomNumberGenerator); hashService.setGeneratePublicSalt(true); final HashRequest hashRequest = new HashRequest.Builder().setSource("password").build(); final StopWatch stopWatch = new StopWatch(); stopWatch.start();//from w w w. j a va2s. c o m final Hash hash = hashService.computeHash(hashRequest); stopWatch.stop(); final byte[] hashBytes = hash.getBytes(); log.info("hashBytes length = {}", hashBytes.length); log.info("hash Base64 length = {}", hash.toBase64().length()); log.info("hash time: {}", stopWatch.getTime()); log.info("hash.salt : {}", hash.getSalt()); final ByteSource salt = hash.getSalt(); log.info("salt : {}", salt); log.info("hash Base64 : {}", hash.toBase64()); final String hash1 = hashService .computeHash(new HashRequest.Builder().setSource("password").setSalt(salt).build()).toBase64(); final String hash2 = hashService .computeHash(new HashRequest.Builder().setSource("password").setSalt(salt).build()).toBase64(); log.info("hash1 Base64 : {}", hash1); log.info("hash2 Base64 : {}", hash2); Assert.assertEquals(hash1, hash2); Sha512Hash encodedPassword = new Sha512Hash("password", salt, 1024 * 64); Sha512Hash encodedPassword2 = new Sha512Hash(encodedPassword.getBytes(), privateSalt, 1024 * 64); log.info("encodedPassword Base64 : {}", encodedPassword.toBase64()); log.info("encodedPassword2 Base64 : {}", encodedPassword2.toBase64()); Sha512Hash encodedPassword3 = new Sha512Hash("password", salt, 1024 * 64); Sha512Hash encodedPassword4 = new Sha512Hash(encodedPassword3.getBytes(), privateSalt, 1024 * 64); log.info("encodedPassword3 Base64 : {}", encodedPassword3.toBase64()); log.info("encodedPassword4 Base64 : {}", encodedPassword4.toBase64()); Assert.assertEquals(encodedPassword2, encodedPassword4); hashService.setHashIterations(1024 * 127); }
From source file:com.app.util.UserUtil.java
License:Open Source License
public static String generateUnsubscribeToken(String customerId) { RandomNumberGenerator rng = new SecureRandomNumberGenerator(); Object salt = rng.nextBytes(); return new Sha512Hash(customerId, salt, 1024).toBase64(); }
From source file:com.app.util.UserUtil.java
License:Open Source License
private static List<String> _generatePasswordAndSalt(String plainTextPassword) { RandomNumberGenerator rng = new SecureRandomNumberGenerator(); Object salt = rng.nextBytes(); String hashedPasswordBase64 = new Sha512Hash(plainTextPassword, salt, 1024).toBase64(); List<String> passwordAndSalt = new ArrayList<>(); passwordAndSalt.add(hashedPasswordBase64); passwordAndSalt.add(salt.toString()); return passwordAndSalt; }
From source file:com.enioka.jqm.api.Dto2Jpa.java
License:Open Source License
private static RUser setJpa(EntityManager em, RUserDto dto) { RUser jpa = null;// www . ja v a 2 s. c o m if (dto.getId() == null) { jpa = new RUser(); } else { jpa = em.find(RUser.class, dto.getId()); } jpa.setEmail(dto.getEmail()); jpa.setExpirationDate(dto.getExpirationDate()); jpa.setFreeText(dto.getFreeText()); jpa.setLocked(dto.getLocked()); jpa.setLogin(dto.getLogin()); jpa = em.merge(jpa); RRole r = null; for (RRole ex : jpa.getRoles()) { ex.getUsers().remove(jpa); // jpa.getRoles().remove(ex); } for (Integer rid : dto.getRoles()) { r = em.find(RRole.class, rid); if (r == null) { throw new ErrorDto("Trying to associate an account with a non-existing role", "", 4, Status.BAD_REQUEST); } jpa.getRoles().add(r); r.getUsers().add(jpa); } if (dto.getNewPassword() != null && !dto.getNewPassword().isEmpty()) { ByteSource salt = new SecureRandomNumberGenerator().nextBytes(); jpa.setPassword(new Sha512Hash(dto.getNewPassword(), salt, 100000).toHex()); jpa.setHashSalt(salt.toHex()); } // Done return jpa; }
From source file:com.enioka.jqm.api.SimpleApiSecurity.java
License:Open Source License
/** * Will create (or recreate) if necessary the temporary login data.<br> * Will create its own transaction - therefore the given em must not have any active transaction. *//* w w w . ja va2 s . c o m*/ static Duet getId(EntityManager em) { if (logindata == null && useAuth == null) { try { GlobalParameter gp = em .createQuery("SELECT gp from GlobalParameter gp WHERE gp.key = 'enableWsApiAuth'", GlobalParameter.class) .getSingleResult(); useAuth = Boolean.parseBoolean(gp.getValue()); } catch (NoResultException e) { useAuth = true; } if (!useAuth) { jqmlogger.debug("The client API will not use any authentication to download files"); logindata = new Duet(); logindata.pass = null; logindata.usr = null; } else { jqmlogger.debug("The client API will use authentication to download files"); } } if (!useAuth) { return logindata; } if (user == null || user.getExpirationDate().before(Calendar.getInstance())) { synchronized (lock) { if (user == null || user.getExpirationDate().before(Calendar.getInstance())) { jqmlogger.debug( "The client API will create an internal secret to access the simple API for file downloading"); em.getTransaction().begin(); // Create new user = new RUser(); secret = UUID.randomUUID().toString(); Calendar expiration = Calendar.getInstance(); expiration.add(Calendar.DAY_OF_YEAR, 1); user.setExpirationDate(expiration); user.setInternal(true); user.setLocked(false); user.setLogin(UUID.randomUUID().toString()); ByteSource salt = new SecureRandomNumberGenerator().nextBytes(); user.setPassword(new Sha512Hash(secret, salt, 100000).toHex()); user.setHashSalt(salt.toHex()); em.persist(user); logindata = new Duet(); logindata.pass = secret; logindata.usr = user.getLogin(); RRole r = em.createQuery("SELECT r from RRole r where r.name = 'administrator'", RRole.class) .getSingleResult(); r.getUsers().add(user); // Purge all old internal accounts for (RUser ru : em .createQuery("SELECT u FROM RUser u WHERE u.internal = true AND u.expirationDate < :n", RUser.class) .setParameter("n", Calendar.getInstance()).getResultList()) { // Not using DELETE query but a remove in a loop because two-ways M2M relationship are stupid in JPA. for (RRole rr : ru.getRoles()) { rr.getUsers().remove(ru); } ru.getRoles().clear(); em.remove(ru); } em.getTransaction().commit(); } } } return logindata; }
From source file:com.enioka.jqm.test.helpers.TestHelpers.java
License:Open Source License
public static void encodePassword(RUser user) { ByteSource salt = new SecureRandomNumberGenerator().nextBytes(); user.setPassword(new Sha512Hash(user.getPassword(), salt, 100000).toHex()); user.setHashSalt(salt.toHex());//from www . ja va 2s .c o m }
From source file:com.enioka.jqm.tools.Helpers.java
License:Open Source License
static void encodePassword(RUser user) { ByteSource salt = new SecureRandomNumberGenerator().nextBytes(); user.setPassword(new Sha512Hash(user.getPassword(), salt, 100000).toHex()); user.setHashSalt(salt.toHex());//w w w. j a v a 2s .c o m }
From source file:com.h57.sample.service.IdentityService.java
License:BSD License
public String encodePassphrase(String rawPassphrase, String salt) { return new Sha512Hash(rawPassphrase, getCombinedSalt(salt), getIterations()).toBase64(); }
From source file:de.iai.ilcd.security.IlcdSecurityRealm.java
License:Open Source License
/** * Get the encrypted password for plain text and hash salt * //from w ww . jav a2 s . c om * @param plainPassword * plain text password * @param hashSalt * salt for the hash * @return result hex string */ public static String getEncryptedPassword(String plainPassword, String hashSalt) { return new Sha512Hash(plainPassword, hashSalt, 5).toHex(); }