As a beginner, I wrote the contract below for learning purposes, aiming to create a "Safe Messaging System":
pragma solidity ^0.4.24;
contract SafeMessage {
string message;
string password;
string enterPassword;
function sendMessage(string _message, string _password) public {
require (bytes(_message).length > 0 && bytes(_password).length > 0 );
message = _message;
password = _password;
}
function readMessage (string _enterPassword) view public returns (string) {
require (stringsEqual(password, _enterPassword) == true);
return (message);
}
function stringsEqual(string storage _a, string memory _b) internal pure returns(bool) {
bytes storage a = bytes(_a);
bytes memory b = bytes(_b);
if (keccak256(a) != keccak256(b)) {
return false;
}
return true;
}
}
So, with the code above, I can set a message, and calling "readMessage" will only return the message with the right password. The problem is, from what I understand, the data stored in string form (message & password) can be read by somebody else. Is that true? And how does that work?
Also, for the password, I could just take its hash instead of storing it directly as string, but that wouldn't solve the problem if the message can be read otherwise anyway. If what was mentioned in the previous paragraph is true, how could one actually create a "Safe Messaging System" with a smart contract?