views:

36

answers:

1

Hi,

I'm creating an encoded key like this:

public static String generateKey(String username) {
    return KeyFactory.keyToString(
        KeyFactory.createKey(User.class.getSimpleName(), username));
}

is it possible to decompose the key to get the original username out of it?

String encoded = generateKey("bob");
String decoded = KeyFactory.decodeKey(encoded);
// decoded = "bob".

Thanks

--------- Edit: How I'm storing the key -------------------------

@PrimaryKey 
@Extension(vendorName = "datanucleus", key = "gae.encoded-pk", value="true") 
private String mKey;
A: 

It's probably not a great idea to try and extract one, even if the key returned currently looks reversible. From the KeyFactory documentation:

Clients should not make any assumptions about this return value, except that it is a websafe string that does not need to be quoted when used in HTML or in URLs.

jball
That is a bummer! Was hoping that I could decode it, would save me from having to store the username as another string in the class.
Sorry to be the bearer of bad news :(
jball