Skip to content

Commit 4bc1094

Browse files
committed
Initial checkin. Relates to brinchj/RndPhrase#11
0 parents  commit 4bc1094

8 files changed

+282
-0
lines changed

.editorconfig

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
; EditorConfig is awesome: http://EditorConfig.org
2+
3+
; top-most EditorConfig file
4+
root = true
5+
6+
[*]
7+
charset = utf-8
8+
9+
trim_trailing_whitespace = true
10+
11+
; Unix style line endings
12+
end_of_line = lf
13+
14+
; Always end file on newline
15+
insert_final_newline = true
16+
17+
; Indentation
18+
indent_style = space
19+
indent_size = 4

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

.jshintrc

+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
{
2+
// JSHint Default Configuration File (as on JSHint website)
3+
// See http://jshint.com/docs/ for more details
4+
5+
"maxerr" : 50, // {int} Maximum error before stopping
6+
7+
// Enforcing
8+
"bitwise" : true, // true: Prohibit bitwise operators (&, |, ^, etc.)
9+
"camelcase" : false, // true: Identifiers must be in camelCase
10+
"curly" : true, // true: Require {} for every new block or scope
11+
"eqeqeq" : true, // true: Require triple equals (===) for comparison
12+
"forin" : true, // true: Require filtering for..in loops with obj.hasOwnProperty()
13+
"immed" : true, // true: Require immediate invocations to be wrapped in parens e.g. `(function () { } ());`
14+
"indent" : 4, // {int} Number of spaces to use for indentation
15+
"latedef" : true, // true: Require variables/functions to be defined before being used
16+
"newcap" : true, // true: Require capitalization of all constructor functions e.g. `new F()`
17+
"noarg" : true, // true: Prohibit use of `arguments.caller` and `arguments.callee`
18+
"noempty" : true, // true: Prohibit use of empty blocks
19+
"nonew" : false, // true: Prohibit use of constructors for side-effects (without assignment)
20+
"plusplus" : true, // true: Prohibit use of `++` & `--`
21+
"quotmark" : "single", // Quotation mark consistency:
22+
// false : do nothing (default)
23+
// true : ensure whatever is used is consistent
24+
// "single" : require single quotes
25+
// "double" : require double quotes
26+
"undef" : true, // true: Require all non-global variables to be declared (prevents global leaks)
27+
"unused" : true, // true: Require all defined variables be used
28+
"strict" : false, // true: Requires all functions run in ES5 Strict Mode
29+
"trailing" : true, // true: Prohibit trailing whitespaces
30+
"maxparams" : false, // {int} Max number of formal params allowed per function
31+
"maxdepth" : false, // {int} Max depth of nested blocks (within functions)
32+
"maxstatements" : false, // {int} Max number statements per function
33+
"maxcomplexity" : false, // {int} Max cyclomatic complexity per function
34+
"maxlen" : false, // {int} Max number of characters per line
35+
36+
// Relaxing
37+
"asi" : false, // true: Tolerate Automatic Semicolon Insertion (no semicolons)
38+
"boss" : false, // true: Tolerate assignments where comparisons would be expected
39+
"debug" : false, // true: Allow debugger statements e.g. browser breakpoints.
40+
"eqnull" : false, // true: Tolerate use of `== null`
41+
"es5" : false, // true: Allow ES5 syntax (ex: getters and setters)
42+
"esnext" : false, // true: Allow ES.next (ES6) syntax (ex: `const`)
43+
// (ex: `for each`, multiple try/catch, function expression…)
44+
"evil" : false, // true: Tolerate use of `eval` and `new Function()`
45+
"expr" : false, // true: Tolerate `ExpressionStatement` as Programs
46+
"funcscope" : false, // true: Tolerate defining variables inside control statements"
47+
"globalstrict" : false, // true: Allow global "use strict" (also enables 'strict')
48+
"iterator" : false, // true: Tolerate using the `__iterator__` property
49+
"lastsemic" : false, // true: Tolerate omitting a semicolon for the last statement of a 1-line block
50+
"laxbreak" : false, // true: Tolerate possibly unsafe line breakings
51+
"laxcomma" : false, // true: Tolerate comma-first style coding
52+
"loopfunc" : false, // true: Tolerate functions being defined in loops
53+
"multistr" : false, // true: Tolerate multi-line strings
54+
"proto" : false, // true: Tolerate using the `__proto__` property
55+
"scripturl" : false, // true: Tolerate script-targeted URLs
56+
"smarttabs" : false, // true: Tolerate mixed tabs/spaces when used for alignment
57+
"shadow" : false, // true: Allows re-define variables later in code e.g. `var x=1; x=2;`
58+
"sub" : false, // true: Tolerate using `[]` notation when it can still be expressed in dot notation
59+
"supernew" : false, // true: Tolerate `new function () { ... };` and `new Object;`
60+
"validthis" : false, // true: Tolerate using this in a non-constructor function
61+
62+
// Environments
63+
"browser" : true, // Web Browser (window, document, etc)
64+
"couch" : false, // CouchDB
65+
"devel" : true, // Development/debugging (alert, confirm, etc)
66+
"dojo" : false, // Dojo Toolkit
67+
"jquery" : true, // jQuery
68+
"mootools" : false, // MooTools
69+
"node" : false, // Node.js
70+
"nonstandard" : true, // Widely adopted globals (escape, unescape, etc)
71+
"prototypejs" : false, // Prototype and Scriptaculous
72+
"rhino" : false, // Rhino
73+
"worker" : false, // Web Workers
74+
"wsh" : false, // Windows Scripting Host
75+
"yui" : false, // Yahoo User Interface
76+
77+
// Legacy
78+
"nomen" : false, // true: Prohibit dangling `_` in variables
79+
"onevar" : true, // true: Allow only one `var` statement per function
80+
"passfail" : false, // true: Stop on first error
81+
"white" : true, // true: Check against strict whitespace and indentation rules
82+
83+
// Custom Globals
84+
"predef" : [ // additional predefined global variables
85+
"process",
86+
"module",
87+
"exports",
88+
"require",
89+
"define",
90+
"__dirname",
91+
"__filename",
92+
"Buffer",
93+
"TR",
94+
"TRPAT",
95+
"GETTEXT",
96+
"GETSTATICURL",
97+
"ko"
98+
]
99+
}

