tags:

views:

54

answers:

1

IGNORE THE QUESTION:

The CSS File I was including pulled in the the other files hence the correlation *facepalm*


We have the following code for picking a CNAME CDN reference per filename. It must return the same URL everytime based on a given filename. We thought this would be sufficiently random:

<?php

  function cdn_prefix($fileName) {
    $number_of_servers = 4;

    $md5 = md5($fileName);

    $md5 = substr($md5, 0, 4);

    $hash_number = base_convert($md5, 16, 10);

    $server_number = ($hash_number % $number_of_servers) + 1;

    $server_prefix = '//static' . $server_number . '.' . $_SERVER['SERVER_NAME'];

    return $server_prefix . $fileName;
  }

?>

However it seems to favour the number 3:MD5

No matter what I do (salt, different bases, random multiplication, etc) the results headerBg through to mainNavPipe (on the screen shot) all have the same number.

Is there a better algorithm?

EDIT:

Here are the results using same algorithm using a SHA1 SHA1

Everywhere calls the same function - as it returns the whole URL and wouldn't show the static[1-4] domain unless it when through this function.

The array (for testing) is:

FILES = [
    '/a/files/image/250.jpg',
    '/a/files/image/244.jpg',
    '/a/files/image/247.jpg',
    '/a/css/global/core.css',
    '/a/css/global/print.css',
    '/a/img/global/new_logo.gif',
    '/a/img/global/book-a-free-survey.gif',
    '/a/img/global/make_an_enquiry.gif',
    '/a/img/global/purchase-locks-blue.jpg',
    '/a/files/image/251.jpg',
    '/a/img/global/bg.gif',
    '/a/img/global/headerBg.jpg',
    '/a/img/global/basketBg.gif',
    '/a/img/global/arrow.png',
    '/a/img/global/trolley.gif',
    '/a/img/global/mainNavBg.gif',
    '/a/img/global/mainNavCurrentBg.gif',
    '/a/img/global/mainNavPipe.gif',
    '/a/img/common/sectionNavBg.jpg',
    '/a/img/global/nav_arrow.gif',
    '/a/img/global/footerBg.jpg',
    '/a/img/global/footerCopyrightBg.jpg',
    '/a/img/global/footerLogo.jpg'
]
+1  A: 

This was probably a one-time thing or a bug elsewhere.

function cdn_prefix($fileName) {
    $number_of_servers = 4;
    $md5 = md5($fileName);
    $md5 = substr($md5, 0, 4);
    $hash_number = base_convert($md5, 16, 10);
    $server_number = ($hash_number % $number_of_servers) + 1;
    return $server_number;
}
$arr = array(1=>0, 2=>0, 3=>0, 4=>0,);
for ($i = 1; $i < 200000; $i++) {
    $arr[cdn_prefix("anrg".$i)]++;
}
print_r($arr);

gives:

Array
(
    [1] => 49770
    [2] => 50090
    [3] => 50026
    [4] => 50113
)
Artefacto