From 5e429ba71fdf31756666dcc550d651edba220ff9 Mon Sep 17 00:00:00 2001 From: Raul Ochoa Date: Thu, 23 Mar 2017 01:03:45 +0100 Subject: [PATCH 1/2] Use crc32 instead of md5 for computing subdomain candidate --- lib/cartodb/models/resource-locator.js | 28 +++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/lib/cartodb/models/resource-locator.js b/lib/cartodb/models/resource-locator.js index 190cbe85..5799e642 100644 --- a/lib/cartodb/models/resource-locator.js +++ b/lib/cartodb/models/resource-locator.js @@ -91,9 +91,31 @@ function getCdnDomain(serverMetadata, resource) { return null; } +// ref https://jsperf.com/js-crc32 +function crcTable() { + var c; + var table = []; + for (var n = 0; n < 256; n++) { + c = n; + for (var k = 0; k < 8; k++) { + c = ((c & 1) ? (0xEDB88320 ^ (c >>> 1)) : (c >>> 1)); + } + table[n] = c; + } + return table; +} +var CRC_TABLE = crcTable(); + +function crc32(str) { + var crc = 0 ^ (-1); + for (var i = 0; i < str.length; i++) { + crc = (crc >>> 8) ^ CRC_TABLE[(crc ^ str.charCodeAt(i)) & 0xFF]; + } + return (crc ^ (-1)) >>> 0; +} + function subdomain(subdomains, resource) { - var resourceHash = crypto.createHash('md5').update(resource, 'binary').digest('hex'); - var index = parseInt(resourceHash, 16) % subdomains.length; + var index = crc32(resource) % subdomains.length; return subdomains[index]; } -module.exports.subdomain = subdomain; \ No newline at end of file +module.exports.subdomain = subdomain; From e0c2423ace5d8a33b7b46f3696cb2525ecdfe449 Mon Sep 17 00:00:00 2001 From: Raul Ochoa Date: Thu, 23 Mar 2017 01:14:56 +0100 Subject: [PATCH 2/2] Remove unused import --- lib/cartodb/models/resource-locator.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/cartodb/models/resource-locator.js b/lib/cartodb/models/resource-locator.js index 5799e642..e12ef2b4 100644 --- a/lib/cartodb/models/resource-locator.js +++ b/lib/cartodb/models/resource-locator.js @@ -1,5 +1,3 @@ -var crypto = require('crypto'); - var dot = require('dot'); dot.templateSettings.strip = false;