Hello, I need your help. I need to check whether a user is registered in the system when the user logs in (a table with the accounts of all users of the system is stored in the database). But after reading the Internet, I realized that it was desirable not to store a password, but a password hash, to find the hash, I used the sha-1 algorithm. Below is attached the php script of the server accepting email and password, and verifying whether such an account exists or not. If it exists, then send it. We will convert the data into the json format, which we get in the SignUp class; if it does not exist, then nothing.
<?php $data = file_get_contents('php://input'); $json = json_decode($data); $email = $json->{'Email'}; $password = $json->{'Password'}; $values = array(); $mysqli = new mysqli("*******", *******", "******", ******"); if (mysqli_connect_errno()) { printf("Соединение не установлено: %s\n", mysqli_connect_error()); exit(); } $query = "SELECT UserRegistrations.FullName, UserRegistrations.Email, UserRegistrations.Password, Profiles.IdProfile, Profiles.Profile FROM UserRegistrations inner join Profiles on UserRegistrations.ProfileKod = Profiles.IdProfile where Email = '$email' and Password = '$password'"; $result = $mysqli->query($query); if ($result->num_rows > 0) { while($row = $result->fetch_assoc()) { $values[] = $row; } echo json_encode($values); }
?>
The SignUp class for sending a request to the server and receiving a response from the server (in json format) storing a php script. The hashSha1 variable stores the password hash. When sending data, an email and an encrypted password are sent to the server. The problem as I think is that the data does not reach the server or the problem in the php-script. I tried to send data to the server without encryption, everything worked as soon as I encrypted the password, and sent an email and password to the server (the account already exists), but I don’t receive an account, although it is in the database.
public class SignUp extends AsyncTask<String, String, String> { public String emailValue; public String passwordValue; public String hashSha1; @Override protected String doInBackground(String... params) { emailValue = emailEditText.getText().toString(); passwordValue = passwordEditText.getText().toString(); MessageDigest sha1 = null; try { sha1 = MessageDigest.getInstance("SHA-1"); sha1.update(passwordValue.getBytes("ASCII")); byte[] data = sha1.digest(); hashSha1 = convertToHex(data); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } client = new DefaultHttpClient(); post = new HttpPost("*******"); HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); // Timeout HttpResponse response; JSONObject json = new JSONObject(); try { json.put("Email", emailValue); json.put("Password", hashSha1); Log.d(TAG, hashSha1); post.setHeader("json", json.toString()); StringEntity se = new StringEntity(json.toString()); se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json")); post.setEntity(se); response = client.execute(post); if (response != null) { InputStream in = response.getEntity().getContent(); // Get the Log.i("Read from Server", in.toString()); BufferedReader br = new BufferedReader(new InputStreamReader(in)); StringBuilder sb = new StringBuilder(); String line; while ((line = br.readLine()) != null) { sb.append(line + "\n"); } br.close(); Log.d(TAG, "Sb " + sb.toString()); if (!sb.toString().equals("")) { JSONArray array = new JSONArray(sb.toString()); for (int i = 0; i < array.length(); i++) { //parse of array JSONObject jObject = array.getJSONObject(i); fullName = jObject.getString("FullName"); email = jObject.getString("Email"); password = jObject.getString("Password"); profile = jObject.getString("Profile"); userregistrations.add(new UserRegistration(fullName, email, password, profile)); value = 1; Log.d(TAG, fullName); Log.d(TAG, email); Log.d(TAG, password); Log.d(TAG, profile); } } else{ Log.d(TAG, "Sb == 0"); value = 0; } } } catch (Exception e) { e.printStackTrace(); } return null; }