2016-12-08 5 views
1

私のアプリケーションのファイルディレクトリにある.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(); 
    } 

} 

この結果は、メモリ位置だけが表示され、パスワードハッシュを比較してログインを検証できないということです。どんな助けもありがとう。

+0

テキストボックスとファイルの内容の両方に表示された内容を表示できますか? – RichColours

+0

私はあなたのhash()関数を単体テストに詰め込んだので、何か意味のあることが起きているようです。 – RichColours

+0

'text = myScan.next();'あなたは前者を捨てている。それらを追加してください。 'text + = myScan.next();' nullではなく、 ""でテキストを初期化します。 – greenapps

答えて

0

パスワードファイルから1行ずつ読みたいですが、現在は空白になるまで読み込んでいます。

while (myScan.hasNextLine()) { 
    text=myScan.nextLine(); 
} 
+0

私はデータベースをセットアップしてファイルの内容全体を読み込もうとするまで、ファイルをilndividuallyに格納するので、ファイルには1つのものしかなければなりません(1つの単語/行にする必要があります)。ハッシュが改行文字を作成している場合には、行全体を読み込もうとしていますが、正しく動作してくれてありがとうございます。 – CS2016

+0

エンコードされた値を格納するためにはもっと簡単です。http://stackoverflow.com/質問/ 332079/in-java-how-do-i-convert-a-byte-array-to-a-hex-digit-keep-l-l – Victory

関連する問題