lib/chi-square.js
function(buffer) {
        var inputSize = buffer.length;

        // Holds an array that represents the count of various characters.
        var charCount = [];

        // Loop through the bytes and increase charCount.
        for (var a = 0; a < inputSize; a++) {
            var temp = buffer.readUInt8(a);

            if (charCount[temp] !== undefined) {
                charCount[temp]++;
            }
            else {
                charCount[temp] = 1;
            }
        }

        // The average number of times a specific character should occur.
        var expectedCount = inputSize / 256;

        var chiSquare = 0;

        for (var i = 0; i < 256; i++) {
            // Ideally this would be close to 0.
            a = (charCount[i] !== undefined ? charCount[i] : 0) - expectedCount;

            chiSquare += (a * a) / expectedCount;
        }

        return calculateChiSquareProbability(parseFloat(chiSquare.toFixed(2)), 255);
    }
function(buffer) {
        var inputSize = buffer.length;

        // Holds an array that represents the count of various characters.
        var charCount = [];

        // Loop through the bytes and increase charCount.
        for (var a = 0; a < inputSize; a++) {
            var temp = buffer.readUInt8(a);

            if (charCount[temp] !== undefined) {
                charCount[temp]++;
            }
            else {
                charCount[temp] = 1;
            }
        }

        // The average number of times a specific character should occur.
        var expectedCount = inputSize / 256;

        var chiSquare = 0;

        for (var i = 0; i < 256; i++) {
            // Ideally this would be close to 0.
            a = (charCount[i] !== undefined ? charCount[i] : 0) - expectedCount;

            chiSquare += (a * a) / expectedCount;
        }

        return calculateChiSquareProbability(parseFloat(chiSquare.toFixed(2)), 255);
    }
function ( buffer ) { var inputSize = buffer . length ; // Holds an array that represents the count of various characters.
 var charCount = [ ] ; // Loop through the bytes and increase charCount.
 for ( var a = 0 ; a < inputSize ; a ++ ) { var temp = buffer . readUInt8 ( a ) ; if ( charCount [ temp ] !== undefined ) { charCount [ temp ] ++ ; } else { charCount [ temp ] = 1 ; } } // The average number of times a specific character should occur.
 var expectedCount = inputSize / 256 ; var chiSquare = 0 ; for ( var i = 0 ; i < 256 ; i ++ ) { // Ideally this would be close to 0.
 a = ( charCount [ i ] !== undefined ? charCount [ i ] : 0 ) - expectedCount ; chiSquare += ( a * a ) / expectedCount ; } return calculateChiSquareProbability ( parseFloat ( chiSquare . toFixed ( 2 ) ) , 255 ) ; }
Calculates a Chi-square distribution over a sequence of bytes within the Buffer.
The result is a float representing the probability of how frequently
a truly random sequence of bytes would exceed the calculated value.

Ideally this float should have a value of 0.5. If so, the given Buffer contained random data.

@param {Buffer} buffer
@return {Number}
Calculates a Chi - square distribution over a sequence of bytes within the Buffer . The result is a float representing the probability of how frequently a truly random sequence of bytes would exceed the calculated value .