0

I need to generate a string using Java based on another string. But I need also to be able to check that the string generated is valid using the original string. The thing is that I need a Java app that can generate a valid code for each customer to allow them to do some actions, so when they use it, I need to be able to validate that the code they imputed was valid for that customer.

The origin string could be the email which is an unique field or maybe the id... Also the same logic to generate the key needs to be done in an Android app so they can get the key in case they are offline and then in the Java app I need to be able to verify if that key was valid or not.

petezurich
  • 9,280
  • 9
  • 43
  • 57
Faabass
  • 1,394
  • 8
  • 29
  • 58
  • 2
    It's hard to understand what you're looking for, but I think MD5 is goog enough for the job: https://stackoverflow.com/questions/4846484/md5-hashing-in-android – Vaiden Jul 10 '17 at 11:19
  • 1
    If you want to provide the client with a temporary token, then that token should be stored in your database to link to the account (and its permissions), how you store the token is up to you, but to make it humanly readable, just use Base64 encoding, no need for encryption – Wietlol Jul 10 '17 at 11:21
  • UUID and store the UUID and the customer ID somewhere –  Jul 10 '17 at 11:26

1 Answers1

-1

generate random word by java.util.Random and sum char by StringBuilder and for checking use String.contains

String specialWord = "abcde";

String wordNew = newWordBySpecialWord();//create word
Log.i("wordNew  ", wordNew);

Log.i("checking  ", String.valueOf(checking(wordNew))); //return true
Log.i("checking  ", String.valueOf(checking(wordNew+"gfrf"))); //return false



public String newWordBySpecialWord() {
    char[] chars = specialWord.toCharArray();
    StringBuilder sb = new StringBuilder();
    Random random = new Random();
    for (int i = 0; i < 16; i++) {
        char c = chars[random.nextInt(chars.length)];
        sb.append(c);
    }
    return sb.toString();
}

public boolean checking(String word) {
    for (char aChar : word.toCharArray()) {
        if (!specialWord.contains(String.valueOf(aChar))) {
            return false;
        }
    }
    return true;
}
Rasoul Miri
  • 11,234
  • 1
  • 68
  • 78
  • You posted essentially the same answer again - but it did not get better in the meantime. When checking, every word will pass that contains only characters of the original `specialWord`. For example `wordNew+"acba" is valid. In other words just 26 tries are necessary to guess a valid token if there is at least one letter in the original word. Moreover, one cannot determine what the original word was. So there is no chance to find out if the code is "valid for that customer". – Henry Jul 10 '17 at 20:05