package net.yasme.android.encryption;

import android.util.Log;

import net.yasme.android.entities.User;

import org.codehaus.jackson.map.deser.ValueInstantiators;

import java.security.MessageDigest;

/**
 * Created by Marco Eberl on 27.07.2014.
 */
public class PasswordEncryption{

    private static final String SALT = "Y45M3";

    private User user;

    public PasswordEncryption(User user) {
        this.user = user;
    }

    //delay Brute-Force-Attacks
    //protect against "using same password for several services"
    public String getHashedPassword(){
        return SHA512(salt(SALT,user.getPw()));
    }

    public String getHashedPasswordEmailSalted(){
        return SHA512(salt(user.getEmail(),user.getPw()));
    }

    public String storeHashedPassword(){
        String secure = getHashedPassword();
        user.setPw(secure);
        return secure;
    }

    public String storeHashedPasswordEmailSalted(){
        String secure = getHashedPasswordEmailSalted();
        user.setPw(secure);
        return secure;
    }

    private String SHA512(String password){
       try {
           MessageDigest md;
           md = MessageDigest.getInstance("SHA-512");
           byte[] hash = new byte[40];
           md.update(password.getBytes("UTF-8"));
           hash = md.digest();
           return hash.toString();
           //return base64Encode(hash);
       }
       catch (Exception e){
           Log.d(this.getClass().getSimpleName(), "[???] Hashing Password failed");
           Log.d(this.getClass().getSimpleName(), "[???] Error: " + e.getMessage());
       }
       return null;
    }

    private String salt(String salt, String password){
        String passwordSalted = salt+password;
        return passwordSalted;
    }
}

@@ -342,18 +342,16 @@
 45M3%22;%0D%0A
-%0D%0A
     priv
@@ -372,88 +372,8 @@
 %0D%0A%0D%0A
-    public PasswordEncryption(User user) %7B%0D%0A        this.user = user;%0D%0A    %7D%0D%0A%0D%0A
     
@@ -482,203 +482,65 @@
 lic 
-String getHashedPassword()%7B%0D%0A        return SHA512(salt(SALT,user.getPw()));%0D%0A    %7D%0D%0A%0D%0A    public String getHashedPasswordEmailSalted()%7B%0D%0A        return SHA512(salt(user.getEmail(),user.getPw()))
+PasswordEncryption(User user) %7B%0D%0A        this.user = user
 ;%0D%0A 
@@ -558,34 +558,27 @@
  public 
-String storeHashed
+User secure
 Password
@@ -601,38 +601,38 @@
 ing secure = get
-Hashed
+Secure
 Password();%0D%0A   
@@ -624,16 +624,20 @@
 assword(
+SALT
 );%0D%0A    
@@ -668,38 +668,36 @@
 %0A        return 
+u
 se
-cure
+r
 ;%0D%0A    %7D%0D%0A%0D%0A    
@@ -707,26 +707,19 @@
 lic 
-String storeHashed
+User secure
 Pass
@@ -769,22 +769,22 @@
  get
-Hashed
+Secure
 Password
 Emai
@@ -779,28 +779,29 @@
 Password
-EmailSalted(
+(user.getPw()
 );%0D%0A    
@@ -840,22 +840,141 @@
  return 
+u
 se
-cure
+r;%0D%0A    %7D%0D%0A%0D%0A    public String getSecurePassword(String saltString)%7B%0D%0A        return SHA512(salt(saltString,user.getPw()))
 ;%0D%0A    %7D

optimize PasswortEncryption