Tuesday, May 29, 2012

Java program to encrypt password for LDAP entry


In the LDAP server, let's say oracle identity directory (OID) or OpenLDAP, we are allowed to configure the encoding algorithm, SHA,MD5,SHAA,etc, to hash the user password.

if the password is 'abcd1234', the password in ldap will be stored as '{SHA}fOA1nxKFfyqQx95GX0CpXwHLXak=' in userPassword attribute.

This is the java program to encrypt the password with SHA to simulate how LDAP to encrypt user password.

package test.ldap.sha;

import java.security.MessageDigest;

import sun.misc.BASE64Encoder;

public class PasswordEncryptor {

public static void main(String[] args) throws Exception {
String pwdPlainText = "abcd1234";

MessageDigest md = MessageDigest.getInstance("SHA");
md.update(pwdPlainText.getBytes());
byte raw[] = md.digest();

BASE64Encoder base64 = new BASE64Encoder();
String result = "{SHA}" + base64.encode(raw);
System.out.println("userpassword in LDAP:" + result);

String pwdGeneratedByLdap = "{SHA}fOA1nxKFfyqQx95GX0CpXwHLXak=";
System.out.println(pwdGeneratedByLdap.equals(result));

}
}

This is output:
userpassword in LDAP:{SHA}fOA1nxKFfyqQx95GX0CpXwHLXak=
true

Monday, May 14, 2012

Design Pattern Study Notes 5 - Builder


























1) interface to define build methods.
============== Builder.java ================

package test.pattern.builder;
/**
 *  interface
 *
 */
public interface Builder {
public abstract void doPart1();
public abstract void doPart2(String para);
public abstract void doPart3(int para);
public abstract Product getProduct();
}

2) to create product object
============== Director.java =============
package test.pattern.builder;

public class Director {

private Builder builder;

public Director(Builder builder) {
this.builder = builder;
}

public Product construct(String para, int para2) {
builder.doPart1();
builder.doPart2(para);
builder.doPart3(para2);
Product product = builder.getProduct();
return product;
}
}

3) product class
============== Product.java =============
package test.pattern.builder;

public class Product {

private String component1;
private int component2;
        // getter and setter methods
}

4) concrete builder: 
a) implement the methods defined in above interface to build the product step by step
b) return the product instance

============== ConcreteBuilder1.java ==================
package test.pattern.builder;

public class ConcreteBuilder1 implements Builder {
Product prod = new Product();

public void doPart1() {
System.out.println("execute builder1:part1");
}

public void doPart2(String para) {
System.out.println("execute builder1:part2:" + para);
prod.setComponent1(para);
}

public void doPart3(int para) {
System.out.println("execute builder1:part3:" + para);
prod.setComponent2(para);
}

public Product getProduct() {
return prod;
}

}

=============== ConcreteBuilder2.java ==============
package test.pattern.builder;

public class ConcreteBuilder2 implements Builder {
Product prod = new Product();

public void doPart1() {
System.out.println("execute builder2:part1");
}

public void doPart2(String para) {
System.out.println("execute builder2:part2:" + para);
prod.setComponent1(para);
}

public void doPart3(int para) {
System.out.println("execute builder2:part3:" + para);
prod.setComponent2(para);
}

public Product getProduct() {
return prod;
}
}

5) test class
============== Client.java =================
package test.pattern.builder;

public class Client {

public static void main(String[] args) {
Director director = new Director(new ConcreteBuilder1());
Product prod = director.construct("do something", 4);
System.out.println("got: " + prod.getComponent1());
System.out.println("got: " + prod.getComponent2());

director = new Director(new ConcreteBuilder2());
prod = director.construct("do something else", 5);
System.out.println("got: " + prod.getComponent1());
System.out.println("got: " + prod.getComponent2());
}

}

This is the output:
execute builder1:part1
execute builder1:part2:do something
execute builder1:part3:4
got: do something
got: 4
execute builder2:part1
execute builder2:part2:do something else
execute builder2:part3:5
got: do something else
got: 5


Thursday, May 10, 2012

Design Pattern Study Notes 4 - Singleton













Problem:
only one instance in the system.

Solution:
Singleton pattern

1) singleton class
===================== Singleton.java ====================
package test.pattern.singleton;

public class Singleton {

private static Singleton instance = null;

/*
* the constructor must be private
*/
private Singleton() {
System.out.println("private constructor");
}

/*
* just in case running in multi-thread env,need to add synchronized
*/
public static synchronized Singleton getInstance() {
if (instance == null) {
instance = new Singleton();
}

return instance;
}
}

2) test class
==================== Client.java ====================
package test.pattern.singleton;

public class Client {

public static void main(String[] args) {
Singleton s1 = Singleton.getInstance();
Singleton s2 = Singleton.getInstance();
System.out.println(s1==s2);
}
}

This is the output:
private constructor
true


Design Pattern Study Notes 3 - Template Method




















Problem:
need to define the process or logic on the top class, and implement the detail logic at lower classes

