I'm working for a company that is generating gift card codes which can be used to pay for goods on online stores.
I'm wondering what the most secure way of generating these gift card codes are. The length needs to be 16 characters (though that is negotiable) and can be alphanumeric (though numeric would be more customer friendly).
From what I can see, the most secure way to do this is generate a gift card code of a specific length with the following Java code:
static final String AB = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
static SecureRandom rnd = new SecureRandom();
String randomString( int len ){
StringBuilder sb = new StringBuilder( len );
for( int i = 0; i < len; i++ )
sb.append( AB.charAt( rnd.nextInt(AB.length()) ) );
return sb.toString();
}
This is taken from the Stack Overflow answer here. I removed the lowercase letters from the string to make it more user friendly. So this produces 36 ^ 16
combinations. Numeric alone would be 10 ^ 16
combinations. I believe numeric alone would be enough but it's often stressed that, given the increasing prevalence of gift card fraud, the string should be alphanumeric. So that's question one: numeric or alphanumeric?
When users use the gift cards on an online store to pay for goods, a call is made to our API which returns the balance and currency for that gift card. Given that the gift card codes are entered on 3rd party servers, these gift cards are now available to people with access to those servers. This is obviously a problem in the case where there is still a balance left after a user has partially redeemed one.
One option would be to, when the call to our API is made (with the gift card code) to get the balance, we return and save on their store a random string which can only be used by the online store when they are billing us - we will match that with the gift card code on our system. The problem with that is presumably the gift card code the user enters on checkout gets logged somewhere in their logs, and is accessible to anyone with access to those logs.
Another option is that we refresh the gift card code after it is partially redeemed. So the user essentially gets issued with a new gift card code for the balance and the previous one is cancelled. This is probably the most secure, but not that user friendly. So that's the second question: how do we secure gift card codes that are only partially redeemed and still have value left on them?
EDIT:
We also have a Check Balance page where a user enters a gift card code and the currency and balance are returned. This presumably creates some additional security concerns.