We have a web application written in Java whereby when the users sign in, we want them to be anonymous to each other. So what we do, is give them a number. I needed an algorithm to generate the number from their database user id, and I wanted the algorithm to vary such that it wasn't that easy to figure out.
So what I do, is on even days of the month, the user id is added to the day of the month, and on odd month days, I take the absolute value of the day of month - the user id.
//get day of month
int dayOfMonth = cal.get(Calendar.DAY_OF_MONTH);
//cast it to a double so that I can get a modulus 2 which indicates whether it is even or odd
double amem = (double) dayOfMonth;
if ((amem % 2) == 0) { // it's an even day with no remainder
dayOfMonth = dayOfMonth + userId;
} else {
//its an odd day
dayOfMonth = Math.abs(dayOfMonth - userId);
}
Hope this helps someone.
No comments:
Post a Comment