How to specify hash algorithm when updating LDAP via Java?

Posted by JuanZe on Stack Overflow See other posts from Stack Overflow or by JuanZe
Published on 2010-04-14T16:28:27Z Indexed on 2010/04/14 16:33 UTC
Read the original article Hit count: 175

Filed under:
|
|
|

Is there a way to specify the hash algorithm (MD5, SHA1, etc.) to use for storing the passwords when you update an Open LDAP directory using Java APIs with code like this:

private void resetPassword(String principal, String newPassword) throws NamingException {
InitialDirContext ctxAdmin = null;
    Hashtable<String, String> ctxData = new Hashtable<String, String>();
    ctxData.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    ctxData.put(Context.PROVIDER_URL, "ldap://myserver:389");
    ctxData.put(Context.SECURITY_AUTHENTICATION, "simple");
    ctxData.put(Context.SECURITY_PRINCIPAL, "admin_dn");
    ctxData.put(Context.SECURITY_CREDENTIALS, "admin_passwd");
    InitialDirContext ctxAdmin = new InitialDirContext(ctxData);
    if (newPassword == null || newPassword.equals("")) {
        String msg = "Password can't be null";
        throw new NamingException(msg);
    } else {
        if (principal == null || principal.equals("")) {
            String msg = "Principal can't be null";
            throw new NamingException(msg);
        } else {
        if (ctxAdmin == null) {
            String errCtx = "Can't get LDAP context";
            throw new NamingException(errCtx);
            }
        }
    }   
    BasicAttribute attr = new BasicAttribute("userpassword", newPassword);
    ModificationItem modItem = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, attr);
    ModificationItem[] items = new ModificationItem[1];
    items[0] = modItem;
    ctxAdmin.modifyAttributes("cn=" + principal + ",ou=Users,dc=com", items);
}

© Stack Overflow or respective owner

Related posts about open-ldap

Related posts about hashing