.npmignore

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
test
2+
README.md
3+
.gitignore
4+
.jshintrc
5+
.editorconfig

README.md

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Cubehash.js
2+
===========
3+
CubeHash is a very simple cryptographic hash function.
4+
5+
This module is a javascript implementation of the algorithm [described here](http://cubehash.cr.yp.to/).
6+
7+
Adapted from [brinchj](https://github.com/brinchj)'s [implementation](https://github.com/brinchj/RndPhrase/blob/master/lib/cubehash.js).
8+
9+
Cubehash.js is AMD an CommonJS compatible, and can be user through [npm](https://npmjs.org) or [bower](http://bower.io)

cubehash.js

+106
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*jslint bitwise: true*/
2+
3+
// For info on CubeHash see: http://cubehash.cr.yp.to/
4+
5+
(function (root, factory) {
6+
if (typeof define === 'function' && define.amd) {
7+
// AMD. Register as an anonymous module.
8+
define(factory);
9+
} else if (typeof exports === 'object') {
10+
// Node. Does not work with strict CommonJS, but
11+
// only CommonJS-like enviroments that support module.exports,
12+
// like Node.
13+
module.exports = factory();
14+
} else {
15+
// Browser globals (root is window)
16+
root.returnExports = factory();
17+
}
18+
}(this, function () {
19+
// Init vector was computed by 10r rounds as described in the specification
20+
var init = [
21+
-2096419883, 658334063, -679114902, 1246757400,
22+
-1523021469, -289996037, 1196718146, 1168084361,
23+
-2027445816, -1170162360, -822837272, 625197683,
24+
1543712850, -1365258909, 759513533, -424228083,
25+
-13765010209, -2824905881, -9887154026, 19352563566,
26+
5669379852, -31581549269, 21359466527, 10648459874,
27+
-8561790247, 9779462261, -22434204802, -4124492433,
28+
19608647852, 9541915967, 5144979599, -4355863926
29+
];
30+
31+
function rotate(a, b) {
32+
return (a << b) | (a >>> (32 - b));
33+
}
34+
35+
function intToHex(v) {
36+
var s = '';
37+
38+
for (; v !== 0; v >>>= 8) {
39+
s += ((v >> 4) & 0xF).toString(16) + (v & 0xF).toString(16);
40+
}
41+
42+
return s;
43+
}
44+
45+
function swap(arr, i, j) {
46+
var tmp = arr[i];
47+
arr[i] = arr[j];
48+
arr[j] = tmp;
49+
return arr;
50+
}
51+
52+
function transform(state) {
53+
var i, r,
54+
y = new Array(16);
55+
56+
for (r = 0;r < 8; r += 1) {
57+
for (i = 0; i < 16; i += 1) { state[i + 16] += y[i^8] = state[i]; }
58+
for (i = 0; i < 16; i += 1) { state[i] = rotate(y[i], 7)^state[i + 16]; }
59+
for (i = 0; i < 16; i += 1) { y[i^2] = state[i + 16]; }
60+
for (i = 0; i < 16; i += 1) { state[i + 16] = y[i] + state[i]; }
61+
for (i = 0; i < 16; i += 1) { y[i^4] = state[i]; }
62+
for (i = 0; i < 16; i += 1) { state[i] = rotate(y[i], 11)^state[i + 16]; }
63+
for (i = 0; i < 16; i += 2) {
64+
swap(state, i + 16, i + 17);
65+
}
66+
}
67+
68+
for (i = 0; i < 16; i += 1) {
69+
y[i] = 0;
70+
}
71+
}
72+
73+
function hash(data) {
74+
// init state
75+
var i,
76+
s = '',
77+
state = new Array(32);
78+
79+
for (i = 0; i < 32; i += 1) {
80+
state[i] = init[i];
81+
}
82+
83+
// update with data
84+
data += String.fromCharCode(128);
85+
86+
for (i = 0; i < data.length; i += 1) {
87+
state[0] ^= data.charCodeAt(i);
88+
transform(state);
89+
}
90+
91+
// finalize
92+
state[31] ^= 1;
93+
94+
for (i = 0; i < 10; i += 1) {
95+
transform(state);
96+
}
97+
98+
// convert to hex
99+
for (i = 0; i < 8; i += 1) {
100+
s += intToHex(state[i]);
101+
}
102+
return s;
103+
}
104+
105+
return hash;
106+
}));

package.json

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"name": "cubehash.js",
3+
"description": "CubeHash is a very simple cryptographic hash function",
4+
"version": "0.1.0",
5+
"author": "Peter Müller <munter@fumle.dk>",
6+
"keywords": [
7+
"crypto",
8+
"hash"
9+
],
10+
"license": "BSD",
11+
"main": "cubehash.js",
12+
"directories": {
13+
"test": "test"
14+
},
15+
"scripts": {
16+
"test": "mocha"
17+
},
18+
"repository": {
19+
"type": "git",
20+
"url": "git@github.com:Munter/cubehash.js.git"
21+
},
22+
"devDependencies": {
23+
"mocha": "*"
24+
}
25+
}

test/cubehash-test.js

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*global describe, it*/
2+
3+
var assert = require('assert'),
4+
cubehash = require('../cubehash');
5+
6+
describe('cubehash', function () {
7+
it('Should be a function', function (done) {
8+
assert.equal(typeof cubehash, 'function');
9+
done();
10+
});
11+
12+
it('Should hash correctly', function (done) {
13+
assert.equal(cubehash(''), '38d1e8a22d7baac6fd5262d83de89cacf784a02caa866335299987722aeabc59');
14+
assert.equal(cubehash('Hello'), '692638db57760867326f851bd2376533f37b640bd47a0ddc607a9456b692f70f');
15+
assert.equal(cubehash('The quick brown fox jumps over the lazy dog'), '94e0c958d85cdfaf554919980f0f50b945b88ad08413e0762d6ff0219aff3e55');
16+
done();
17+
});
18+
});

0 commit comments

Comments
 (0)