WOL Apgar Routine

APGAR is an encrypted token sent to the server to identify a user’s password. This is a one-way encryption routine and cannot be decrypted. All Command and Conquer game client passwords using this algorithm must be exactly 8 characters long.

Psuedo-code implementation

function apgar(input)  
lookup = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
out = ""
i = 1

while (i <= 8)
left = ascii value of input[ i ]
right = ascii value of input[ length[input] - i ]

if left is odd
x = ((left * 2) XOR (left AND 1)) AND right
else
x = left XOR right

out += lookup[x AND 63]
i++

return out

PHP Implementation — Sean Wragg

<?php  
echo apgar('password'); //result WaIMMsbf

function apgar($a) {
if (strlen($a) == 8) {
$u = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
for ($i = 0; $i <= 7; $i++) {
$r = ($i == 0) ? 0 : ord($a[8 - $i]);
$x = (ord($a[$i]) & 1) ? (ord($a[$i]) << 1) & $r : ord($a[$i]) ^ $r;
$o[] = substr($u, ($x & 0x3f), 1);
}
return implode('', $o);
}
}

Javascript Implementation — Sean Wragg

function apgar(s) {  
if (s.length != 8) return false;
var s = s.split(''), o = [];
var u = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789./';
for(var i = 0; i <= 7; i++) {
var a = s[(8 - i)], r = ((i == 0) ? 0 : a.charCodeAt(0));
var j = s[i].charCodeAt(0), x = ((j & 1) ? (j << 1) & r : j ^ r);
var k = (x & 0x3f), f = u.substring(k, (k + 1));
o.push(f);
}
return o.join('');
}

console.log('apgar', apgar('password')); // WaIMMsbf

C++ Implementation — RenegadeProjects.com

std::string apgar(std::string input) {  
std::string lookup = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
std::string out = "";
int i = 0;
while (i < 8) {
unsigned char left = input[i];
unsigned char right = input[input.length() - i];

unsigned char x = left & 1 ? ((left << 1) ^ (left & 1)) & right : left ^ right;

out += lookup[x & 63];
i++;
}
return out;
}

mIRC Script Implementation — Frank Razenberg

alias apgar {  
var %lookup = abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789
var %out, %i = 1
while (%i <= 8) {
var %left, %right = 0
if (%i <= $len($1-)) { %left = $asc($mid($1-, %i ,1)) }
if ($calc($len($1-) - %i + 1) < $len($1-)) { %right = $asc($mid($1-, $calc($len($1-) - %i + 2), 1)) }
var %x = $iif(%left & 1, $and($calc(%left * (2 ^ $and(%left, 1))), %right), $xor(%left, %right))
;# echo -a left: %left - righT: %right - x: %x
%out = %out $+ $mid(%lookup, $calc($and(%x, 63) + 1), 1)
inc %i
}
return %out
}

Originally published at seanwragg.com on August 26, 2010.

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.