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

The following examples show how to use org.apache.shiro.crypto.hash.Hash. 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: 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 #2
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 #3
Source File: MySimpleHash.java    From cms with Apache License 2.0 5 votes vote down vote up
public boolean equals(Object o) {
    if (o instanceof Hash) {
        Hash other = (Hash)o;
        return MessageDigest.isEqual(this.getBytes(), other.getBytes());
    } else {
        return false;
    }
}
 
Example #4
Source File: DefaultSecurityPasswordServiceTest.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Test
public void testHash() {
  String password = "testpassword";
  Hash hash = underTest.hashPassword(password);

  assertThat(underTest.passwordsMatch(password, hash), is(true));
}
 
Example #5
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 #6
Source File: SHA512.java    From JavaSecurity with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example #7
Source File: User.java    From niubi-job with Apache License 2.0 4 votes vote down vote up
@Transient
@Override
public ByteSource getCredentialsSalt() {
    return Hash.Util.bytes(passwordSalt);
}
 
Example #8
Source File: HashHelper.java    From niubi-job with Apache License 2.0 4 votes vote down vote up
public static String getHashedPassword(String password, String salt) {
    Hash hash = new SimpleHash(hashAlgorithm, password, salt);
    return hash.toHex();
}
 
Example #9
Source File: DatabaseRealm.java    From java-platform with Apache License 2.0 4 votes vote down vote up
public Hash hashProvidedCredentials(Object credentials, Object salt) {
	return ((RetryLimitMd5CredentialsMatcher) getCredentialsMatcher()).hashProvidedCredentials(credentials, salt);
}
 
Example #10
Source File: RetryLimitMd5CredentialsMatcher.java    From java-platform with Apache License 2.0 4 votes vote down vote up
public Hash hashProvidedCredentials(Object credentials, Object salt) {
	return super.hashProvidedCredentials(credentials, salt, getHashIterations());
}
 
Example #11
Source File: DefaultSecurityPasswordService.java    From nexus-public with Eclipse Public License 1.0 4 votes vote down vote up
@Override
public Hash hashPassword(final Object plaintext) {
  return passwordService.hashPassword(plaintext);
}
 
Example #12
Source File: DefaultSecurityPasswordService.java    From nexus-public with Eclipse Public License 1.0 4 votes vote down vote up
@Override
public boolean passwordsMatch(final Object plaintext, final Hash savedPasswordHash) {
  return passwordService.passwordsMatch(plaintext, savedPasswordHash);
}
 
Example #13
Source File: SimpleHashTest.java    From nano-framework with Apache License 2.0 4 votes vote down vote up
@Test
public void test0() throws UnsupportedEncodingException {
	/** 加密类型(不区分大小写)、密码、私匙、加密次数 */
	Hash hash = new SimpleHash("MD5", "123456", "Nano Framework Extension Shiro Salt for user: [admin]", 2);
	LOG.debug(hash.toString());
}
 
Example #14
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());
}
 
Example #15
Source File: AbstractCredentialsSecurerSupport.java    From super-cloudops with Apache License 2.0 votes vote down vote up
Hash hashing(String algorithm, ByteSource source, ByteSource salt, int hashIterations);