[Date Prev][Date Next] [Chronological] [Thread] [Top]

Sample java code for User authentication using SSHA hasing



Hi all.

I want to use LDAP authentication for my web application. using SSHA hashing
password stored in the LDAP database. I want sample java code for this?

Using following code, I was able get the password, but  don't know creating
SSHA hashing password and comparing with existing password,


Can some one help me on this?

Thanks
SR









import javax.naming.*;

import javax.naming.directory.*;

import java.util.Hashtable;

 public class LdapAuth {

public static void main(String[] args) {

// Set up environment for creating initial context

Hashtable env = new Hashtable(11);

env.put(

Context.INITIAL_CONTEXT_FACTORY,

"com.sun.jndi.ldap.LdapCtxFactory");

env.put(Context.PROVIDER_URL, "ldap://ldap.example.com:389";);

// Authenticate as S. User and password "mysecret"

env.put(Context.SECURITY_AUTHENTICATION, "simple");

env.put(Context.SECURITY_PRINCIPAL, "cn=Manager,dc=example,dc=com");

env.put(Context.SECURITY_CREDENTIALS, "secret");

/*

env.put(Context.SECURITY_PRINCIPAL,

"cn=sundaram,ou=People,dc=example,dc=com");

env.put(Context.SECURITY_CREDENTIALS, "abc123");

*/

try {

// Create initial context

DirContext ctx = new InitialDirContext(env);

// Perform the search

NamingEnumeration n1 =

ctx.search("ou=People,dc=example,dc=com", "(cn=su*)", null);

System.out.println("CN : " + n1.toString());

if (n1 == null) {

System.out.println("No item in the name list");

} else {

while (n1.hasMore()) {

//Object item = n1.next();

SearchResult item= (SearchResult) n1.next();

System.out.println("si :" + item.getName() );

String temp = item.getAttributes().toString();


System.out.println("att" +temp);

int s = temp.indexOf( "=userPassword:");

int e = temp.indexOf( "scriptpath" );

System.out.println( s + ":"+ e);


String pass= temp.substring( s +14, e);


System.out.println("pass :" + pass);

System.out.println(

"Item class is " + item.getClass().getName());

System.out.println(item);

}

}

// Close the context when we're done

ctx.close();

} catch (NamingException e) {

e.printStackTrace();

}

}

}