Lucy
Lucy

Reputation: 65

How can I hash the password in my app

I want to hash the String password in my app ,I have searched for examples online but I don't understand how I would be able to implement in my case ,could someone please show me an example of how to correctly to implement the hash with my code . Thank you

  package ie.example.artur.adminapp;

    import android.os.AsyncTask;
    import android.os.Bundle;
    import android.support.design.widget.FloatingActionButton;
    import android.support.design.widget.Snackbar;
    import android.support.v7.app.AppCompatActivity;
    import android.support.v7.widget.Toolbar;
    import android.view.View;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.TextView;

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.Statement;

    public class MainActivity extends AppCompatActivity {


        EditText editTextName,editTextEmail,editTextPassword;
        TextView textView;
        private static final String DB_URL = "jdbc:mysql://10.3.2.51/socialmedia_website";
        private static final String USER = "zzz";
        private static final String PASS = "zzz";



        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_register);

            textView = (TextView) findViewById(R.id.textView);
            editTextName = (EditText) findViewById(R.id.editTextName);
            editTextEmail = (EditText) findViewById(R.id.editTextEmail);
            editTextPassword = (EditText) findViewById(R.id.editTextPassword);

            Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);

            FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
            fab.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                            .setAction("Action", null).show();
                }
            });
        }

        public void btnConn(View view) {
            Send objSend = new Send();
            objSend.execute("");


        }

        private class Send extends AsyncTask<String, String, String>

        {
            String msg = "";
            String name = editTextName.getText().toString();
            String email = editTextEmail.getText().toString();
            String password = editTextPassword.getText().toString();

            @Override
            protected void onPreExecute() {
                textView.setText("Please Wait Inserting Data");
            }

            @Override
            protected String doInBackground(String... strings) {
                try {
                    Class.forName("com.mysql.jdbc.Driver");
                    Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
                    if (conn == null) {
                        msg = "Connection goes wrong";
                    } else {
                        String query = "Insert INTO users (name,email,password) VALUES('" + name+"','"+email+"','"+password+"')";
                        Statement stmt = conn.createStatement();
                        stmt.executeUpdate(query);
                        msg = "Inserting Successful!!";

                    }

                    conn.close();

            }

            catch(
            Exception e
            )

            {
                msg = "Connection goes Wrong";
                e.printStackTrace();

            }

            return msg;


        }



    @Override
        protected void onPostExecute(String msg) {textView.setText(msg);}



        }

}

Upvotes: 0

Views: 1142

Answers (1)

Kapil G
Kapil G

Reputation: 4141

Ah ok. As per discussion in Comment. So in your class that takes the password as input you have to implement this method.

So what you have to do is implement the answer method in your class itself. So add below method in your class -

public String md5(String s) {
try {
    // Create MD5 Hash
    MessageDigest digest = java.security.MessageDigest.getInstance("MD5");
    digest.update(s.getBytes());
    byte messageDigest[] = digest.digest();

    // Create Hex String
    StringBuffer hexString = new StringBuffer();
    for (int i=0; i<messageDigest.length; i++)
        hexString.append(Integer.toHexString(0xFF & messageDigest[i]));
    return hexString.toString();

} catch (NoSuchAlgorithmException e) {
    e.printStackTrace();
}
return "";
}

Now in your Async task you are doing this -

 String password = editTextPassword.getText().toString();

Instead pass this value to your md5 method and get the hashed code as password.

String password = md5(editTextPassword.getText().toString());

Upvotes: 5

Related Questions