Example usage for org.apache.shiro.crypto.hash Sha1Hash Sha1Hash

List of usage examples for org.apache.shiro.crypto.hash Sha1Hash Sha1Hash

Introduction

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

Prototype

public Sha1Hash(Object source) 

Source Link

Usage

From source file:org.smallmind.nutsnbolts.shiro.realm.DefaultLdapRealm.java

License:Open Source License

@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {

    Hashtable<String, String> env;

    env = new Hashtable<String, String>();
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, "ldap://" + connectionDetails.getHost() + ":" + connectionDetails.getPort()
            + "/" + connectionDetails.getRootNamespace());
    env.put(Context.SECURITY_AUTHENTICATION, "simple");
    env.put(Context.SECURITY_PRINCIPAL, connectionDetails.getUserName());
    env.put(Context.SECURITY_CREDENTIALS, connectionDetails.getPassword());

    try {/* w  w w  .j  av a  2  s.co  m*/

        Attributes userAttributes;

        if ((userAttributes = ((DirContext) new InitialDirContext(env).lookup(searchPath))
                .getAttributes("uid=" + token.getPrincipal().toString())) != null) {

            Attribute passwordAttribute;

            if ((passwordAttribute = userAttributes.get("userPassword")) != null) {

                String hashedPasswordPlusAlgorithm;
                Hash sha1Hash;

                hashedPasswordPlusAlgorithm = new String((byte[]) passwordAttribute.get());
                sha1Hash = new Sha1Hash(new String((char[]) token.getCredentials()));
                if (hashedPasswordPlusAlgorithm.equals("{SHA}" + sha1Hash.toBase64())) {

                    return new SimpleAuthenticationInfo(token.getPrincipal(), sha1Hash.getBytes(), getName());
                }
            }
        }
    } catch (NamingException namingException) {
        throw new AuthenticationException(namingException);
    }

    return null;
}

From source file:org.sonatype.security.realms.url.URLRealm.java

License:Open Source License

private String getAuthenticationCacheKey(String username, String pass) {
    Sha1Hash h = new Sha1Hash(pass);
    return username + "-" + h.toString() + "-" + this.getClass().getSimpleName();
}