Question

I am a bit out of my comfort zone here, so I'm not even sure I'm aproaching the problem appropriately. Anyhow, here goes:

So I have a problem where I shall hash some info with sha1 that will work as that info's id.
when a client wants to signal what current info is being used, it sends a percent-encoded sha1-string.

So one example is, my server hashes some info and gets a hex representation like so:
44 c1 b1 0d 6a de ce 01 09 fd 27 bc 81 7f 0e 90 e3 b7 93 08

and the client sends me

D%c1%b1%0dj%de%ce%01%09%fd%27%bc%81%7f%0e%90%e3%b7%93%08

Removing the % we get

D c1 b1 0dj de ce 01 09 fd 27 bc 81 7f 0e 90 e3 b7 93 08

which matches my hash except for the beginning D and the j after the 0d, but replacing those with their ascii hex no, we have identical hash.

So, as I have read and understood the urlencoding, the standard would allow a client to send the D as either D or %44? So different clients would be able to send different representations off the same hash, and I will not be able to just compare them for equality?

I would prefer to be able to compare the urlencoded strings as they are when they are sent, but one way to do it would be to decode them, removing all '%' and get the ascii hex value for whatever mismatch I get, much like the D and the j in my above example.

This all seems to be a very annoying way to do things, am I missing something, please tell me I am? :)

I am doing this in node.js but I suppose the solution would be language/platform agnostic.

Was it helpful?

Solution

I made this crude solution for now:

var unreserved = 'A B C D E F G H I J S O N K L M N O P Q R S T U V W X Y Za b c d e f g h i j s o n k l m n o p q r s t u v w x y z + 1 2 3 4 5 6 7 8 9 0 - _ . ~';

function hexToPercent(hex){
var index = 0,
end = hex.length,
delimiter = '%',
step = 2,
result = '',
tmp = '';

if(end % step !== 0){
    console.log('\'' + hex + '\' must be dividable by ' + step + '.');
    return result;
}

while(index < end){
    tmp = hex.slice(index, index + step);

    if(unreserved.indexOf(String.fromCharCode('0x' + tmp)) !== -1){
        result = result + String.fromCharCode('0x' + tmp);
    }
    else{
        result = result + delimiter + tmp;
    }
    index = index + step;

}
return result;

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top