Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GItHub/npm: support CRC32 checksum check #202

Open
azu opened this issue Sep 25, 2021 · 1 comment
Open

GItHub/npm: support CRC32 checksum check #202

azu opened this issue Sep 25, 2021 · 1 comment
Labels
Status: PR Welcome Welcome to Pull Request Status: Proposal Request for comments

Comments

@azu
Copy link
Member

azu commented Sep 25, 2021

Currently, secretlint-rule-{npm/github} does not check CRC32 in tokens.

we want to suport it.

PoC

// https://gist.github.com/kevinyan815/f71b2f5ca3541631abd2e50f3929739b
function toBase62(n) {
    if (n === 0) {
        return '0';
    }
    const digits = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
    let result = '';
    while (n > 0) {
        result = digits[n % digits.length] + result;
        n = parseInt(n / digits.length, 10);
    }
    
    return result;
}

//https://stackoverflow.com/a/18639999
const crc32 = function (str) {
    const makeCRCTable = function () {
        var c;
        var crcTable = [];
        for (var n = 0; n < 256; n++) {
            c = n;
            for (var k = 0; k < 8; k++) {
                c = ((c & 1) ? (0xEDB88320 ^ (c >>> 1)) : (c >>> 1));
            }
            crcTable[n] = c;
        }
        return crcTable;
    };
    const crcTable = makeCRCTable();
    let crc = 0 ^ (-1);
    for (let i = 0; i < str.length; i++) {
        crc = (crc >>> 8) ^ crcTable[(crc ^ str.charCodeAt(i)) & 0xFF];
    }
    return (crc ^ (-1)) >>> 0;
};

console.log(toBase62(crc32("qkJaB6MffYVzZXWqmcoF49yrUxP3wf")).padStart(6, "0")); // "0LsakP"

Minimal implementation for CRC32.

TODO

  • npm and GitHub use same base62 table?
  • Should we make it option?

Reference

Originally posted by @azu in #200 (comment)

@azu azu added Status: Proposal Request for comments Status: PR Welcome Welcome to Pull Request labels Sep 25, 2021
@azu
Copy link
Member Author

azu commented Jan 28, 2023

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: PR Welcome Welcome to Pull Request Status: Proposal Request for comments
Projects
None yet
Development

No branches or pull requests

1 participant