Search code examples
javakeepass

How to avoid several calls to a method by saving its returning value?


I have this method

public String getCredentials(String entry) throws IOException {
    KeePassFile database = KeePassDatabase
            .getInstance(config.getProperty("keyPassDataBasePath"))
            .openDatabase(new File(config.getProperty("keyPassKeyPath")));
    Entry sampleEntry = database.getEntryByTitle(entry);
    return sampleEntry.getPassword();
}

Which is basically going to a KeePass DB, retrieves a password according to the title of its belonging account.

There are a LOT of methods that need 2 passwords, so 2 entries are used. I don't want to call that method everytime as I think it's a waste of resources. How can I save the returning value, and use it in other classes where the methods need those values?

Would this work? I feel like it's gonna make the method call several times anyways

    private static String pwd1;
    private static String pwd2;

    public void setValues() throws IOException {
        pwd1 = getCredentials("accountName1");
        pwd2 = getCredentials("accountName2");
    }

    public String getPwd1(){
        return pwd1;
    }

    public String getPwd2(){
        return pwd2;
    }

Solution

  • Store them in a HasMap with the key being the entry and the password being the value:

    class CachedCredentials {
      private Map<String, String> storedPasswords = new HashMap<>();
    
      private Properties config;
      
      public CachedCredentials(Properties config) {
         this.config = config;
      }
      
      public String getCredentials(String entry) {
        if (!storedPasswords.containsKey(entry)) {
          KeePassFile database = KeePassDatabase
            .getInstance(config.getProperty("keyPassDataBasePath"))
            .openDatabase(new File(config.getProperty("keyPassKeyPath")));
      
          Entry sampleEntry = database.getEntryByTitle(entry);   
          storedPasswords.put(entry, sampleEntry.getPassword());
        }
    
        return storedPasswords.get(entry);
      }
    

    then in your setValues method you can do:

    private cachedCreds; //initialize this in your constructor
    
    public void setValues() throws IOException {
        pwd1 = cachedCreds.getCredentials("accountName1");
        pwd2 = cachedCreds.getCredentials("accountName2");
    }
    

    This solution might provide to be insecure if someone is doing a memory snooping while the program is running. Might want to think of a way to obfuscate the cached password by either base64encoding it or actually encrypting it but that goes above what is being asked.