0

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



        }

}
Marc Delisle
  • 8,879
  • 3
  • 29
  • 29
Lucy
  • 65
  • 2
  • 10
  • You should use the database's PASSWORD function, and you should be using prepared statements. – user207421 Jul 24 '17 at 11:41
  • What kind of hashing you are looking at. And what have you tried? do you want MD5 or SHA1 hashing? – Kapil G Jul 24 '17 at 11:42
  • Your question is duplicate this link will be help you out. https://stackoverflow.com/questions/3934331/how-to-hash-a-string-in-android – Mohsin Khan Jul 24 '17 at 11:44
  • @kapsym I have looked at the general questions asked in this website like this https://stackoverflow.com/questions/3934331/how-to-hash-a-string-in-android and I want MD5 hashing in this case – Lucy Jul 24 '17 at 11:45
  • Your link itself has the answer so are you looking at something extra? – Kapil G Jul 24 '17 at 11:46
  • @MohsinKhan I have looked at that question and I don't understand how I can implement that tom my app,why duplicate straight away???? – Lucy Jul 24 '17 at 11:46
  • @kapsym yes but how does that work around ,do I have to create a separate class ,I'm having problems understanding how it works together ,I don't where to implement that in my code ,that's the issue – Lucy Jul 24 '17 at 11:48
  • @Lucy See if the below answer helps you understand how to use it. – Kapil G Jul 24 '17 at 11:54

1 Answers1

5

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());
Kapil G
  • 4,081
  • 2
  • 20
  • 32