uuid 2.0.2 → 3.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/.eslintrc.json ADDED
@@ -0,0 +1,46 @@
1
+ {
2
+ "root": true,
3
+ "env": {
4
+ "browser": true,
5
+ "commonjs": true,
6
+ "node": true,
7
+ "mocha": true
8
+ },
9
+ "extends": ["eslint:recommended"],
10
+ "installedESLint": true,
11
+ "rules": {
12
+ "array-bracket-spacing": ["warn", "never"],
13
+ "arrow-body-style": ["warn", "as-needed"],
14
+ "arrow-parens": ["warn", "as-needed"],
15
+ "arrow-spacing": "warn",
16
+ "brace-style": "warn",
17
+ "camelcase": "warn",
18
+ "comma-spacing": ["warn", {"after": true}],
19
+ "dot-notation": "warn",
20
+ "indent": ["warn", 2, {
21
+ "SwitchCase": 1,
22
+ "FunctionDeclaration": {"parameters": 1},
23
+ "MemberExpression": 1,
24
+ "CallExpression": {"arguments": 1}
25
+ }],
26
+ "key-spacing": ["warn", {"beforeColon": false, "afterColon": true, "mode": "minimum"}],
27
+ "keyword-spacing": "warn",
28
+ "no-console": "off",
29
+ "no-empty": "off",
30
+ "no-multi-spaces": "warn",
31
+ "no-redeclare": "off",
32
+ "no-restricted-globals": ["warn", "Promise"],
33
+ "no-trailing-spaces": "warn",
34
+ "no-undef": "error",
35
+ "no-unused-vars": ["warn", {"args": "none"}],
36
+ "padded-blocks": ["warn", "never"],
37
+ "object-curly-spacing": ["warn", "never"],
38
+ "quotes": ["warn", "single"],
39
+ "react/prop-types": "off",
40
+ "react/jsx-no-bind": "off",
41
+ "semi": ["warn", "always"],
42
+ "space-before-blocks": ["warn", "always"],
43
+ "space-before-function-paren": ["warn", "never"],
44
+ "space-in-parens": ["warn", "never"]
45
+ }
46
+ }
package/AUTHORS ADDED
@@ -0,0 +1,5 @@
1
+ Robert Kieffer <robert@broofa.com>
2
+ Christoph Tavan <dev@tavan.de>
3
+ AJ ONeal <coolaj86@gmail.com>
4
+ Vincent Voyer <vincent@zeroload.net>
5
+ Roman Shtylman <shtylman@gmail.com>
package/HISTORY.md ADDED
@@ -0,0 +1,28 @@
1
+ # 3.0.1 (2016-11-28)
2
+
3
+ * split uuid versions into separate files
4
+
5
+ # 3.0.0 (2016-11-17)
6
+
7
+ * remove .parse and .unparse
8
+
9
+ # 2.0.0
10
+
11
+ * Removed uuid.BufferClass
12
+
13
+ # 1.4.0
14
+
15
+ * Improved module context detection
16
+ * Removed public RNG functions
17
+
18
+ # 1.3.2
19
+
20
+ * Improve tests and handling of v1() options (Issue #24)
21
+ * Expose RNG option to allow for perf testing with different generators
22
+
23
+ # 1.3.0
24
+
25
+ * Support for version 1 ids, thanks to [@ctavan](https://github.com/ctavan)!
26
+ * Support for node.js crypto API
27
+ * De-emphasizing performance in favor of a) cryptographic quality PRNGs where available and b) more manageable code
28
+
package/LICENSE.md CHANGED
@@ -1,2 +1,21 @@
1
- Copyright (c) 2010-2012 Robert Kieffer
2
- MIT License - http://opensource.org/licenses/mit-license.php
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2010-2016 Robert Kieffer and other contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,44 +1,95 @@
1
- # uuid [![Build Status](https://secure.travis-ci.org/defunctzombie/node-uuid.svg?branch=master)](http://travis-ci.org/defunctzombie/node-uuid) #
2
-
3
- [![browser support](https://ci.testling.com/defunctzombie/node-uuid.png)](https://ci.testling.com/defunctzombie/node-uuid)
1
+ # uuid [![Build Status](https://secure.travis-ci.org/kelektiv/node-uuid.svg?branch=master)](http://travis-ci.org/kelektiv/node-uuid) #
4
2
 
5
3
  Simple, fast generation of [RFC4122](http://www.ietf.org/rfc/rfc4122.txt) UUIDS.
6
4
 
7
5
  Features:
8
6
 
9
- * Generate RFC4122 version 1 or version 4 UUIDs
10
- * Runs in node.js and all browsers.
11
- * Cryptographically strong random # generation on supporting platforms
12
- * 1185 bytes minified and gzip'ed (Want something smaller? Check this [crazy shit](https://gist.github.com/982883) out! )
13
- * [Annotated source code](http://broofa.github.com/node-uuid/docs/uuid.html)
14
-
15
- ## Getting Started
7
+ * Support for version 1, 4 and 5 UUIDs
8
+ * Cross-platform
9
+ * Uses cryptographically-strong random number APIs (when available)
10
+ * Zero-dependency, small footprint (... but not [this small](https://gist.github.com/982883))
16
11
 
17
- Install it in your browser:
12
+ ## Quickstart - CommonJS (Recommended)
18
13
 
19
- ```html
20
- <script src="uuid.js"></script>
14
+ ```shell
15
+ npm install uuid
21
16
  ```
22
17
 
23
- Or in node.js:
18
+ Then generate your uuid version of choice ...
24
19
 
20
+ Version 1 (timestamp):
21
+
22
+ ```javascript
23
+ const uuidv1 = require('uuid/v1');
24
+ uuidv1(); // -> '6c84fb90-12c4-11e1-840d-7b25c5ee775a'
25
25
  ```
26
- npm install uuid
26
+
27
+ Version 4 (random):
28
+
29
+ ```javascript
30
+ const uuidv4 = require('uuid/v4');
31
+ uuidv4(); // -> '110ec58a-a0f2-4ac4-8393-c866d813b8d1'
27
32
  ```
28
33
 
34
+ Version 5 (namespace):
35
+
29
36
  ```javascript
30
- var uuid = require('uuid');
37
+ const uuidv5 = require('uuid/v5');
38
+
39
+ // ... using predefined DNS namespace (for domain names)
40
+ uuidv5('hello.example.com', uuidv5.DNS)); // -> 'fdda765f-fc57-5604-a269-52a7df8164ec'
41
+
42
+ // ... using predefined URL namespace (for, well, URLs)
43
+ uuidv5('http://example.com/hello', uuidv5.URL); // -> '3bbcee75-cecc-5b56-8031-b6641c1ed1f1'
44
+
45
+ // ... using a custom namespace
46
+ const MY_NAMESPACE = '<UUID string you previously generated elsewhere>';
47
+ uuidv5('Hello, World!', MY_NAMESPACE); // -> '90123e1c-7512-523e-bb28-76fab9f2f73d'
48
+ ```
49
+
50
+ ## Quickstart - Browser-ready Versions
31
51
 
32
- // Generate a v1 (time-based) id
33
- uuid.v1(); // -> '6c84fb90-12c4-11e1-840d-7b25c5ee775a'
52
+ Browser-ready versions of this module are available via [wzrd.in](https://github.com/jfhbrook/wzrd.in).
34
53
 
35
- // Generate a v4 (random) id
36
- uuid.v4(); // -> '110ec58a-a0f2-4ac4-8393-c866d813b8d1'
54
+ For version 1 uuids:
55
+
56
+ ```html
57
+ <script src="http://wzrd.in/standalone/uuid%2Fv1@latest"></script>
58
+ <script>
59
+ uuidv1(); // -> v1 UUID
60
+ </script>
61
+ ```
62
+
63
+ For version 4 uuids:
64
+
65
+ ```html
66
+ <script src="http://wzrd.in/standalone/uuid%2Fv4@latest"></script>
67
+ <script>
68
+ uuidv4(); // -> v4 UUID
69
+ </script>
70
+ ```
71
+
72
+ For version 5 uuids:
73
+
74
+ ```html
75
+ <script src="http://wzrd.in/standalone/uuid%2Fv5@latest"></script>
76
+ <script>
77
+ uuidv5('http://example.com/hello', uuidv5.URL); // -> v5 UUID
78
+ </script>
37
79
  ```
38
80
 
39
81
  ## API
40
82
 
41
- ### uuid.v1([`options` [, `buffer` [, `offset`]]])
83
+ ### Version 1
84
+
85
+ ```javascript
86
+ const uuidv1 = require('uuid/v1');
87
+
88
+ // Allowed arguments
89
+ uuidv1();
90
+ uuidv1(options);
91
+ uuidv1(options, buffer, offset);
92
+ ```
42
93
 
43
94
  Generate and return a RFC4122 v1 (timestamp-based) UUID.
44
95
 
@@ -54,14 +105,12 @@ Generate and return a RFC4122 v1 (timestamp-based) UUID.
54
105
 
55
106
  Returns `buffer`, if specified, otherwise the string form of the UUID
56
107
 
57
- Notes:
58
-
59
- 1. The randomly generated node id is only guaranteed to stay constant for the lifetime of the current JS runtime. (Future versions of this module may use persistent storage mechanisms to extend this guarantee.)
108
+ Note: The <node> id is generated guaranteed to stay constant for the lifetime of the current JS runtime. (Future versions of this module may use persistent storage mechanisms to extend this guarantee.)
60
109
 
61
110
  Example: Generate string UUID with fully-specified options
62
111
 
63
112
  ```javascript
64
- uuid.v1({
113
+ uuidv1({
65
114
  node: [0x01, 0x23, 0x45, 0x67, 0x89, 0xab],
66
115
  clockseq: 0x1234,
67
116
  msecs: new Date('2011-11-01').getTime(),
@@ -73,24 +122,27 @@ Example: In-place generation of two binary IDs
73
122
 
74
123
  ```javascript
75
124
  // Generate two ids in an array
76
- var arr = new Array(32); // -> []
77
- uuid.v1(null, arr, 0); // -> [02 a2 ce 90 14 32 11 e1 85 58 0b 48 8e 4f c1 15]
78
- uuid.v1(null, arr, 16); // -> [02 a2 ce 90 14 32 11 e1 85 58 0b 48 8e 4f c1 15 02 a3 1c b0 14 32 11 e1 85 58 0b 48 8e 4f c1 15]
79
-
80
- // Optionally use uuid.unparse() to get stringify the ids
81
- uuid.unparse(buffer); // -> '02a2ce90-1432-11e1-8558-0b488e4fc115'
82
- uuid.unparse(buffer, 16) // -> '02a31cb0-1432-11e1-8558-0b488e4fc115'
125
+ const arr = new Array(32); // -> []
126
+ uuidv1(null, arr, 0); // -> [02 a2 ce 90 14 32 11 e1 85 58 0b 48 8e 4f c1 15]
127
+ uuidv1(null, arr, 16); // -> [02 a2 ce 90 14 32 11 e1 85 58 0b 48 8e 4f c1 15 02 a3 1c b0 14 32 11 e1 85 58 0b 48 8e 4f c1 15]
83
128
  ```
84
129
 
85
- ### uuid.v4([`options` [, `buffer` [, `offset`]]])
130
+ ### Version 4
131
+
132
+ ```javascript
133
+ const uuidv4 = require('uuid/v4')
134
+
135
+ // Allowed arguments
136
+ uuidv4();
137
+ uuidv4(options);
138
+ uuidv4(options, buffer, offset);
139
+ ```
86
140
 
87
141
  Generate and return a RFC4122 v4 UUID.
88
142
 
89
143
  * `options` - (Object) Optional uuid state to apply. Properties may include:
90
-
91
144
  * `random` - (Number[16]) Array of 16 numbers (0-255) to use in place of randomly generated values
92
- * `rng` - (Function) Random # generator to use. Set to one of the built-in generators - `uuid.mathRNG` (all platforms), `uuid.nodeRNG` (node.js only), `uuid.whatwgRNG` (WebKit only) - or a custom function that returns an array[16] of byte values.
93
-
145
+ * `rng` - (Function) Random # generator function that returns an Array[16] of byte values (0-255)
94
146
  * `buffer` - (Array | Buffer) Array or buffer where UUID bytes are to be written.
95
147
  * `offset` - (Number) Starting index in `buffer` at which to begin writing.
96
148
 
@@ -111,95 +163,65 @@ uuid.v4({
111
163
  Example: Generate two IDs in a single buffer
112
164
 
113
165
  ```javascript
114
- var buffer = new Array(32); // (or 'new Buffer' in node.js)
166
+ const buffer = new Array(32); // (or 'new Buffer' in node.js)
115
167
  uuid.v4(null, buffer, 0);
116
168
  uuid.v4(null, buffer, 16);
117
169
  ```
118
170
 
119
- ### uuid.parse(id[, buffer[, offset]])
120
- ### uuid.unparse(buffer[, offset])
121
-
122
- Parse and unparse UUIDs
123
-
124
- * `id` - (String) UUID(-like) string
125
- * `buffer` - (Array | Buffer) Array or buffer where UUID bytes are to be written. Default: A new Array or Buffer is used
126
- * `offset` - (Number) Starting index in `buffer` at which to begin writing. Default: 0
127
-
128
- Example parsing and unparsing a UUID string
171
+ ### Version 5
129
172
 
130
173
  ```javascript
131
- var bytes = uuid.parse('797ff043-11eb-11e1-80d6-510998755d10'); // -> <Buffer 79 7f f0 43 11 eb 11 e1 80 d6 51 09 98 75 5d 10>
132
- var string = uuid.unparse(bytes); // -> '797ff043-11eb-11e1-80d6-510998755d10'
174
+ const uuidv5 = require('uuid/v4');
175
+
176
+ // Allowed arguments
177
+ uuidv5(name, namespace);
178
+ uuidv5(name, namespace, buffer);
179
+ uuidv5(name, namespace, buffer, offset);
133
180
  ```
134
181
 
135
- ### uuid.noConflict()
182
+ Generate and return a RFC4122 v4 UUID.
136
183
 
137
- (Browsers only) Set `uuid` property back to it's previous value.
184
+ * `name` - (String | Array[]) "name" to create UUID with
185
+ * `namespace` - (String | Array[]) "namespace" UUID either as a String or Array[16] of byte values
186
+ * `buffer` - (Array | Buffer) Array or buffer where UUID bytes are to be written.
187
+ * `offset` - (Number) Starting index in `buffer` at which to begin writing. Default = 0
138
188
 
139
- Returns the uuid object.
189
+ Returns `buffer`, if specified, otherwise the string form of the UUID
140
190
 
141
191
  Example:
142
192
 
143
193
  ```javascript
144
- var myUuid = uuid.noConflict();
145
- myUuid.v1(); // -> '6c84fb90-12c4-11e1-840d-7b25c5ee775a'
146
- ```
147
-
148
- ## Deprecated APIs
149
-
150
- Support for the following v1.2 APIs is available in v1.3, but is deprecated and will be removed in the next major version.
194
+ // Generate a unique namespace (typically you would do this once, outside of
195
+ // your project, then bake this value into your code)
196
+ const uuidv4 = require('uuid/v4');
197
+ const MY_NAMESPACE = uuidv4(); //
151
198
 
152
- ### uuid([format [, buffer [, offset]]])
153
-
154
- uuid() has become uuid.v4(), and the `format` argument is now implicit in the `buffer` argument. (i.e. if you specify a buffer, the format is assumed to be binary).
199
+ // Generate a couple namespace uuids
200
+ const uuidv5 = require('uuid/v5');
201
+ uuidv5('hello', MY_NAMESPACE);
202
+ uuidv5('world', MY_NAMESPACE);
203
+ ```
155
204
 
156
205
  ## Testing
157
206
 
158
- In node.js
159
-
160
- ```
161
- > cd test
162
- > node test.js
207
+ ```shell
208
+ npm test
163
209
  ```
164
210
 
165
- In Browser
211
+ ## Deprecated / Browser-ready API
166
212
 
167
- ```
168
- open test/test.html
169
- ```
213
+ The API below is available for legacy purposes and is not expected to be available post-3.X
170
214
 
171
- ### Benchmarking
215
+ ```javascript
216
+ const uuid = require('uuid');
172
217
 
173
- Requires node.js
218
+ uuid.v1(...); // alias of uuid/v1
219
+ uuid.v4(...); // alias of uuid/v4
220
+ uuid(...); // alias of uuid/v4
174
221
 
222
+ // uuid.v5() is not supported in this API
175
223
  ```
176
- cd benchmark/
177
- npm install
178
- node benchmark.js
179
- ```
180
-
181
- For a more complete discussion of uuid performance, please see the `benchmark/README.md` file, and the [benchmark wiki](https://github.com/broofa/uuid/wiki/Benchmark)
182
-
183
- For browser performance [checkout the JSPerf tests](http://jsperf.com/node-uuid-performance).
184
-
185
- ## Release notes
186
-
187
- ### 2.0.0
188
-
189
- * Removed uuid.BufferClass
190
-
191
- ### 1.4.0
192
-
193
- * Improved module context detection
194
- * Removed public RNG functions
195
-
196
- ### 1.3.2
197
-
198
- * Improve tests and handling of v1() options (Issue #24)
199
- * Expose RNG option to allow for perf testing with different generators
200
224
 
201
- ### 1.3.0
225
+ ## Legacy node-uuid package
202
226
 
203
- * Support for version 1 ids, thanks to [@ctavan](https://github.com/ctavan)!
204
- * Support for node.js crypto API
205
- * De-emphasizing performance in favor of a) cryptographic quality PRNGs where available and b) more manageable code
227
+ The code for the legacy node-uuid package is available in the `node-uuid` branch.
package/bin/uuid ADDED
@@ -0,0 +1,50 @@
1
+ #!/usr/bin/env node
2
+ var assert = require('assert');
3
+
4
+ function usage() {
5
+ console.log('Usage:');
6
+ console.log(' uuid');
7
+ console.log(' uuid v1');
8
+ console.log(' uuid v4');
9
+ console.log(' uuid v5 <name> <namespace uuid>');
10
+ console.log(' uuid --help');
11
+ console.log('\nNote: <namespace uuid> may be "URL" or "DNS" to use the corresponding UUIDs defined by RFC4122');
12
+ }
13
+
14
+ var args = process.argv.slice(2);
15
+
16
+ if (args.indexOf('--help') >= 0) {
17
+ usage();
18
+ process.exit(0);
19
+ }
20
+ var version = args.shift() || 'v4';
21
+
22
+ switch (version) {
23
+ case 'v1':
24
+ var uuidV1 = require('../v1');
25
+ console.log(uuidV1());
26
+ break;
27
+
28
+ case 'v4':
29
+ var uuidV4 = require('../v4');
30
+ console.log(uuidV4());
31
+ break;
32
+
33
+ case 'v5':
34
+ var uuidV5 = require('../v5');
35
+
36
+ var name = args.shift();
37
+ var namespace = args.shift();
38
+ assert(name != null, 'v5 name not specified');
39
+ assert(namespace != null, 'v5 namespace not specified');
40
+
41
+ if (namespace == 'URL') namespace = uuidV5.URL;
42
+ if (namespace == 'DNS') namespace = uuidV5.DNS;
43
+
44
+ console.log(uuidV5(name, namespace));
45
+ break;
46
+
47
+ default:
48
+ usage();
49
+ process.exit(1);
50
+ }
package/index.js ADDED
@@ -0,0 +1,8 @@
1
+ var v1 = require('./v1');
2
+ var v4 = require('./v4');
3
+
4
+ var uuid = v4;
5
+ uuid.v1 = v1;
6
+ uuid.v4 = v4;
7
+
8
+ module.exports = uuid;
@@ -0,0 +1,23 @@
1
+ /**
2
+ * Convert array of 16 byte values to UUID string format of the form:
3
+ * XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
4
+ */
5
+ var byteToHex = [];
6
+ for (var i = 0; i < 256; ++i) {
7
+ byteToHex[i] = (i + 0x100).toString(16).substr(1);
8
+ }
9
+
10
+ function bytesToUuid(buf, offset) {
11
+ var i = offset || 0;
12
+ var bth = byteToHex;
13
+ return bth[buf[i++]] + bth[buf[i++]] +
14
+ bth[buf[i++]] + bth[buf[i++]] + '-' +
15
+ bth[buf[i++]] + bth[buf[i++]] + '-' +
16
+ bth[buf[i++]] + bth[buf[i++]] + '-' +
17
+ bth[buf[i++]] + bth[buf[i++]] + '-' +
18
+ bth[buf[i++]] + bth[buf[i++]] +
19
+ bth[buf[i++]] + bth[buf[i++]] +
20
+ bth[buf[i++]] + bth[buf[i++]];
21
+ }
22
+
23
+ module.exports = bytesToUuid;
@@ -0,0 +1,33 @@
1
+ // Unique ID creation requires a high quality random # generator. In the
2
+ // browser this is a little complicated due to unknown quality of Math.random()
3
+ // and inconsistent support for the `crypto` API. We do the best we can via
4
+ // feature-detection
5
+ var rng;
6
+
7
+ var crypto = global.crypto || global.msCrypto; // for IE 11
8
+ if (crypto && crypto.getRandomValues) {
9
+ // WHATWG crypto RNG - http://wiki.whatwg.org/wiki/Crypto
10
+ var rnds8 = new Uint8Array(16); // eslint-disable-line no-undef
11
+ rng = function whatwgRNG() {
12
+ crypto.getRandomValues(rnds8);
13
+ return rnds8;
14
+ };
15
+ }
16
+
17
+ if (!rng) {
18
+ // Math.random()-based (RNG)
19
+ //
20
+ // If all else fails, use Math.random(). It's fast, but is of unspecified
21
+ // quality.
22
+ var rnds = new Array(16);
23
+ rng = function() {
24
+ for (var i = 0, r; i < 16; i++) {
25
+ if ((i & 0x03) === 0) r = Math.random() * 0x100000000;
26
+ rnds[i] = r >>> ((i & 0x03) << 3) & 0xff;
27
+ }
28
+
29
+ return rnds;
30
+ };
31
+ }
32
+
33
+ module.exports = rng;
package/lib/rng.js ADDED
@@ -0,0 +1,10 @@
1
+ // Unique ID creation requires a high quality random # generator. In node.js
2
+ // this is pretty straight-forward - we use the crypto API.
3
+
4
+ var rb = require('crypto').randomBytes;
5
+
6
+ function rng() {
7
+ return rb(16);
8
+ }
9
+
10
+ module.exports = rng;
@@ -0,0 +1,85 @@
1
+ // Adapted from Chris Veness' SHA1 code at
2
+ // http://www.movable-type.co.uk/scripts/sha1.html
3
+ 'use strict';
4
+
5
+ function f(s, x, y, z) {
6
+ switch (s) {
7
+ case 0: return (x & y) ^ (~x & z);
8
+ case 1: return x ^ y ^ z;
9
+ case 2: return (x & y) ^ (x & z) ^ (y & z);
10
+ case 3: return x ^ y ^ z;
11
+ }
12
+ }
13
+
14
+ function ROTL(x, n) {
15
+ return (x << n) | (x>>> (32 - n));
16
+ }
17
+
18
+ function sha1(bytes) {
19
+ var K = [0x5a827999, 0x6ed9eba1, 0x8f1bbcdc, 0xca62c1d6];
20
+ var H = [0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0];
21
+
22
+ if (typeof(bytes) == 'string') {
23
+ var msg = unescape(encodeURIComponent(bytes)); // UTF8 escape
24
+ bytes = new Array(msg.length);
25
+ for (var i = 0; i < msg.length; i++) bytes[i] = msg.charCodeAt(i);
26
+ }
27
+
28
+ bytes.push(0x80);
29
+
30
+ var l = bytes.length/4 + 2;
31
+ var N = Math.ceil(l/16);
32
+ var M = new Array(N);
33
+
34
+ for (var i=0; i<N; i++) {
35
+ M[i] = new Array(16);
36
+ for (var j=0; j<16; j++) {
37
+ M[i][j] =
38
+ bytes[i * 64 + j * 4] << 24 |
39
+ bytes[i * 64 + j * 4 + 1] << 16 |
40
+ bytes[i * 64 + j * 4 + 2] << 8 |
41
+ bytes[i * 64 + j * 4 + 3];
42
+ }
43
+ }
44
+
45
+ M[N - 1][14] = ((bytes.length - 1) * 8) /
46
+ Math.pow(2, 32); M[N - 1][14] = Math.floor(M[N - 1][14]);
47
+ M[N - 1][15] = ((bytes.length - 1) * 8) & 0xffffffff;
48
+
49
+ for (var i=0; i<N; i++) {
50
+ var W = new Array(80);
51
+
52
+ for (var t=0; t<16; t++) W[t] = M[i][t];
53
+ for (var t=16; t<80; t++) {
54
+ W[t] = ROTL(W[t - 3] ^ W[t - 8] ^ W[t - 14] ^ W[t - 16], 1);
55
+ }
56
+
57
+ var a = H[0], b = H[1], c = H[2], d = H[3], e = H[4];
58
+
59
+ for (var t=0; t<80; t++) {
60
+ var s = Math.floor(t/20);
61
+ var T = ROTL(a, 5) + f(s, b, c, d) + e + K[s] + W[t] >>> 0;
62
+ e = d;
63
+ d = c;
64
+ c = ROTL(b, 30) >>> 0;
65
+ b = a;
66
+ a = T;
67
+ }
68
+
69
+ H[0] = (H[0] + a) >>> 0;
70
+ H[1] = (H[1] + b) >>> 0;
71
+ H[2] = (H[2] + c) >>> 0;
72
+ H[3] = (H[3] + d) >>> 0;
73
+ H[4] = (H[4] + e) >>> 0;
74
+ }
75
+
76
+ return [
77
+ H[0] >> 24 & 0xff, H[0] >> 16 & 0xff, H[0] >> 8 & 0xff, H[0] & 0xff,
78
+ H[1] >> 24 & 0xff, H[1] >> 16 & 0xff, H[1] >> 8 & 0xff, H[1] & 0xff,
79
+ H[2] >> 24 & 0xff, H[2] >> 16 & 0xff, H[2] >> 8 & 0xff, H[2] & 0xff,
80
+ H[3] >> 24 & 0xff, H[3] >> 16 & 0xff, H[3] >> 8 & 0xff, H[3] & 0xff,
81
+ H[4] >> 24 & 0xff, H[4] >> 16 & 0xff, H[4] >> 8 & 0xff, H[4] & 0xff
82
+ ];
83
+ }
84
+
85
+ module.exports = sha1;
package/lib/sha1.js ADDED
@@ -0,0 +1,21 @@
1
+ 'use strict';
2
+
3
+ var crypto = require('crypto');
4
+
5
+ function sha1(bytes) {
6
+ // support modern Buffer API
7
+ if (typeof Buffer.from === 'function') {
8
+ if (Array.isArray(bytes)) bytes = Buffer.from(bytes);
9
+ else if (typeof bytes === 'string') bytes = Buffer.from(bytes, 'utf8');
10
+ }
11
+
12
+ // support pre-v4 Buffer API
13
+ else {
14
+ if (Array.isArray(bytes)) bytes = new Buffer(bytes);
15
+ else if (typeof bytes === 'string') bytes = new Buffer(bytes, 'utf8');
16
+ }
17
+
18
+ return crypto.createHash('sha1').update(bytes).digest();
19
+ }
20
+
21
+ module.exports = sha1;