java - Android文件存储存储内存位置

标签 java android file hash storage

我试图将密码的哈希值存储到应用程序文件目录中的 .txt 文件中,但是存储或读取的似乎只是内存位置,而不是实际的哈希值。

这是负责对密码进行哈希处理的函数:

private void hashPassword(String pass)throws UnsupportedEncodingException, NoSuchAlgorithmException {
    //Hashed password is a global String
    hashedPassword = hasher.hash(pass);
}

这是上面调用的 Hasher 类方法,用于创建密码的 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();
}

这些是将散列密码存储到文本文件中的函数,下面是另一个 Activity 用来从文件中读取、存储和显示散列的函数。

    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();
}

关于java - Android文件存储存储内存位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41047667/

相关文章:

java - java中使用文件作为大Map的存储介质

file - 在 Linux 上使用 D 如何确定文件是否隐藏?

java - 如何修复 "HTTP Status 500- Servlet exception threw an exception"

java - 无法将 JDBC-Hikari 连接到我的 Micronaut 应用程序

java - 配置 Play !在多个数据库环境中使用特定数据库的 2 个模型

android - 如何忽略 Android 中的测试用例?

android - MapView 显示空白,但 MapFragments 正常工作

android - 有没有办法使用 SDK 将时钟与 Microsoft Band 同步?

java - 从较长的文件路径中获取文件路径的一部分

java - AES-128 加密不适用于 Java < 1.7