Skip to content

Commit

Permalink
Add support to uuid/v5 & unit tests
Browse files Browse the repository at this point in the history
Fixes #1.
  • Loading branch information
VitorLuizC committed Nov 22, 2017
1 parent 5057a58 commit 131f0f2
Show file tree
Hide file tree
Showing 9 changed files with 3,760 additions and 799 deletions.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,24 @@ components **template** and script, like the example below.
class="button"
@click="uuid = $uuid.v4()"
>Generate V4</button>
<button
class="button"
@click="uuid = $uuid.v5()"
>Generate V5</button>
</div>
</template>

<script>
import { uuid } from 'vue-uuid' // uuid object is also exported to things
// outside Vue instance.
export default {
data () {
return {
uuid: this.$uuid() // is equals to this.$uuid.v4()
uuid: uuid.v1(),
v1: this.$uuid.v1(),
v4: this.$uuid.v4(),
v5: this.$uuid.v5()
}
}
}
Expand Down
11 changes: 9 additions & 2 deletions dist/vue-uuid.common.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }

var uuid = _interopDefault(require('uuid'));
var v1 = _interopDefault(require('uuid/v1'));
var v4 = _interopDefault(require('uuid/v4'));
var v5 = _interopDefault(require('uuid/v5'));

var uuid = { v1: v1, v4: v4, v5: v5 };

var install = function (Vue) {
Vue.prototype.$uuid = uuid;
};

module.exports = install;
exports.uuid = uuid;
exports['default'] = install;
12 changes: 12 additions & 0 deletions dist/vue-uuid.es.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import v1 from 'uuid/v1';
import v4 from 'uuid/v4';
import v5 from 'uuid/v5';

var uuid = { v1: v1, v4: v4, v5: v5 };

var install = function (Vue) {
Vue.prototype.$uuid = uuid;
};

export { uuid };
export default install;
145 changes: 135 additions & 10 deletions dist/vue-uuid.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('crypto')) :
typeof define === 'function' && define.amd ? define(['crypto'], factory) :
(global.vueUuid = factory(global.crypto));
}(this, (function (crypto) { 'use strict';
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('crypto')) :
typeof define === 'function' && define.amd ? define(['exports', 'crypto'], factory) :
(factory((global.vueUuid = {}),global.crypto));
}(this, (function (exports,crypto) { 'use strict';

crypto = crypto && crypto.hasOwnProperty('default') ? crypto['default'] : crypto;

Expand Down Expand Up @@ -167,16 +167,141 @@ function v4(options, buf, offset) {

var v4_1 = v4;

var uuid = v4_1;
uuid.v1 = v1_1;
uuid.v4 = v4_1;
// Adapted from Chris Veness' SHA1 code at
// http://www.movable-type.co.uk/scripts/sha1.html
'use strict';

var uuid_1 = uuid;
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
];
}

var sha1Browser = sha1;

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 uuid string or an Array of 16 byte values'); }

// Per 4.3
var bytes = sha1Browser(namespace.concat(name));
bytes[6] = (bytes[6] & 0x0f) | 0x50;
bytes[8] = (bytes[8] & 0x3f) | 0x80;

return buf || bytesToUuid_1(bytes);
}

// Pre-defined namespaces, per Appendix C
v5.DNS = '6ba7b810-9dad-11d1-80b4-00c04fd430c8';
v5.URL = '6ba7b811-9dad-11d1-80b4-00c04fd430c8';

var v5_1 = v5;

var uuid = { v1: v1_1, v4: v4_1, v5: v5_1 };

var install = function (Vue) {
Vue.prototype.$uuid = uuid_1;
Vue.prototype.$uuid = uuid;
};

return install;
exports.uuid = uuid;
exports['default'] = install;

Object.defineProperty(exports, '__esModule', { value: true });

})));
2 changes: 1 addition & 1 deletion dist/vue-uuid.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import uuid from 'uuid'
import v1 from 'uuid/v1'
import v4 from 'uuid/v4'
import v5 from 'uuid/v5'

export const uuid = { v1, v4, v5 }

const install = (Vue) => {
Vue.prototype.$uuid = uuid
Expand Down
Loading

0 comments on commit 131f0f2

Please sign in to comment.