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;
import org.jboss.seam.util.Hex;

import java.security.MessageDigest;
import java.security.SecureRandom;

@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();
        }

        public void createApiKey(Account account){
                String username = account.getUsername();
                String apikey = encryptUserName(username);
                account.setApiKey(apikey);
        }

        public static String encryptUserName(String username){
                try {
                	byte[] salt = new byte[16];
                	SecureRandom.getInstance("SHA1PRNG").nextBytes(salt);
        		MessageDigest md5 = MessageDigest.getInstance("MD5");
        		byte[] name = username.getBytes("UTF-8");
                
                	// add salt
                	byte[] salted = new byte[name.length + salt.length];
                	System.arraycopy(name, 0, salted, 0, name.length);
                	System.arraycopy(salt, 0, salted, name.length, salt.length);
                
                	// generate md5 digest 
                	md5.reset();
                	byte[] digest = md5.digest(salted);

                	// Appends salt bytes to md5 digest.
                	byte[] salteddigest = new byte[digest.length + salt.length];  
                	System.arraycopy(digest, 0, salteddigest, 0, digest.length);  
                	System.arraycopy(salt, 0, salteddigest, digest.length, salt.length);  
                
                	return new String(Hex.encodeHex(salteddigest));
                } catch (Exception exc) {
            		throw new RuntimeException(exc);
        	}

        }

}

@@ -895,24 +895,17 @@
 ();%0A%09%7D%0A%0A
-        
+%09
 public A
@@ -937,34 +937,21 @@
  apikey)
+ 
 %7B%0A
-                
+%09%09
 Session 
@@ -999,32 +999,18 @@
 gate();%0A
-                
+%09%09
 return (
@@ -1059,167 +1059,87 @@
 ass)
-%0A                       .add( Restrictions.naturalId()%0A                       .set(%22apiKey%22, apikey))%0A                     .uniqueResult();%0A        %7D%0A%0A        
+.add(%0A%09%09%09%09Restrictions.naturalId().set(%22apiKey%22, apikey)).uniqueResult();%0A%09%7D%0A%0A%09
 publ
@@ -1175,34 +1175,21 @@
 account)
+ 
 %7B%0A
-                
+%09%09
 String u
@@ -1225,83 +1225,58 @@
 ();%0A
-                String apikey = encryptUserName(username);%0A                
+%09%09String apikey = createSaltedApiKey(username);%0A%09%09
 acco
@@ -1302,27 +1302,13 @@
 y);%0A
-        %7D%0A%0A        
+%09%7D%0A%0A%09
 publ
@@ -1328,23 +1328,26 @@
 ing 
-encryptUserName
+createSaltedApiKey
 (Str
@@ -1363,48 +1363,21 @@
 ame)
+ 
 %7B%0A
-                try %7B%0A                
+%09%09try %7B%0A%09%09
 %09byt
@@ -1401,32 +1401,18 @@
 te%5B16%5D;%0A
-                
+%09%09
 %09SecureR
@@ -1458,24 +1458,17 @@
 (salt);%0A
-        
+%09
 %09%09Messag
@@ -1515,24 +1515,17 @@
 %22MD5%22);%0A
-        
+%09
 %09%09byte%5B%5D
@@ -1564,70 +1564,26 @@
 %22);%0A
-                %0A                %09// add salt%0A                
+%0A%09%09%09// add salt%0A%09%09
 %09byt
@@ -1632,32 +1632,18 @@
 ength%5D;%0A
-                
+%09%09
 %09System.
@@ -1686,32 +1686,18 @@
 ength);%0A
-                
+%09%09
 %09System.
@@ -1754,485 +1754,93 @@
 h);%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                
+%0A%09%09%09// generate md5 digest%0A%09%09%09md5.reset();%0A%09%09%09byte%5B%5D digest = md5.digest(salted);%0A%0A%09%09
 %09ret
@@ -1868,22 +1868,16 @@
 codeHex(
-salted
 digest))
@@ -1882,24 +1882,14 @@
 ));%0A
-                
+%09%09%09%0A%09%09
 %7D ca
@@ -1914,20 +1914,9 @@
 ) %7B%0A
-            
+%09
 %09%09th
@@ -1950,29 +1950,14 @@
 c);%0A
-        %09%7D%0A%0A        %7D%0A
+%09%09%7D%0A%0A%09%7D
 %0A%7D%0A

create a valid md5 key