Reputation:
I have to send a hashed string from an android client program to a java server.
Client side:
byte[] encodedID = MessageDigest.getInstance("SHA-256").digest(id.getBytes("UTF-8"));
String encodedID64 = Base64.encodeToString(encodedID,Base64.NO_WRAP);
Server:
byte[] hashbytes = logInfo.getString(LoginHash).getBytes();
String logHash = new String(hashbytes,"UTF-8");
The string is stored on the server. When the app closes, the background service for network tasks is restarted. It reads the Base64 encoded hash from shared preferences, and sends it to the server for logging in again. However, calling matches() to compare the stored string and the value received returns a false.
This is what the received and the stored values look like:
Stored:"WuYnLw/vKlrrzEWaetqcbqPMcu+cQ0hvnV0Yf5kiIdE="
Given :"WuYnLw/vKlrrzEWaetqcbqPMcu+cQ0hvnV0Yf5kiIdE="
I guess it's an encoding issue, but I can't figure out how to fix it.
Upvotes: 1
Views: 2222
Reputation: 14938
You should use equals()
since matches()
treats +
as a special character:
System.out.println("a+b".matches("a+b")); // false
System.out.println("a+b".equals("a+b")); // true
Upvotes: 3