Java Android文件存储存储内存位置

Java Android文件存储存储内存位置,java,android,file,hash,storage,Java,Android,File,Hash,Storage,我正在尝试将密码的散列存储到我的应用程序文件目录中的.txt文件中,但是似乎存储或读取的只是内存位置,而不是实际的散列 这是负责对密码进行哈希运算的函数: private void hashPassword(String pass)throws UnsupportedEncodingException, NoSuchAlgorithmException { //Hashed password is a global String hashedPassword = hasher.h

我正在尝试将密码的散列存储到我的应用程序文件目录中的
.txt
文件中,但是似乎存储或读取的只是内存位置,而不是实际的散列

这是负责对密码进行哈希运算的函数:

private void hashPassword(String pass)throws UnsupportedEncodingException, NoSuchAlgorithmException {
    //Hashed password is a global String
    hashedPassword = hasher.hash(pass);
}
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));
}
这是上面调用的哈希类方法,用于创建密码的SHA-256哈希:

private void hashPassword(String pass)throws UnsupportedEncodingException, NoSuchAlgorithmException {
    //Hashed password is a global String
    hashedPassword = hasher.hash(pass);
}
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();
    }

}

这样做的结果是只显示一个内存位置,我无法比较密码散列和验证登录。任何帮助都将不胜感激。

如果您想逐行读取密码文件,当前您正在读取,直到空白为止

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

如果要逐行读取密码文件,则当前正在读取,直到出现空白

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

请告诉我们您在文本框和文件内容中看到了什么。我将您的hash()函数插入到单元测试中,它似乎做了一些有意义的事情。
text=myScan.next()。你把前者扔掉了。把它们加起来<代码>文本+=myScan.next()。将文本初始化为“”而不是null。请显示您在文本框和文件内容中看到的内容。我已将您的hash()函数插入单元测试,它似乎执行了一些有意义的操作。
text=myScan.next()。你把前者扔掉了。把它们加起来<代码>文本+=myScan.next()。用“”而不是null初始化文本。文件中应该只有一个内容,因为我单独存储文件,直到建立数据库,所以我尝试读取文件的全部内容(应该是一个字/行。我将尝试读取整行,以防散列创建一个新行字符。这工作正常,感谢您帮助我看到这一点。存储十六进制编码的值可能更直接,请看,文件中应该只有一件事,因为我单独存储文件,直到获得一个数据库集因此,我正在尝试读取文件的全部内容(应该是一个字/行。我将尝试读取整行内容,以防散列创建新行字符。这工作正常,感谢您帮助我了解这一点。存储十六进制编码的值可能更直接,请参阅