Yesterday, Child and I attended a baptism for one of her high-school friends. While there, she met another friend who had recently gotten married. During introductions, we found out that both I and Child's friend's husband have the same first name. We also are both programmers. We both program in PHP. We both work from home.
The coincidences didn't stop there. We both looked familiar to each other, and after running through Ultimate Frisbee, college classes, and old apartments, we realized that we knew each other from a past writing group. In the process of figuring that out, Child and I found out that he and his friends had actually started the Quark club (BYU's sci-fi/fantasy club) many years ago. Quark was where Child and I first met. (Incidentally, "quark" is the sound a quantum duck makes.)
He thought it had died out when he and his friends left college, and it actually had, but someone had eventually revived it. It's now a full-blown club with dozens of active members; weekly writing meetings, reading groups, and movie nights; monthly socials; and at least one marriage a year. He was shocked to hear that, and I think it made his day to know that the club he had started was going so strong.
Completely unrelated: if Jean Valjean used the prison number tattooed on his chest to prove that he was Jean Valjean instead of the innocent man Javert had captured, then why didn't the LACK of a tattoo on the innocent man's chest prove his innocence?
Showing posts with label random. Show all posts
Showing posts with label random. Show all posts
Sunday, November 08, 2009
Monday, June 16, 2008
Underhanded C Programming
A couple days ago, Slashdot posted a link to The Underhanded C Contest. The idea is to write a program that redacts part of an image while making it possible to recover the lost information. The trick: it has to look innocent to another programmer reviewing your code.
My first version has the following output:
Here's the pertinent part of the code:
Redact to black (the "right" way):
p.rgb[R] = 0;
p.rgb[G] = 0;
p.rgb[B] = 0;
Redact to random noise (an acceptable way, see image on right):
p.rgb[R] = rand();
p.rgb[G] = rand();
p.rgb[B] = rand();
Nefarious redaction to random noise (see image on right):
p.rgb[R] ^= rand();
p.rgb[G] ^= rand();
p.rgb[B] ^= rand();
The key is using "^=" rather than just "=". The "^" will perform a bitwise XOR, and XOR has an interesting property. If you XOR A with B, and XOR the result by B a second time, you get back A.
So all we have to do to get the proper image back again is use the same sequence of random numbers, and that's easier than it sounds. It's customary to seed the random number generator using the following code:
srand((unsigned int)time(0));
But the file we're spitting out has the timestamp it was created--the exact time we're using as the seed in our RNG!
(If you're worried about the timestamp changing as the file is copied and passed around, then simply embed the time as a comment in the file--not nefarious at all.)
My first version has the following output:
Before | After |
![]() | ![]() |
Here's the pertinent part of the code:
Redact to black (the "right" way):
p.rgb[R] = 0;
p.rgb[G] = 0;
p.rgb[B] = 0;
Redact to random noise (an acceptable way, see image on right):
p.rgb[R] = rand();
p.rgb[G] = rand();
p.rgb[B] = rand();
Nefarious redaction to random noise (see image on right):
p.rgb[R] ^= rand();
p.rgb[G] ^= rand();
p.rgb[B] ^= rand();
The key is using "^=" rather than just "=". The "^" will perform a bitwise XOR, and XOR has an interesting property. If you XOR A with B, and XOR the result by B a second time, you get back A.
So all we have to do to get the proper image back again is use the same sequence of random numbers, and that's easier than it sounds. It's customary to seed the random number generator using the following code:
srand((unsigned int)time(0));
But the file we're spitting out has the timestamp it was created--the exact time we're using as the seed in our RNG!
(If you're worried about the timestamp changing as the file is copied and passed around, then simply embed the time as a comment in the file--not nefarious at all.)
Labels:
noise,
programming,
rand,
random,
redaction,
rng,
seed,
underhanded,
xor
Subscribe to:
Posts (Atom)