Skip to content

Commit

Permalink
Added : Support for buffer (#12)
Browse files Browse the repository at this point in the history
* Added : Support for buffer

* Added chrome launcher + update browserify

* Travis : allow karma
  • Loading branch information
Alex-Werner authored May 27, 2018
1 parent 2923591 commit f405c52
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 16 deletions.
11 changes: 9 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
language: node_js
sudo: false

dist: trusty
sudo: required
node_js:
- '6'
- '7'
- '8'
- '9'

addons:
chrome: stable
before_install:
# Needed for Karma test runner
- export DISPLAY=:99.0
- sh -e /etc/init.d/xvfb start
- sleep 3 # give xvfb some time to start
install:
- npm install

Expand Down
20 changes: 14 additions & 6 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ module.exports = function (grunt) {
'dist/x11-hash.js': ['lib']
},
options: {
require: [
'./index.js:x11hash'
]
require: [
'./index.js:x11hash',
'buffer'
]
}
}
},
Expand All @@ -42,16 +43,23 @@ module.exports = function (grunt) {
'test/test.js'
],
exclude: [],
browserNoActivityTimeout: 30000,
browserNoActivityTimeout: 60000,
reporters: ['mocha'],
port: 9876,
colors: true,
logLevel: 'INFO',
autoWatch: false,
browsers: ['PhantomJS'],
concurrency: Infinity,
customLaunchers: {
FirefoxHeadless: {
base: 'Firefox',
flags: ['-headless'],
},
},
browsers: ['ChromeHeadless', 'Firefox'],
singleRun: true,
plugins: [
'karma-phantomjs-launcher',
'karma-chrome-launcher',
'karma-firefox-launcher',
'karma-mocha-reporter',
'karma-mocha',
Expand Down
6 changes: 3 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ var errors = module.exports.errors = {

/**
* Obtain an x11 hash
* @param input {string|array} input data to hash
* @param inputFormat {number} optional - format of the input: 0: string, 1: 8 bit array, 2: 32 bit array
* @param input {string|array|buffer} input data to hash
* @param inputFormat {number} optional - format of the input: 0: string, 1: 8 bit array/Buffer, 2: 32 bit array
* @param outputFormat {number} optional - format of the output: 0: string, 1: 8 bit array, 2: 32 bit array
* @returns {string|array} x11 hash of input as a string, 8-bit array or 32-bit array
*/
Expand All @@ -51,7 +51,7 @@ module.exports.digest = function (input, inputFormat, outputFormat) {
throw (errors.input_format_mismatch_string);
}
} else if (inputFormat === 1 || inputFormat === 2) {
if (!Array.isArray(input)) {
if (!Array.isArray(input) && !h.isBuffer(input)) {
throw (errors.input_format_mismatch_array);
}
} else {
Expand Down
3 changes: 3 additions & 0 deletions lib/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,3 +249,6 @@ module.exports.b64Decode = function(input) {
}
return output;
};
module.exports.isBuffer = function(obj) {
return obj !== null && obj.constructor !== null && typeof obj.constructor.isBuffer === 'function' && obj.constructor.isBuffer(obj);
};
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
},
"devDependencies": {
"benchmark": "^2.1.4",
"browserify": "^13.3.0",
"browserify": "^16.2.2",
"chai": "^3.5.0",
"eslint": "^4.19.1",
"eslint-config-standard": "^10.2.1",
Expand All @@ -49,6 +49,7 @@
"grunt-multi": "0.0.7",
"karma": "^2.0.2",
"karma-chai": "^0.1.0",
"karma-chrome-launcher": "^2.2.0",
"karma-firefox-launcher": "^1.1.0",
"karma-mocha": "^1.3.0",
"karma-mocha-reporter": "^2.2.5",
Expand Down
14 changes: 10 additions & 4 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
'use strict';

// enable reuse of this test between npm test and karma browser test...
var x11, expect;
// enable reuse of this test between npm test and karma browser test...
var x11, buffer, expect;
if (typeof module !== 'undefined' && module.exports) {
x11 = require('../');
expect = require('chai').expect;
buffer = Buffer;
} else {
x11 = require('x11hash');
buffer = require('buffer').Buffer

}

var fox = 'The quick brown fox jumps over the lazy dog';
var empty = '';
var dash = 'DASH';
var longDream = 'Take this kiss upon the brow! And, in parting from you now, Thus much let me avow-- You are not wrong, who deem That my days have been a dream; Yet if hope has flown away In a night, or in a day, In a vision, or in none, Is it therefore the less gone? All that we see or seem Is but a dream within a dream. I stand amid the roar Of a surf-tormented shore, And I hold within my hand Grains of the golden sand-- How few! yet how they creep Through my fingers to the deep, While I weep--while I weep! O God! can I not grasp Them with a tighter clasp? O God! can I not save One from the pitiless wave? Is all that we see or seem But a dream within a dream?';
var int32 = [-1245000620, -1578223460, 654805539, -1068884769, -968029107, -8582190, 491541657, 290156804, 1046922525, 1254877013, -1307320917, 1691597203, 55068107, 1715389297, 252729336, 127805489];

var buffer = new buffer('0400000097ea9c8bee806143a8ae50116fe3d329dcbb18b5d8ea71a7a213a1b052000000b1950f668df2593684169b0e33ee7fb1b8e00d90ed906d80b4c2baa7d1b65f548f495a57ed98521d348b0700','hex')
describe('X11 Hash Functions', function () {

// main x11 digest function tests
Expand All @@ -40,6 +42,10 @@ describe('X11 Hash Functions', function () {
expect(x11.digest(int32, 2)).to.equal('ce06ca169b75084cd7b245966296e637e9af85091e848937af070f110cdb6298');
});

it('buffer', function () {
expect(x11.digest(buffer, 1)).to.equal('01840edc7f98704e3c66c6fcad1e90642a382f7c1cb4af64ed085cc50c000000');
});

// argument exceptions...
describe('input argument exceptions', function () {

Expand Down

0 comments on commit f405c52

Please sign in to comment.