Solution:
Template method pattern

1) top abstract class
=============== Template.java ================

package test.pattern.template;

public abstract class Template {

public void process() {
beforeOperation();
operation();
afterOperation();
}

private void beforeOperation() {
System.out.println("before action.");
}

private void afterOperation() {
System.out.println("after action.");
}

public abstract void operation();
}

2) sub class to implement the abstract method
================= TemplateImpl.java ==============

package test.pattern.template;

public class TemplateImpl extends Template {

public void operation() {
System.out.println("do something.");

}
}

3) the other sub class to implement the abstract method

================== TemplateImpl2.java =================
package test.pattern.template;

public class TemplateImpl2 extends Template {

public void operation() {
System.out.println("do something else.");
}
}


4) test class
=================== Client.java ==================
package test.pattern.template;

public class Client {

public static void main(String[] args) {
Template t1 = new TemplateImpl();
t1.process();

Template t2 = new TemplateImpl2();
t2.process();

}

}

This is the output:

before action.
do something.
after action.
before action.
do something else.
after action.



Design Pattern Study Notes 2 - Adapter














Problem:
If want to reuse the existing class, but its interface does not meet the new requirement.

Solution:
Adapter pattern.

1) This is new interface we want to implement.
======================== Target.java ====================

package test.pattern.adapter;

public interface Target {

public void methodA();
public void methodB();
}

2) This is the existing class we have.
======================= Adaptee.java ================

package test.pattern.adapter;

public class Adaptee {

public void methodA() {
System.out.println("it is method A.");
}
}

3) This is an adapter class to implement above new interface Target.java
=================== Adapter.java =====================
package test.pattern.adapter;

public class Adapter extends Adaptee implements Target {

public void methodB() {
System.out.println("it is method B.");
}

}

4) This is another adapter to implement above new interface Target.java
=================== Adapter2.java ====================
package test.pattern.adapter;

public class Adapter2 implements Target {

private Adaptee adaptee;

public Adapter2(Adaptee adaptee) {
this.adaptee = adaptee;
}

public void methodA() {
this.adaptee.methodA();
}

public void methodB() {
System.out.println("this is method B.");
}

}

5) test class
============== Client.java ======================
package test.pattern.adapter;

public class Client {

public static void main(String[] args) {
Target adapter = new Adapter();
adapter.methodA();
adapter.methodB();

Adaptee adaptee = new Adaptee();
Target adapter2 = new Adapter2(adaptee);
adapter2.methodA();
adapter2.methodB();
}

}

This is output:
it is method A.
it is method B.
it is method A.
this is method B.



Wednesday, May 9, 2012

Design Pattern Study Notes 1 - Iterator


Problem:
A team has one or multiple members, now print the member information one by one.

Solution:
iterator pattern.

1) define an iterator interface.
====================== MyIterator.java ==================
package test.pattern.iterator;

/**
 *  This is the customized iterator interface.
 *
 */
public interface MyIterator {
public boolean hasNext();

public Object next();
}

2) define aggregate interface

====================== Aggregate.java ==================

package test.pattern.iterator;

public interface Aggregate {
MyIterator iterator();
}

3) member class to encapsulate member's information
====================== Member.java =====================
package test.pattern.iterator;

public class Member {
private String name;
private int age;

        // getter and setter methods
}

4) team class to implement Aggregate class
==================== Team.java =======================
package test.pattern.iterator;

public class Team implements Aggregate {

private Member[] members;
private int last;

public Team(int maxSize) {
members = new Member[maxSize];
this.last = 0;
}
public void addMember(Member member){
this.members[last]=member;
last++;
}

public int getLength() {
return members.length;
}

public Member getMemberAt(int index) {
return members[index];
}

public MyIterator iterator() {
return new TeamIterator(this);
}

}

5) TeamIterator class to implement MyIterator interface
=================== TeamIterator.java ================

package test.pattern.iterator;

public class TeamIterator implements MyIterator {

private Team team;
private int index;

public TeamIterator(Team team) {
this.team = team;
this.index = 0;
}

public boolean hasNext() {

if (index >= 0 && index < team.getLength())
return true;
return false;
}

public Object next() {
Member member = team.getMemberAt(index);
index++;
return member;
}

}

6) test class
==================== Client.java ===================
package test.pattern.iterator;

public class Client {

public static void main(String[] args) {
Team team = new Team(5);
team.addMember(new Member("member A", 10));
team.addMember(new Member("member B", 11));
team.addMember(new Member("member C", 12));
team.addMember(new Member("member D", 13));
team.addMember(new Member("member E", 15));

MyIterator it = team.iterator();
while (it.hasNext()) {
Member mem = (Member) it.next();
System.out.println(mem.getName() + "'s age is " + mem.getAge());
}

}

}

This is the output:
member A's age is 10
member B's age is 11
member C's age is 12
member D's age is 13
member E's age is 15

------------
After jdk1.5,  for-loop is another solution.