DimensionDev / Maskbook

The portal to the new, open Internet. ([I:b])
https://mask.io
GNU Affero General Public License v3.0
1.51k stars 310 forks source link

[RFC] Checksum-based hidden feature detection #229

Closed neruthes closed 4 years ago

neruthes commented 5 years ago

Background

Criteria

Basic Idea

Only one additional character is added; no header or footer requirement.

We can study much from Luhn algorithm, including variations adopted by ICAO 9303.

Example Code

var NeruthesMaskworkChecksum_v1 = {
    encode: function (input) {
        return input + '' + (
            input.toUpperCase().replace(/[\+\=\/]/g, '0').split('').map(function (d, i) {
                return d.charCodeAt(0)*[17,7,1][i%3];
            }).reduce(function (l, r) {
                return (l + r) % 36;
            }) % 36
        ).toString(36).toUpperCase();
    },
    decode: function (input) {
        var content = input.slice(0, input.length-1);
        var expectedChecksumChar = input.slice(input.length-1);
        var calculatedChecksumChar = NeruthesMaskworkChecksum_v1.encode(content);
        if (expectedChecksumChar === calculatedChecksumChar.slice(calculatedChecksumChar.length-1)) {
            return content;
        } else {
            return null;
        };
    }
};

var TestData = [
    ['000', '000C'],
];

var testDidPass = true;

TestData.map(line => {
    if (NeruthesMaskworkChecksum_v1.encode(line[0]) === line[1] && NeruthesMaskworkChecksum_v1.decode(line[1]) === line[0]) {

    } else {
        console.log('boom>>');
        console.log('encode:');
        console.log(line[0] + ' should be <' + line[1] + '> but is actually:');
        console.log(NeruthesMaskworkChecksum_v1.encode(line[0]));
        console.log('----');
        console.log('decode:');
        console.log(line[1] + ' should be <' + line[0] + '> but is actually:');
        console.log(NeruthesMaskworkChecksum_v1.decode(line[0]));
        console.log('=====================');
        testDidPass = false;
    }
});

console.log(testDidPass ? 'Success!' : ':(');
Artoria2e5 commented 5 years ago

var dd = d.charCodeAt(0);

We don't need the substitution anymore do we?

return l + r

My brain is yelling something about MAX_SAFE_INTEGER but it should be fine here

Tedko commented 4 years ago

No need for now.

neruthes commented 4 years ago

30-day extension granted.

neruthes commented 4 years ago

I am closing all my RFCs since Meta/Article-3: RFC Peer Review Convention has been abolished. I assume that everyone agrees that this is not worth discussing for now. Some discussions may be restarted, if necessary, after Meta/Bill-4: DSD Peer Review Convention is ratified. In the meantime, if there is any question with documentation and workflow, please consult @yisiliu. Thanks for everyone who has engaged in the discussions so long.