私のアプリケーションのファイルディレクトリにある.txt
ファイルにパスワードのハッシュを保存しようとしていますが、実際のハッシュではなくメモリの場所が記憶されています。Androidのファイルストレージにメモリの場所が格納されています
これは、パスワードをハッシュの世話をする機能である:
private void hashPassword(String pass)throws UnsupportedEncodingException, NoSuchAlgorithmException {
//Hashed password is a global String
hashedPassword = hasher.hash(pass);
}
これは、パスワードのSHA-256ハッシュを作成するために、上記と呼ばれる調理人のクラスメソッドである:
public String hash(String text) throws UnsupportedEncodingException, NoSuchAlgorithmException {
byte[] b = text.getBytes(StandardCharsets.UTF_8);
MessageDigest digest = MessageDigest.getInstance("SHA-256");
return new String(digest.digest(b));
}
この
//Set up a file in the directory path of the app and give it the name pass.txt and pubKey.txt
private void setUpFiles(String typeSetup) {
//Check to see if it's setting up the pass file or key file
if(typeSetup.equals("pass")){
//passFile is a global file object object
passFile = new File(makeId.this.getApplicationContext().getFilesDir(),"pass.txt");
} else if (typeSetup.equals("key")){
encryptionKeyFile = new File(makeId.this.getApplicationContext().getFilesDir(),"pubKey.txt");
}
}
最後に、これは次の場所に書き込むファイルを設定するために使用する関数です。パスを格納する処理関数:
//Store the login info provided by the user
private boolean storeLogin() throws IOException {
//Store the hashed password in a text file
setUpFiles("pass");
//Create objects to handle file writing
FileWriter pw = new FileWriter(passFile);
BufferedWriter bw = new BufferedWriter(pw);
//Write the hashed password to the file
bw.write(new String(hashedPassword));
//Close resources
bw.close();
pw.close();
//Return if the file was created or not
return passFile.exists();
}
これらは以下、テキストファイルにハッシュされたパスワードを格納するための関数であったが、ファイルからストアを読み取り、ハッシュを表示するために、別のアクティビティによって使用される機能です。
public void loadPassHash() throws FileNotFoundException {
//File that should contain the hash of the password
File passFile = new File(c.getFilesDir(),"pass.txt");
//Create a textview object
tv = (TextView) findViewById(R.id.viewer);
//Create a scanner object to read the file
Scanner myScan = new Scanner(passFile);
//Initialize the text variable to store the contents of the file
String text = null;
//Loop the file and set the text to it
while (myScan.hasNext()) {
text=myScan.next();
}
//If there is something in the file
if (text!=null) {
//Set the global passHash string to the value of text
passHash=text;
//Display the hashed password for development
tv.setText(passHash);
//Close the scanner
myScan.close();
} else {
finish();
}
}
この結果は、メモリ位置だけが表示され、パスワードハッシュを比較してログインを検証できないということです。どんな助けもありがとう。
テキストボックスとファイルの内容の両方に表示された内容を表示できますか? – RichColours
私はあなたのhash()関数を単体テストに詰め込んだので、何か意味のあることが起きているようです。 – RichColours
'text = myScan.next();'あなたは前者を捨てている。それらを追加してください。 'text + = myScan.next();' nullではなく、 ""でテキストを初期化します。 – greenapps