List of usage examples for org.apache.shiro.crypto.hash Hash getSalt
ByteSource getSalt();
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);// w w w . j a v a2 s . c om 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 ww . j a v a2s. c om 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:CryptoTest.java
License:Apache License
@Test public void test_secureRandomNumberGenerator_nextBytesSize() { log.info("*** test_secureRandomNumberGenerator_nextBytesSize ***"); final DefaultHashService hashService = new DefaultHashService(); final SecureRandomNumberGenerator secureRandomNumberGenerator = new SecureRandomNumberGenerator(); secureRandomNumberGenerator.setDefaultNextBytesSize(8); final ByteSource privateSalt = secureRandomNumberGenerator.nextBytes(); log.info("privateSalt = {}", privateSalt); log.info("privateSalt byte length = {}", privateSalt.getBytes().length); 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 Hash hash = hashService.computeHash(hashRequest); final DefaultHashService hashService2 = new DefaultHashService(); final SecureRandomNumberGenerator secureRandomNumberGenerator2 = new SecureRandomNumberGenerator(); secureRandomNumberGenerator2.setDefaultNextBytesSize(16); hashService2.setHashAlgorithmName("SHA-512"); hashService2.setHashIterations(1024 * 128); hashService2.setPrivateSalt(privateSalt); hashService2.setRandomNumberGenerator(secureRandomNumberGenerator2); hashService2.setGeneratePublicSalt(true); final HashRequest hashRequest2 = new HashRequest.Builder().setSource("password").setSalt(hash.getSalt()) .build();/* ww w. j av a 2 s . c om*/ final Hash hash2 = hashService.computeHash(hashRequest2); log.info("hash = {}", hash.toBase64()); log.info("hash2 = {}", hash2.toBase64()); Assert.assertEquals(hash2.toBase64(), hash.toBase64()); }
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
public static void main(String[] args) { String password = "SHA-512 hash sample text"; Hash hash = calculateHash(password); boolean correct = verifyPassword(hash.getBytes(), hash.getSalt(), password); log.info("Entered password is correct: {}", correct); }
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()); return hash;/*w ww . j a v a 2s . c om*/ }
From source file:de.dominikschadow.javasecurity.hash.SHA512HashSample.java
License:Apache License
public static void main(String[] args) { SHA512HashSample hs = new SHA512HashSample(); String password = "SHA-512 hash sample text"; Hash hash = hs.calculateHash(password); boolean correct = hs.verifyPassword(hash.getBytes(), hash.getSalt(), password); logger.info("Entered password is correct: {}", correct); }
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 w w.jav a 2s . c o m }
From source file:org.apache.hadoop.gateway.shirorealm.KnoxLdapRealm.java
License:Apache License
@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()); }