title: How can I store hashed password in sqlite android
body: <pre><code>protected static byte[] getHashedKey(String password,String MODE)throws IOException, NoSuchAlgorithmException, InvalidKeySpecException {
  
      saltIV = new byte[48];
      salt = new byte[32];
      ivBytes =new byte[16];
  
      if(MODE.equals("CREATE_VAULT")){
          //generate salt ,iv &amp; save them
          salt = generateSalt();
          ivBytes = generateIV();
  
          System.arraycopy(salt, 0, saltIV, 0, salt.length);
          System.arraycopy(ivBytes, 0, saltIV, salt.length, ivBytes.length);
  
          //save salt &amp; iv
          //FileOutputStream saltIvOutFile = new FileOutputStream("C:\\saltIv.ats");
          //saltIvOutFile.write(saltIV);
          //saltIvOutFile.close();
      }
      if(MODE.equals("OPEN_VAULT")){
          FileInputStream saltIvInFile = new FileInputStream("C:\\saltIv.ats");
          saltIvInFile.read(saltIV);
          saltIvInFile.close();
  
          System.arraycopy(saltIV, 0, salt, 0, salt.length);
          System.arraycopy(saltIV, salt.length, ivBytes, 0, ivBytes.length);
      }
  
      // Derive the key
      SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");
      PBEKeySpec spec = new PBEKeySpec(
              password.toCharArray(),
              salt,
              pswdIterations,
              keySize
      );
  
      SecretKey secretKey = factory.generateSecret(spec);
  
      return secretKey.getEncoded();
  }
  </code></pre>
  
  <p>this is where the hashed password gets generated and</p>
  
  <p>try {</p>
  
  <pre><code>String finalKey = Arrays.toString(androCrypter.getHashedKey(v.password,"CREATE_VAULT"));
                                  } catch (IOException e) {
                                      e.printStackTrace();
                                  } catch (NoSuchAlgorithmException e) {
                                      e.printStackTrace();
                                  } catch (InvalidKeySpecException e) {
                                      e.printStackTrace();
                                  }
                                  boolean vaultCreationSuccessful = new TableController(context).addNewVault(finalKey,v.vaultName,v.algorithm);
  </code></pre>
  
  <p>this is where I save the hash as string and insert it to database
  but it never gets saved.The sqlite column is TEXT type.I came to know that sqlite supports directly saving byte array so I tried with byte[] also but same result,no entry in database.There is nothing wrong with <strong>addVault()</strong> code as I have checked it with other normal string and all,it works just fine.
  Here is the code</p>
  
  <pre><code>protected boolean addNewVault(String finalKey,String vaultName,String algorithm) {
  
          ContentValues values = new ContentValues();
          values.put("Vault_Name", vaultName);
          values.put("KEY", finalKey);
          values.put("Algorithm", algorithm);
  
          SQLiteDatabase db = this.getWritableDatabase();
  
          boolean createSuccessful = db.insert(DATABASE_NAME, null, values) &gt; 0;
          db.close();
  
          return createSuccessful;
      }
  </code></pre>
  
  <p>But in this case I cant get it to work.Nothing gets saved.I also read about BLOB but I think its not needed as because byte[] is supported in sqlite.
  What is wrong ? 
  Any help/suggestion would be really appreciated.Thank you.</p>
  
  <p>NOTE:Please ignore the FileInputStream and FileOutputtream lines in getHashedKey();</p>
  
  <p>EDIT:
  I just figured out that this</p>
  
  <pre><code>try {
         finalKey = Arrays.toString(androCrypter.getHashedKey(v.password,"CREATE_VAULT"));
                                  } catch (IOException e) {
                                      e.printStackTrace();
                                  } catch (NoSuchAlgorithmException e) {
                                      e.printStackTrace();
                                  } catch (InvalidKeySpecException e) {
  </code></pre>
  
  <p>code is not getting executed that is the problem. I have created another class just to check if it works there and it does.
  Tried another method and calling it like finalKey= getFinalKey(v.password) and this method contains the above code but that does not work.
  I am completely out of clue.Please help.</p>