package org.fedorahosted.flies.core.dao;

import javax.persistence.EntityManager;

import org.fedorahosted.flies.core.model.Account;
import org.fedorahosted.flies.core.model.Project;
import org.fedorahosted.flies.core.model.ProjectTarget;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.jboss.seam.annotations.AutoCreate;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;

@Name("accountDAO")
@AutoCreate
public class AccountDAO {

	@In
	EntityManager entityManager;
	
	public Account getByUsername(String username){
		Session session = (Session) entityManager.getDelegate();
		return (Account) session.createCriteria(Account.class)
			.add( Restrictions.naturalId()
		        .set("username", username))
		    .uniqueResult();
	}

        public Account getByApiKey(String apikey){
                Session session = (Session) entityManager.getDelegate();
                return (Account) session.createCriteria(Account.class)
                       .add( Restrictions.naturalId()
                       .set("apiKey", apikey))
                     .uniqueResult();
        }
 
}

@@ -430,16 +430,120 @@
 ns.Name;
+%0Aimport org.jboss.seam.util.Hex;%0A%0Aimport java.security.MessageDigest;%0Aimport java.security.SecureRandom;
 %0A%0A@Name(
@@ -1239,12 +1239,1450 @@
       %7D%0A
- 
+%0A        public void createApiKey(Account account)%7B%0A                String username = account.getUsername();%0A                String apikey = encryptUserName(username);%0A                account.setApiKey(apikey);%0A        %7D%0A%0A        public static String encryptUserName(String username)%7B%0A                try %7B%0A                %09byte%5B%5D salt = new byte%5B16%5D;%0A                %09SecureRandom.getInstance(%22SHA1PRNG%22).nextBytes(salt);%0A%0A        %09%09MessageDigest md5 = MessageDigest.getInstance(%22MD5%22);%0A        %09%09byte%5B%5D name = username.getBytes(%22UTF-8%22);%0A                %0A                %09// add salt%0A                %09byte%5B%5D salted = new byte%5Bname.length + salt.length%5D;%0A                %09System.arraycopy(name, 0, salted, 0, name.length);%0A                %09System.arraycopy(salt, 0, salted, name.length, salt.length);%0A                %0A                %09// generate md5 digest %0A                %09md5.reset();%0A                %09byte%5B%5D digest = md5.digest(salted);%0A%0A                %09// Appends salt bytes to md5 digest.%0A                %09byte%5B%5D salteddigest = new byte%5Bdigest.length + salt.length%5D;  %0A                %09System.arraycopy(digest, 0, salteddigest, 0, digest.length);  %0A                %09System.arraycopy(salt, 0, salteddigest, digest.length, salt.length);  %0A                %0A                %09return new String(Hex.encodeHex(salteddigest));%0A                %7D catch (Exception exc) %7B%0A            %09%09throw new RuntimeException(exc);%0A        %09%7D%0A%0A        %7D%0A
 %0A%7D%0A

Add createApiKey method. Using "random salt"+username and md5 to generate api key.