Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* First pass at v5 support * test v5 against known results * specify string encoding of UTF8 (#190) * specify string encoding of UTF8 Node changed the default from `binary` to `utf8` some time back; this change just specifies `utf8`for older versions of node. https://nodejs.org/dist/latest-v4.x/docs/api/buffer.html#buffer_class_method_buffer_from_str_encoding https://nodejs.org/dist/latest-v7.x/docs/api/buffer.html#buffer_class_method_buffer_from_string_encoding * support node <4 Buffer API * rm blueimp-md5 dependency ('cuz we don't actually depend on it) * {}'s
- Loading branch information
Showing
11 changed files
with
276 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
{ | ||
"root": true, | ||
"env": { | ||
"browser": true, | ||
"commonjs": true, | ||
"node": true, | ||
"mocha": true | ||
}, | ||
"extends": ["eslint:recommended"], | ||
"installedESLint": true, | ||
"rules": { | ||
"array-bracket-spacing": ["warn", "never"], | ||
"arrow-body-style": ["warn", "as-needed"], | ||
"arrow-parens": ["warn", "as-needed"], | ||
"arrow-spacing": "warn", | ||
"brace-style": "warn", | ||
"camelcase": "warn", | ||
"comma-spacing": ["warn", {"after": true}], | ||
"dot-notation": "warn", | ||
"indent": ["warn", 2, { | ||
"SwitchCase": 1, | ||
"FunctionDeclaration": {"parameters": 1}, | ||
"MemberExpression": 1, | ||
"CallExpression": {"arguments": 1} | ||
}], | ||
"key-spacing": ["warn", {"beforeColon": false, "afterColon": true, "mode": "minimum"}], | ||
"keyword-spacing": "warn", | ||
"no-console": "off", | ||
"no-empty": "off", | ||
"no-multi-spaces": "warn", | ||
"no-redeclare": "off", | ||
"no-restricted-globals": ["warn", "Promise"], | ||
"no-trailing-spaces": "warn", | ||
"no-undef": "error", | ||
"no-unused-vars": ["warn", {"args": "none"}], | ||
"padded-blocks": ["warn", "never"], | ||
"object-curly-spacing": ["warn", "never"], | ||
"quotes": ["warn", "single"], | ||
"react/prop-types": "off", | ||
"react/jsx-no-bind": "off", | ||
"semi": ["warn", "always"], | ||
"space-before-blocks": ["warn", "always"], | ||
"space-before-function-paren": ["warn", "never"], | ||
"space-in-parens": ["warn", "never"] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,6 @@ var rb = require('crypto').randomBytes; | |
|
||
function rng() { | ||
return rb(16); | ||
}; | ||
} | ||
|
||
module.exports = rng; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
// Adapted from Chris Veness' SHA1 code at | ||
// http://www.movable-type.co.uk/scripts/sha1.html | ||
'use strict'; | ||
|
||
function f(s, x, y, z) { | ||
switch (s) { | ||
case 0: return (x & y) ^ (~x & z); | ||
case 1: return x ^ y ^ z; | ||
case 2: return (x & y) ^ (x & z) ^ (y & z); | ||
case 3: return x ^ y ^ z; | ||
} | ||
} | ||
|
||
function ROTL(x, n) { | ||
return (x << n) | (x>>> (32 - n)); | ||
} | ||
|
||
function sha1(bytes) { | ||
var K = [0x5a827999, 0x6ed9eba1, 0x8f1bbcdc, 0xca62c1d6]; | ||
var H = [0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0]; | ||
|
||
if (typeof(bytes) == 'string') { | ||
var msg = unescape(encodeURIComponent(bytes)); // UTF8 escape | ||
bytes = new Array(msg.length); | ||
for (var i = 0; i < msg.length; i++) bytes[i] = msg.charCodeAt(i); | ||
} | ||
|
||
bytes.push(0x80); | ||
|
||
var l = bytes.length/4 + 2; | ||
var N = Math.ceil(l/16); | ||
var M = new Array(N); | ||
|
||
for (var i=0; i<N; i++) { | ||
M[i] = new Array(16); | ||
for (var j=0; j<16; j++) { | ||
M[i][j] = | ||
bytes[i * 64 + j * 4] << 24 | | ||
bytes[i * 64 + j * 4 + 1] << 16 | | ||
bytes[i * 64 + j * 4 + 2] << 8 | | ||
bytes[i * 64 + j * 4 + 3]; | ||
} | ||
} | ||
|
||
M[N - 1][14] = ((bytes.length - 1) * 8) / | ||
Math.pow(2, 32); M[N - 1][14] = Math.floor(M[N - 1][14]); | ||
M[N - 1][15] = ((bytes.length - 1) * 8) & 0xffffffff; | ||
|
||
for (var i=0; i<N; i++) { | ||
var W = new Array(80); | ||
|
||
for (var t=0; t<16; t++) W[t] = M[i][t]; | ||
for (var t=16; t<80; t++) { | ||
W[t] = ROTL(W[t - 3] ^ W[t - 8] ^ W[t - 14] ^ W[t - 16], 1); | ||
} | ||
|
||
var a = H[0], b = H[1], c = H[2], d = H[3], e = H[4]; | ||
|
||
for (var t=0; t<80; t++) { | ||
var s = Math.floor(t/20); | ||
var T = ROTL(a, 5) + f(s, b, c, d) + e + K[s] + W[t] >>> 0; | ||
e = d; | ||
d = c; | ||
c = ROTL(b, 30) >>> 0; | ||
b = a; | ||
a = T; | ||
} | ||
|
||
H[0] = (H[0] + a) >>> 0; | ||
H[1] = (H[1] + b) >>> 0; | ||
H[2] = (H[2] + c) >>> 0; | ||
H[3] = (H[3] + d) >>> 0; | ||
H[4] = (H[4] + e) >>> 0; | ||
} | ||
|
||
return [ | ||
H[0] >> 24 & 0xff, H[0] >> 16 & 0xff, H[0] >> 8 & 0xff, H[0] & 0xff, | ||
H[1] >> 24 & 0xff, H[1] >> 16 & 0xff, H[1] >> 8 & 0xff, H[1] & 0xff, | ||
H[2] >> 24 & 0xff, H[2] >> 16 & 0xff, H[2] >> 8 & 0xff, H[2] & 0xff, | ||
H[3] >> 24 & 0xff, H[3] >> 16 & 0xff, H[3] >> 8 & 0xff, H[3] & 0xff, | ||
H[4] >> 24 & 0xff, H[4] >> 16 & 0xff, H[4] >> 8 & 0xff, H[4] & 0xff | ||
]; | ||
} | ||
|
||
module.exports = sha1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
'use strict'; | ||
|
||
var crypto = require('crypto'); | ||
|
||
function sha1(bytes) { | ||
// support modern Buffer API | ||
if (typeof Buffer.from === 'function') { | ||
if (Array.isArray(bytes)) bytes = Buffer.from(bytes); | ||
else if (typeof bytes === 'string') bytes = Buffer.from(bytes, 'utf8'); | ||
} | ||
|
||
// support pre-v4 Buffer API | ||
else { | ||
if (Array.isArray(bytes)) bytes = new Buffer(bytes); | ||
else if (typeof bytes === 'string') bytes = new Buffer(bytes, 'utf8'); | ||
} | ||
|
||
return crypto.createHash('sha1').update(bytes).digest(); | ||
} | ||
|
||
module.exports = sha1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
var sha1 = require('./lib/sha1-browser'); | ||
var bytesToUuid = require('./lib/bytesToUuid'); | ||
|
||
function uuidToBytes(uuid) { | ||
// Note: We assume we're being passed a valid uuid string | ||
var bytes = []; | ||
uuid.replace(/[a-fA-F0-9]{2}/g, function(hex) { | ||
bytes.push(parseInt(hex, 16)); | ||
}); | ||
|
||
return bytes; | ||
} | ||
|
||
function stringToBytes(str) { | ||
str = unescape(encodeURIComponent(str)); // UTF8 escape | ||
var bytes = new Array(str.length); | ||
for (var i = 0; i < str.length; i++) { | ||
bytes[i] = str.charCodeAt(i); | ||
} | ||
return bytes; | ||
} | ||
|
||
function v5(name, namespace, buf, offset) { | ||
if (typeof(name) == 'string') name = stringToBytes(name); | ||
if (typeof(namespace) == 'string') namespace = uuidToBytes(namespace); | ||
|
||
if (!Array.isArray(name)) throw TypeError('name must be an array of bytes'); | ||
if (!Array.isArray(namespace) || namespace.length != 16) throw TypeError('namespace must be an array of bytes'); | ||
|
||
// Per 4.3 | ||
var bytes = sha1(namespace.concat(name)); | ||
bytes[6] = (bytes[6] & 0x0f) | 0x50; | ||
bytes[8] = (bytes[8] & 0x3f) | 0x80; | ||
|
||
return buf || bytesToUuid(bytes); | ||
} | ||
|
||
// Pre-defined namespaces, per Appendix C | ||
v5.DNS = '6ba7b810-9dad-11d1-80b4-00c04fd430c8'; | ||
v5.URL = '6ba7b811-9dad-11d1-80b4-00c04fd430c8'; | ||
|
||
module.exports = v5; |